• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Get the exact string - not the entire line using grep

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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|
 
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic