Print only matched string instead of entire line

Hi,

I have a file whose lines are something like

Tchampionspsq^@~^@^^^A^@^@^@^A^A^A��^@^@^@^@^@^@^@^@^@^@^A^@^@^@^@^?�^@^@^@^@^@^@^@?�^@^@^@^@^@^@pppsq^@~^@#@^@^@^@^@^@^Hw^H^@^@^@^K^@^@^@^@xp^At^@^FTtime2psq^@     ~^@^^^A^@^@^@^B^A

I need to extract all words matching

T[a-z]*psq

from the file.
Thing is I need only matched string and not the entre line. While using grep -o option, its saying that the pattern has been matched.

Thanks,
Shekhar

The file is full of binary data so you'll have to force grep to believe it's actually a text file. (I'm not really convinced either. :confused:)

grep -a -o pattern file

Suppose file abc.txt contains:

Tchampionspsq^@~^@^^^A^@^@^@^A^A^A��^@^@^@^@^@^@^@^@^@^@^A^@^@^@^@^?�^@^@^@^@^@^@^@?�^@^@^@^@^@^@pppsq^@~^@#@^@^@^@^@^@^Hw^H^@^@^@^K^@^@^@^@xp^At^@^FTtime2psq^@     ~^@^^^A^@^@^@^B^A

now using the grep command,

$$ grep -o "T[a-z]*psq" abc.txt

gives output:

Tchampionspsq

which is according to your expectation.

I have checked it on ubuntu.It went fine.

It doesn't contain those literal characters. It contains masses of binary characters that are represented as control-a, control-@, and so forth. grep will (rightly) determine that this is a binary file with information not representable on a terminal, and just print yes/no information instead of matching lines.

That is why you need -a to force it into treating it as text.

Maybe try the strings command to simplify the problem:

strings filename | grep -o "T[a-z]*psq"