A complex sed statement

I have following requirement.
Say, my text file contains following patterns

{2010501005|XXGpvertex|9|0|17|0|{|{30100001|XXparameter_set|@@@@{{30001002|XXparameter|!prototype_path|$AB_COMPONENTS/Sort/Sort.mpc|3|2|Pf$|@{0|}}

}}@0|@315000|78500|335000|99000|114000|87000|17|And the Sort|Ab Initio Software|Built-in|1|10|0||6||32769|1|{1|0|}}}

Now, here I have to search for $AB_COMPONENTS, and pick the word which is after 2nd forward slash ("/") and before the period("."), i.e is SORT here.And store it in a file.
After this we have to take out words between 8th and 9th "|" in the immediate line. i.e. "And the Sort" here. And store it in a file.
Likewise we have to search the complete file.

Are the above posted lines are in count 2 or in a single line..? Also pls post the desired output format.

There are two lines...

The output shoould be

Sort
And the sort

try this,

 awk -F"|" '
{if(first==1){print $9;first=0;next}for(i=1;i<=NF;i++){
if ($i~/\$AB_COMPONENTS/) {
split($i,a,"/");print substr(a[3],1,(index(a[3],"."))-1);first=1;
}}}' inputfile
1 Like

Thanx a tonne Pravin27...really thanks...its working fine...but much complex to understand :(..thanks a lot..

If the file contains blank lines between both lines:

awk -F"|" '/\$AB_COMPONENTS/{ print gensub(/.*\$AB_COMPONENTS\/.*\/(.*)\..*/,"\\1","");
getline; getline; print $9}' input
Sort
And the sort

If the file doesn't contain blank lines between lines:

awk -F"|" '/\$AB_COMPONENTS/{ print gensub(/.*\$AB_COMPONENTS\/.*\/(.*)\..*/,"\\1","");
getline; print $9}' input
Sort
And the sort

Regards

1 Like

Through sed.. looks complex though

sed '/AB_COMPONENTS/s/.*\/\(.*\)\.mpc.*/\1/;n;s/^[^|]*|[^|]*|[^|]*|[^|]*|[^|]*|[^|]*|[^|]*[^|]*|[^|]*|\([^|]*\)|.*/\1/' inputfile >outfile

Pravin,

what does (first==1){print $9;first=0;next} and i<=NF(for(i=1;i<=NF;i++)) means ??

---------- Post updated at 03:36 PM ---------- Previous update was at 03:29 PM ----------

Pravin,,

what does if(first==1){print $9;first=0;next} for(i=1;i<=NF;i++) do...please if you can help me thru this...

I have tried my best to explain .

first

is a variable name. here i am using as flag.

NF
  • No. of fields in a record.
{if(first==1){print $9;first=0;next}

#variable first is not having value initially so if condition will not satisfiled

for(i=1;i<=NF;i++){

#this is for loop which will go throgh record field by field.

if ($i~/\$AB_COMPONENTS/) {

#if any filed is match with pattern $AB_COMPONENTS then

split($i,a,"/");
\#split that filed \(in your case $AB_COMPONENTS/Sort/Sort.mpc\) with delimiter as "/" and fill the array 'a'
print substr(a[3],1,(index(a[3],"."))-1) 

here i am taking third element from array 'a'(i.e. Sort.mpc) and extracting string before . using index and substr.

first=1;

assigning 1 to variable first. because we found the pattern and perform operation as requested.

Now while reading your second line.

{if(first==1){print $9;first=0;next} 

. Variable first is 1. if condition is satisfied and we can take field number 9 from second line and make first varibale zero.

1 Like