Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-18-2012   -   Original Discussion by rbalaj16
rbalaj16's Unix or Linux Image
rbalaj16 rbalaj16 is offline
Registered User
 
Join Date: Oct 2011
Last Activity: 11 April 2013, 11:40 AM EDT
Posts: 29
Thanks: 23
Thanked 0 Times in 0 Posts
Java Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi,

I have got the below requirement. please suggest.

I have a file like,


Code:
Processing Item is: [08765]
/data/ing/cfg2/abc.txt
/data/ing/cfg3/bgc.txt
Processing Item is: [0975398]
/data/cmd/for2/ght.txt
/data/kernal/config.klgt.txt

I want to process the above file to get the output file like,


Code:
08765:/data/ing/cfg2/abc.txt
08765:/data/ing/cfg3/bgc.txt
0975398:/data/cmd/for2/ght.txt
0975398:/data/kernal/config.klgt.txt

i.e, here the pattern line is the one starts with "Processing Item is:" which has the string/number in square bracket at the end. i want to grep the string/number and put it in the next few lines until next pattern line starts. once the next pattern line comes, grep the number/string from that new pattern line and add it to the below lines until next pattern line is reached and so on..

Last edited by Scrutinizer; 06-18-2012 at 05:03 AM.. Reason: code tags
Sponsored Links
    #2  
Old Unix and Linux 06-18-2012   -   Original Discussion by rbalaj16
rangarasan's Unix or Linux Image
rangarasan rangarasan is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 11 December 2017, 4:20 AM EST
Location: Chennai, India
Posts: 509
Thanks: 16
Thanked 131 Times in 127 Posts
awk

Hi,

Try this one,

Code:
awk -F ':' '/Processing/{p=$2;gsub(/[\]\[ ]/,"",p);next;}{$0=p":"$0;}1' file

Cheers,
Ranga:-)

Last edited by rangarasan; 06-18-2012 at 04:35 AM..
Sponsored Links
    #3  
Old Unix and Linux 06-18-2012   -   Original Discussion by rbalaj16
rbalaj16's Unix or Linux Image
rbalaj16 rbalaj16 is offline
Registered User
 
Join Date: Oct 2011
Last Activity: 11 April 2013, 11:40 AM EDT
Posts: 29
Thanks: 23
Thanked 0 Times in 0 Posts
Ranga,
Thanks for quick response.
I am getting below syntax error. please check this error and suggest me.


Code:
awk: syntax error near line 1
awk: bailing out near line 1


Last edited by Scrutinizer; 06-18-2012 at 05:03 AM.. Reason: code tags
    #4  
Old Unix and Linux 06-18-2012   -   Original Discussion by rbalaj16
itkamaraj's Unix or Linux Image
itkamaraj itkamaraj is offline Forum Advisor  
Registered User
 
Join Date: Apr 2010
Last Activity: 5 November 2017, 9:59 PM EST
Posts: 3,148
Thanks: 45
Thanked 698 Times in 674 Posts
try this


Code:
 
awk -F"[][]" '{if($0~/Processing/){a=$2}else{print a":"$0}}' input.txt

The Following User Says Thank You to itkamaraj For This Useful Post:
rbalaj16 (06-18-2012)
Sponsored Links
    #5  
Old Unix and Linux 06-18-2012   -   Original Discussion by rbalaj16
rangarasan's Unix or Linux Image
rangarasan rangarasan is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 11 December 2017, 4:20 AM EST
Location: Chennai, India
Posts: 509
Thanks: 16
Thanked 131 Times in 127 Posts
use nawk instead of awk if you are using solaris.
The Following User Says Thank You to rangarasan For This Useful Post:
rbalaj16 (06-18-2012)
Sponsored Links
    #6  
Old Unix and Linux 06-18-2012   -   Original Discussion by rbalaj16
rbalaj16's Unix or Linux Image
rbalaj16 rbalaj16 is offline
Registered User
 
Join Date: Oct 2011
Last Activity: 11 April 2013, 11:40 AM EDT
Posts: 29
Thanks: 23
Thanked 0 Times in 0 Posts
MySQL

nawk is doing it correctly. Thanks Ranga.

command from Kamaraj also working except it is bringing ']' also in my output file.

Thanks to both.
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Awk script to match pattern till blank line justchill Shell Programming and Scripting 7 11-19-2010 04:33 AM
Grep multiple line pattern and output the lines pukars4u Shell Programming and Scripting 1 11-11-2009 07:44 PM
Print word 1 in line 1 and word 2 in line 2 if it matches a pattern bangaram Shell Programming and Scripting 7 08-31-2009 06:58 AM
replace only 1st word of a line if it comes in the subsequent lines at same postion. geeko Shell Programming and Scripting 9 06-03-2009 08:25 AM
Need to remove few characters from each line till a pattern is matched kiranlalka Shell Programming and Scripting 4 05-19-2009 04:31 AM



All times are GMT -4. The time now is 04:19 AM.