[Solved] awk to remove lines

Hi,

I have a file with contents.

file1:

<2013 tttaaa
abc123
<2013 gggdddd
<2013 sssssss
<2013 eeeee

I need to remove the lines which do not have the word "tttaaa"

can some one help ?

grep tttaaa file > newfile

You should be more precise in your requirements - Can tttaaa occur anywhere in the line, or always in field 2, or always at the end of the line, or...

If it's field 2, you could do:

awk '$2 ~ /tttaaa/' inputfile.txt

(or $0 or just '/tttaaa/' for anywhere in the line - although for that case grep is probably simpler)

If -i option is supported with sed version, try:

sed -i '/tttaaa/!d' infile

For exact word match, try:

sed -i '/\btttaaa\b/!d' infile
<2013 abc frgv tttaaa
abc123
<2013 bec gggdddd
<2013 cdf sssssss
<2013 frt eeeee

Thanks for the replies. I need one more. And if at i wanted to remove all the lines which matches the word "2013" and do no match "tttaaa" ?

In this case i need the output as

<2013 abc frgv tttaaa
abc123

Can this be done ?

awk '/2013/ && /tttaa/ || !/^</' file
1 Like

I think this is the correct logic for "remove lines that match 2013 and do not contain tttaaa". The way the line starts with < is just a coincidence:

$ sed "/2013/ { /tttaaa/ ! d }" file
<2013 abc frgv tttaaa
abc123
1 Like

it worked. thanks guys