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,

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,

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..

Hi,

Try this one,

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

Cheers,
Ranga:-)

Ranga,
Thanks for quick response.
I am getting below syntax error. please check this error and suggest me.

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

try this

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

use nawk instead of awk if you are using solaris.

1 Like

nawk is doing it correctly. Thanks Ranga.

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

Thanks to both.