Friday 15 January 2010

linux - Removing strings from file using bash script -



linux - Removing strings from file using bash script -

i want delete specific strings file. seek use:

for line3 in $(cat 2.txt) if grep -fxq $line3 4.txt sed -i /$line3/d 4.txt fi done

i want code delete lines 4.txt if in 2.txt, loop deletes lines 4.txt , have no thought why. can tell wrong code ?

2.txt:

a ab abc

4.txt:

a abc abcdef

you can via single awk command:

awk 'argv[1] == filename && fnr==nr {a[$1];next} !($1 in a)' 2.txt 4.txt abcdef

to store output 4.txt use:

awk 'argv[1] == filename && fnr==nr {a[$1];next} !($1 in a)' 2.txt 4.txt > _tmp && mv _tmp 4.txt

ps: added argv[1] == filename && take care of empty file case noted @pjh below.

linux bash

No comments:

Post a Comment