The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
extraction of perfect text from file. nua7 Shell Programming and Scripting 5 06-20-2008 05:14 AM
Shell script for text extraction from a file vignesh53 Shell Programming and Scripting 3 02-05-2008 05:16 AM
help with data extraction script mam Shell Programming and Scripting 11 01-16-2008 03:50 AM
Text File Pattern Mattching barney34 Shell Programming and Scripting 9 10-25-2006 05:49 AM
Data Extraction issue. irehman Shell Programming and Scripting 5 04-06-2006 05:28 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-22-2008
Registered User
 

Join Date: Sep 2008
Posts: 2
extraction of data from a text file which follows certain pattern

hi everybody,

i have a file, in it I need to extract some data that follows a particular pattern..

For example: my file contains like

now running Speak225

sep 22 mon 16:34:05 2008

--------------------------------
--------------------------------

now running BBp499

sep 22 mon 16:36:15 2008
------------------------------
----------------------------

I need to get the names which follows "now running" and times in the precceding lines...

for that i need to get the line numbers and extract the required information..
Can anyone please help me.. That pattern exists so many times in the file and i need to get for all the times it happens using shell scripting .thank you
Reply With Quote
Forum Sponsor
  #2  
Old 09-22-2008
Registered User
 

Join Date: Aug 2008
Posts: 82
Code:
cat File.txt
now running Speak225
sep 22 mon 16:34:05 2008
sep 22 mon 16:34:05 2008
sep 22 mon 16:34:05 2008
now running BBp499
sep 22 mon 16:36:15 2008
sep 22 mon 16:34:05 2008
sep 22 mon 16:36:15 2008
 
grep 'now running' File.txt | awk '{print $3; cnt++;} END {print "No occurance is " cnt;}'
Reply With Quote
  #3  
Old 09-22-2008
joeyg's Avatar
Moderator
 

Join Date: Dec 2007
Location: Home of world champion Boston Celtics
Posts: 983
Wink A different approach

Code:
> cat file2
now running Speak225
sep 22 mon 16:34:05 2008
sep 22 mon 16:34:05 2008
sep 22 mon 16:34:05 2008
now running BBp499
sep 22 mon 16:36:15 2008
sep 22 mon 16:34:05 2008
sep 22 mon 16:36:15 2008

> cat file2 | sed "s/now running/~$2/g" | tr "\n" " " | tr "~" "\n" | cut -d" " -f2,6

Speak225 16:34:05
BBp499 16:36:15
Reply With Quote
  #4  
Old 09-22-2008
Registered User
 

Join Date: Sep 2008
Posts: 2
cat file2 | sed "s/now running/~$2/g" | tr "\n" " " | tr "~" "\n" | cut -d" " -f2,6

Thank you so much for your response and the solution..

It worked fine..

But i forgot to specify in my question that the names may contain two or three words separated by spaces between them..

Like

now running Speak225 freemin

sep 22 mon 16:34:05 2008

--------------------------------
--------------------------------

now running BBp499 freesms local

sep 22 mon 16:36:15 2008
------------------------------
----------------------------


If you dont mind, Please help me in getting those names i.e "Speak225 freemin" , "BBp499 freesms local" and their respective timings...

That means i need to get all the words in the line after "now running" till the end of that line and the timings from the next line

please...

thank you once again for your response..

If possible please tell me what is "-f2"..

Last edited by mohkris; 09-22-2008 at 06:48 PM.
Reply With Quote
  #5  
Old 09-23-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/fl
Posts: 1,061
You can also do what you want to do entirely within sed using back references ....
Code:
$ sed 's/now running \(.*\)/\1/;N; s/\n/ /; s/\(^.* \)\(... .. ... \)\(..:..:..\).*/\1\3/' file
Speak225 freemin 16:34:05
BBp499 freesms local 16:36:15
$
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:15 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0