Operating System - HP-UX
1753964 Members
7106 Online
108811 Solutions
New Discussion юеВ

Deleting file lines in shell script

 

Deleting file lines in shell script

Is there a way to delete a line in a file if you are reading from file1 and you want to delete line from file2 matching certain critera? I'm trying to use standard UNIX commands to accomplish.
3 REPLIES 3
Alan Riggs
Honored Contributor

Re: Deleting file lines in shell script

The sed command is probably what you want, but the syntax can be tricky. The man page helps, but you might want to pick up some additional references. (O'reilly is always a good place to start.)

If you post more details about your specific problem, I might be able to give you a more complete response.
Rick Garland
Honored Contributor

Re: Deleting file lines in shell script

The use of 'sed' will accomplish your goal.

The question of "certain criteria" needs to be established. Once done, you could invoke sed. Example to delete blank lines with a file: sed -e /^$/d

Another option to delete lines: sed -e /$pattern/,^$/d file > newfile
This will delete the line matching the pattern up to the first blank line.
James Odak
Valued Contributor

Re: Deleting file lines in shell script

If what you are trying to do is delete lines from two files that are the same in each file try this, i made this when trying to do the same thing
This only works if each line is one string with no spaces

cat file1 file2 >> tempfile
sort tempfile > temp2file
rm tempfile
uniq -d temp2file > temp3file
rm temp2file
(this will be a list of all line the same in both files)
(now to delete those lines)

for X in `cat temp3file`
do
grep -v ^$X$ file1 > temp4file
cp temp4file file1
grep -v ^$X$ file2 > temp4file
cp temp4file file2
sleep 1
done

then remember to delete temp3file and temp4file
i do all this in a script wich is much easier
but this is how i'd do it from the command line