Petryanu
Petryanu

Reputation: 141

Finding a string in a line in a file and displaying it in unix

I have a file which has three lines as follows;

/abdf/rewisf/dkjrtr/ser/co.c  -o /abdf/rewisf/dkjrtr/wrewr.o
/erwrw/srejr/eroi -I/serwr/erw/reti/ -o /erwer/tryps/trtwre.o
/serer/rtppqwe/rtr -lm -Isejr/ekrlafk -o ekr/eirw/qwpog/gset.o

I need a unix command which displays only the file which ends with ".o" extension with the full pathname in the above line. For example, in the above case, my requirement should display following in each line;

/abdf/rewisf/dkjrtr/wrewr.o
/erwer/tryps/trtwre.o
/ekr/eirw/qwpog/gset.o

Please help. Your help is highly appreciated. Thanks in advance.

Upvotes: 0

Views: 55

Answers (1)

Josh Jolly
Josh Jolly

Reputation: 11786

Using sed:

sed -n 's/^.*\( .*\.o\).*$/\1/gp' test.txt

Will only find one instance of the pattern! If you have a version of grep which accepts -o as an option, you can use this:

egrep -o ' .*.o' test.txt 

Upvotes: 1

Related Questions