Help me grep.....

Hi unix geeks,

I am a newbie to shell scripting.
I want to know how to match a particular string in a file and extract the matched string(s) into another file.

The input file say file1 is as shown,

1 line Changed paths: M /client/j2me/ce/sw/v.0/j2me/src/main/java/com/airone/cclient/j2me/database/RecordsList.java  Pending Contact Count issue after SAS in Samsung 5230, 1 line Changed paths: M /client/j2me/ce/sw/v0/j2me/src/main/resources/mb/resources.xml Error message has changed for invalid pin. 1 line Changed paths: M /client/j2me/ce/sw/v.0/j2me/src/main/java/com/airone/cclient/mb/MF.java Samsung Wave S8500 UI changes 

So each of the bold paths starts with /client.
Now i want to extract all the strings that start with /client ie.the highlighted strings, and i want to dump them in a seperate file.
The output is as shown ,
file2

/client/j2me/ce/sw/v.0/j2me/src/main/java/com/airone/cclient/j2me/database/RecordsList.java
/client/j2me/ce/sw/v0/j2me/src/main/resources/mb/resources.xml
/client/j2me/ce/sw/v.0/j2me/src/main/java/com/airone/cclient/mb/MF.java

Please help me out in achieving this.
Thanks,
Manju.

grep -o /client[^\ ]* inputfile > outputfile
1 Like

The grep command Subbeh proposed works fine if your system's grep supports the -o option. If your system's grep doesn't support the -o option, the following command will also work:

awk '{for(i=1; i<=NF; i++) if(match($i, /^\/client\//)) print $i}' file1

Note: Use /usr/xpg4/bin/awk or nawk instead of awk if you are on a Solaris system.

2 Likes

Hai subhesh,
Thaq for the reply.
The problem is solved, but i just wanted to know what the -o option stands for.
Thanks,
Kashyap.

---------- Post updated at 12:56 PM ---------- Previous update was at 12:55 PM ----------

Hai DonCragun,

Thanks for the altier,
Both the solutions looks fine.

Thanks,
Kashyap.

Man grep
-o, --only-matching
Show only the part of a matching line that matches PATTERN