![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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;}'
|
|
#3
|
||||
|
||||
|
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 |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
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 $ |
|||
| Google The UNIX and Linux Forums |