Abhinash
Abhinash

Reputation: 35

awk command to print only part of matching lines

awk command to compare lines in file and print only first line if there are some new words in other lines.

For example: file.txt is having

i am going
i am going today
i am going with my friend

output should be

I am going

Upvotes: 0

Views: 295

Answers (1)

karakfa
karakfa

Reputation: 67567

this will work for the sample input but perhaps will fail for the actual one, unless you have a representative input we wouldn't know...

$ awk 'NR>1 && $0~p {if(!f) print p; f=1; next} {p=$0; f=0}' file 
i am going

you may want play with p=$0 to restrict matching number of fields if the line lengths are not in increasing order...

Upvotes: 0

Related Questions