Home Man
Search
Today's Posts
Register

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

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 06-18-2012
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 04:03 AM.. Reason: code tags
# 2  
Old 06-18-2012
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 03:35 AM..
# 3  
Old 06-18-2012
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 04:03 AM.. Reason: code tags
# 4  
Old 06-18-2012
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)
# 5  
Old 06-18-2012
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)
# 6  
Old 06-18-2012
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.
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk to combine lines from line with pattern match to a line that ends in a pattern Wes Kem Shell Programming and Scripting 5 02-23-2016 07:11 PM
Match Pattern and print pattern and multiple lines into one line tigerhills Shell Programming and Scripting 4 01-11-2015 09:26 AM
Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern essem Shell Programming and Scripting 11 06-05-2014 04:59 AM
Awk script to match pattern till blank line justchill Shell Programming and Scripting 7 11-19-2010 03:33 AM
Grep multiple line pattern and output the lines pukars4u Shell Programming and Scripting 1 11-11-2009 06: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 05: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 07:25 AM
Need to remove few characters from each line till a pattern is matched kiranlalka Shell Programming and Scripting 4 05-19-2009 03:31 AM
Urgent Help required : awk/sed help to find pattern and delete till end of line rajan_san UNIX for Advanced & Expert Users 1 08-25-2008 05:18 PM
Urgent! Sed/Awk Filter Find Pattern Delete Till End Of Line rajan_san Shell Programming and Scripting 2 08-25-2008 04:53 PM


All times are GMT -4. The time now is 06:37 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password