Inserting a line before the line which matches the patter

Hi

Is there any command where we can insert a line "2|||" before every line starting with "3|"

my input is as follows

1|ETG|12345
3|79.58|||GBP||
1|ETG|12345
3|79.58|||GBP||
1|ETG|12345
2|EN_GB||Electrogalvanize 0.5 m2 ( Renault )
1|ETG|12345
3|88.51|||GBP||

desired output is
1|ETG|12345
2|||
3|79.58|||GBP||
1|ETG|12345
2|||
3|79.58|||GBP||
1|ETG|12345
2|EN_GB||Electrogalvanize 0.5 m2 ( Renault )
1|ETG|12345
2|||
3|88.51|||GBP||

awk '/^1/ {print ; getline; if ( /^3/ ) {print "2|||\n"$0} else {print}  }' infile
1|ETG|12345
2|||
3|79.58|||GBP||
1|ETG|12345
2|||
3|79.58|||GBP||
1|ETG|12345
2|EN_GB||Electrogalvanize 0.5 m2 ( Renault )
1|ETG|12345
2|||
3|88.51|||GBP||

Ok, here is the fix - the already existing ^2 is displayed now too.

Hi
the command which u gave is inserting the line 2||| after all the line starting with 1
but i need to insert this only inbetwen thoes lines where after line 1 line 3 is comming.

1|ETG|12345
2|||
3|79.58|||GBP||
1|ETG|12345
2|||
3|79.58|||GBP||
1|ETG|12345
2|||
2|EN_GB||Electrogalvanize 0.5 m2 ( Renault )
1|ETG|12345
2|||
3|88.51|||GBP||

Yep sorry, I noticed when I posted. I put a note to my post that I will fix it but I guess it didn't reach you in time :wink: The fix is in my former post.

Hi

Thanks a lot for the reply..
but unfortunately i am not geting the desider out put by executing the above command

I am getting the below error

/export/home/dstage/>awk '/^1/ {print ; getline; if ( /^3/ ) {print "2|||\n"$0} else {print} }' temp.dat
awk: syntax error near line 1
awk: illegal statement near line 1
awk: bailing out near line 1

Please look into this

Thanks

I am using gawk. If you are on Solaris, maybe use another awk:
/usr/bin/nawk or /usr/xpg4/bin/awk

this command worked

but in the below senario
1|ETG|63121387883|Alternate|Y
3|79.58|||GBP||
4|001137001
4|0011372
5|1021801
5|1021901
1|ETG|63121387884|Alternate|Y
3|79.58|||GBP||
4|001137001
5|1021732
5|1021802
1|ETG|63128363077|Alternate|Y
2|EN_GB||Electrogalvanize 0.5 m2 ( Renault )
2|FR_GB||Electrogalvanize 0.5 m2 ( Renault )
3|88.51|||GBP||
4|001137001
4|001137002
5|1021701
5|1021901
5|7040500
1|ETG|63128363078|Alternate|Y
3|88.51|||GBP||
4|001137001
5|1021702

when tried to execute the awk command the out is as below

1|ETG|63121387883|Alternate|Y
2|||
3|79.58|||GBP||
1|ETG|63121387884|Alternate|Y
2|||
3|79.58|||GBP||
1|ETG|63128363077|Alternate|Y
2|EN_GB||Electrogalvanize 0.5 m2 ( Renault )
1|ETG|63128363078|Alternate|Y
2|||
3|88.51|||GBP||

All the other records are being lost but thoes records should also be there

It cant work in this scenario since you didn't tell about this requirement before :rolleyes:. Maybe post more info sooner since you had it already.
And by the way, use code tags to display your code and logs etc. eye friendly, thank you.

Oh.. I am sorry. i would take care to use code tags when i post the code next time

awk '/^1/ {print;getline; if ( /^3/ ) {print "2|||\n"$0} else {print}} /^[^123]/ {print} ' infile
1|ETG|63121387883|Alternate|Y
2|||
3|79.58|||GBP||
4|001137001
4|0011372
5|1021801
5|1021901
1|ETG|63121387884|Alternate|Y
2|||
3|79.58|||GBP||
4|001137001
5|1021732
5|1021802
1|ETG|63128363077|Alternate|Y
2|EN_GB||Electrogalvanize 0.5 m2 ( Renault )
4|001137001
4|001137002
5|1021701
5|1021901
5|7040500
1|ETG|63128363078|Alternate|Y
2|||
3|88.51|||GBP||
4|001137001
5|1021702

thanks a lot its working