Searching words in a file containing a pattern

Hi all,
I would like to print words in a file seperated by whitespaces containing a specific pattern like "="

e.g. I have a file1 containing strings like
%cat file1
The= some= in
wish= born

<eof> .I want to display only those words containing = i.e The= , some=,wish=

when i grep for pattern "=" in file1 , I get the entire line..
So I decided to loop like
for words in $( grep -x "=" file1)
do
# How do I print only those words containing = here
done

or is there a better way?

Hi,

I guess if you use regular expression avialable in perl it would be good..

But still if you insist to do it using Unix, then in that case you might have to use awk command which would be a little complex..:slight_smile:

Regards,
Abhisek

awk

# awk '{for(i=1;i<=NF;i++){if ($i ~/=$/) {print $i}}}' file
The=
some=
wish=

alternative solution with Python

for line in open("file"):
    line=line.strip().split()
    for items in line:
        if items.endswith("="):
            print items,

With Perl:

perl -nle'print$1while/(\w+=)/g' infile

With GNU grep:

grep -Eo \\w+= infile
> cat file15
The= some= in
wish= born

> tr " " "\n" <file15
The=
some=
in
wish=
born

> tr " " "\n" <file15 | grep "="
The=
some=
wish=

Thanks a lot for the ideas :slight_smile: