LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 03-26-2015, 02:54 AM   #1
Senthilv
Member
 
Registered: Jan 2011
Posts: 66
Blog Entries: 2

Rep: Reputation: 0
Grep a particular word in file


I have a file like


"42949539976057468996159178968072920078282731623874 CPN_01_POK_CAD 6457527 200 CAD 0 10 round sitout 16591 10385302 7 0 0 1"
"42949540001911141757145953742592175214550679440651 CPN_01_POK_CAD 6910167 200 CAD 0 10 round sitout 22399 10416922 4 0 0 1"
"CPN_01_POK_CAD Q1BOMQAB7ekAAAExslbgY9a9Kd8_ 8812105 42949539952993068120316222639213919055452543761306 3.14 CAD 27461 10532734 0.86 1 0 0 0 1"
"42949539982993260586129273371133791155450805273741 CPN_01_POK_CAD Q1BOMQABhycAAAExsi0UxH68ox8_ 8812278 2.5 0 0.1233333333 4 3 3 2.5 0 0 0 CAD 0 97.5 CAD 0 1138 10532317 0 0 1"
"CPN_01_POK_CAD Q1BOMQABhycAAAExsi0UxH68ox8_ 8812278 42949539982993214768129272629788109125953813023279 98.5 CAD 1138 10532317 1.5 2 0 0 0 1"
"42949539992993420895312187639212981354558175398436 CPN_01_POK_CAD Q1BOMQABx34AAAExsmGJ6rkLdCk_ 8812279 0.57 0.96 0.0249673841 8 6 13 0.16 0.41 0 0 CAD 3 4.39 CAD 0 17613 10532274 0 0 1"
"CPN_01_POK_CAD Q1BOMQAB7ekAAAExslbgY9a9Kd8_ 8812105 42949539952993485057316220436689681087896076270449 2.87 CAD 27461 10532734 1.13 1 0 0 0 1"
"CPN_01_POK_CAD Q1BOMQAB7ekAAAExslbgY9a9Kd8_ 8812105 42949539952993559124316225180820634980294188483527 2.83 CAD 27461 10532734 1.17 1 0 0

I want a output of field before the CAD match word.


like output is

200 CAD
200 CAD
3.14 CAD
 
Old 03-26-2015, 03:05 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
what have you tried so far?
 
Old 03-26-2015, 06:27 PM   #3
A Gallina
LQ Newbie
 
Registered: Apr 2004
Posts: 25

Rep: Reputation: 0
Is this closer to what you want

grep .....CAD file
 
Old 03-26-2015, 08:12 PM   #4
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
This is a little tricky as the fields you want occur at variable positions in the lines, as well as multiple occurrences of the " CAD " pattern in some lines. But you can exploit that sed is greedy.
It is unclear from your post whether the lines are single lines in you input file, or broken across multiple lines.
If single lines, I would suggest this:
Code:
sed 's/.* \(.* CAD\) .*/\1/g' <input_file>

Last edited by allend; 03-26-2015 at 08:15 PM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep Word and Date from file T-Dub116 Linux - Newbie 8 12-11-2013 11:15 PM
grep a word from a file thomas2004ch Linux - Software 11 03-28-2013 11:49 AM
finding a word by using grep in whole file system c2431993 Linux - Newbie 2 09-29-2010 06:38 PM
grep a word in a .tar.gz file prernabhagat Linux - General 1 12-07-2007 01:53 AM
Word count with grep DiagonalArg Linux - Software 3 02-13-2006 12:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 10:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration