Operating System - HP-UX
1828582 Members
2035 Online
109982 Solutions
New Discussion

sed delete lines from `cat somefile`

 
FLQ
Valued Contributor

sed delete lines from `cat somefile`

Hi all,

here is the problem....

for i in `cat /tmp/sometxtfile`
do
sed '/$i/d' inputfile > outfile
done

I do not get my inputfile cleaned up with the content of sometxtfile

How would you do it???

TIA

Francis
5 REPLIES 5
Patrick Wallek
Honored Contributor

Re: sed delete lines from `cat somefile`

I haven't tried the script yet, but the first problem I see is that the same inputfile and outputfile will be used every time.

You are doing the sed once and writing to outputfile, but the next time, next value of $i, you are still useing the original inputfile and overwriting outputfile.

I think somewhere you need to move mv outputfile to inputfile or use variables for them.

something like:

INPUT=inputfile
OUTPUT=outputfile
for i in `cat /tmp/sometxtfile`
do
sed -e /$i/d $INPUT > $OUTPUT
mv $OUTPUT $OUTPUT.1
INPUT=$OUTPUT.1
OUTPUT=outputfile
done

Once this is done, have a look at outputfile.1 and I think you will be pleased with the results.
FLQ
Valued Contributor

Re: sed delete lines from `cat somefile`

It did the trick Sir... :-))

Happy camper :-))


Thanx again

Francis
FLQ
Valued Contributor

Re: sed delete lines from `cat somefile`

Sorry I tried to assign points but I got a 404

I'll try l8r


Francis
Rodney Hills
Honored Contributor

Re: sed delete lines from `cat somefile`

You can elimnate running sed for each entry by adding the edit commands to sometxtfile and feeding that directly into sed. This will then only run sed once and not have to switch your input and output files back and forth. (I know it looks nasty with all the "\", but it works is very fast!).

cat /tmp/sometxtfile | sed 's/^(.*)$/\/\1\/d/' >/tmp/file2
sed -f /tmp/file2 outfile

-- Rod Hills
There be dragons...
Frank Slootweg
Honored Contributor

Re: sed delete lines from `cat somefile`

It depends a little on what exactly you want to do, but "grep -v -f pattern_file" might be a simpler solution. "-v" means "All lines but those matching are printed.". You probably also want "-F" which uses fixed strings instead of regular expressions.