This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Linux / UNIX and the fly likes Get the exact string - not the entire line using grep Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Get the exact string - not the entire line using grep" Watch "Get the exact string - not the entire line using grep" New topic
Author

Get the exact string - not the entire line using grep

yuga devi
Greenhorn

Joined: Aug 07, 2008
Posts: 22
Hi,


Is there any command in unix, to grep the exact word from a file, not the entire line. I have tried using grep -w, but it returns the entire line.

For ex, my file consists of

Log14534|0A353C0B.4A9282E3.00012623.00002EBE|[color=red]123456789|START TRANSACTION|host1|Mon Aug 24 08:56:27 EDT 2009|[/color]

In this, i want to grep, dynamically passing the unique no "123456789" and need to ge the word, "START TRANSACTION".

Please help.
-- Yuga
yuga devi
Greenhorn

Joined: Aug 07, 2008
Posts: 22
Hi, -sorry for the confusion in the highlighted color


Is there any command in unix, to grep the exact word from a file, not the entire line. I have tried using grep -w, but it returns the entire line.

For ex, my file consists of

Log14534|0A353C0B.4A9282E3.00012623.00002EBE|123456789|START TRANSACTION|host1|Mon Aug 24 08:56:27 EDT 2009|

In this, i want to grep, dynamically passing the unique no "123456789" and need to ge the word, "START TRANSACTION".

Please help.
-- Yuga


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39577
    
  27
grep is line-oriented - it can't return partial results. But you can pipe its output into another tool -like sed or awk- that can extract parts of lines.


Ping & DNS - updated with new look and Ping home screen widget
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

yuga devi wrote:Log14534|0A353C0B.4A9282E3.00012623.00002EBE|123456789|START TRANSACTION|host1|Mon Aug 24 08:56:27 EDT 2009|

In this, i want to grep, dynamically passing the unique no "123456789" and need to ge the word, "START TRANSACTION".


egrep is the extended grep to match regular expressions.
-o is a switch to output only matching parts.

I understood that you search for 12..9 and want the next string as result.


The pipesymbol | has to be masked, because egrep would else threat it as OR.

Result: 123456789|START TRANSACTION|


http://home.arcor.de/hirnstrom/bewerbung
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Get the exact string - not the entire line using grep
 
Similar Threads
return a value from shell script to java program
Unix Ksh split command
unix split command
FTP upload
How to extract columns of coordinate values from text file (in java) ?