search string in shell and print new line if not found

3802 views linux
7

Hi I have command with output :

Test TXT some Text1 Test TXT some Text2 Done TXT some Text3 Done TRT some Text1 Test TXT copy Text5 Done TXT some Text2 Test TRT many Text4 Test TRT some Text6 Done TXT many Text9

now I want to search "TXT" and if found print the rest of the line.and if not found print a blank line like below :

some Text1 some Text2 some Text3 . copy Text5 some Text2 . . many Text9

(replace "." with a blank line please)

How to do this without using sed.

answered question

1 Answer

12

awk -F 'TXT' '{if(/TXT/) {print $2} else {print ""}}' file

Output:

 some Text1
 some Text2
 some Text3

 copy Text5
 some Text2


 many Text9

I assume that the string TXT occurs only once per line.

posted this

Have an answer?

JD

Please login first before posting an answer.