Inserting a line before the line which matches the patter


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Inserting a line before the line which matches the patter
# 1  
Old 12-18-2008
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||
# 2  
Old 12-18-2008
Code:
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.

Last edited by zaxxon; 12-18-2008 at 05:23 AM..
# 3  
Old 12-18-2008
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||
# 4  
Old 12-18-2008
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 Smilie The fix is in my former post.
# 5  
Old 12-18-2008
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
# 6  
Old 12-18-2008
I am using gawk. If you are on Solaris, maybe use another awk:
/usr/bin/nawk or /usr/xpg4/bin/awk
# 7  
Old 12-18-2008
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed for appending a line before a pattern and after another patter.

Hi All, I'm appending a line before a pattern and after another pattern(;) but omitting the first pattern - same as if comes duplicates before the second pattern. Also, I'm adding a word before the first pattern - Here is my file select blah blah hello select blah blah ; select... (6 Replies)
Discussion started by: Mannu2525
6 Replies

2. Linux

Print line 1 if line 3 matches of the output

Hi I want to extend following command so that on the basis of "Branch: ****" on the third line I can grep and print name of the file on the first line. cat .labellog.emd | grep DA2458A7962276A7E040E50A0DC06459 | cut -d " " -f2 | grep -v branch_name | xargs -I file <command to describe> file ... (1 Reply)
Discussion started by: ezee
1 Replies

3. Shell Programming and Scripting

awk to print the line that matches and the next if line is wrapped

I have a file and when I match the word "initiators" in the first column I need to be able to print the rest of the columns in that row. This is fine for the most part but on occasion the "initiators" line gets wrapped to the next line. Here is a sample of the file. caw-enabled ... (3 Replies)
Discussion started by: kieranfoley
3 Replies

4. Shell Programming and Scripting

Using regex's from file1, print line and line after matches in file2

Good day, I have a list of regular expressions in file1. For each match in file2, print the containing line and the line after. file1: file2: Output: I can match a regex and print the line and line after awk '{lines = $0} /Macrosiphum_rosae/ {print lines ; print lines } ' ... (1 Reply)
Discussion started by: pathunkathunk
1 Replies

5. Shell Programming and Scripting

Commenting a specific line and inserting a new line after commented line.

Hello All, I have following file contents cat file #line=aaaaaa #line=bbbbbb #line=cccccc #line=dddddd line=eeeeee #comment=11111 #comment=22222 #comment=33333 #comment=44444 comment=55555 Testing script Good Luck! I would like to comment line line=eeeeee and insert a new line... (19 Replies)
Discussion started by: manishdivs
19 Replies

6. UNIX for Dummies Questions & Answers

print line that matches and next line, too

I'm using sh on hp-ux and want to find / print a line that matches 132.101- and the next line, too. grep -A isn't supported on hp-ux, so I'm trying awk and sed. The code below works but only prints the first occurence. I need all matches from the file. awk... (2 Replies)
Discussion started by: Scottie1954
2 Replies

7. Shell Programming and Scripting

How to substitute a line that matches an expression with another line

I need some help. I have a file (all.txt) whereby I want to substitute using sed/awk all lines that matches an expression with another line with different expression i.e subtitute expression, database_id: filename; WITH database_id: PY; There are many occurrences of the expression... (4 Replies)
Discussion started by: aimsoft
4 Replies

8. Shell Programming and Scripting

Inserting a line in a file after every alternate line

Friends , I have a large file and i need to insert a line after every line.I am actually unaware how to do it.Any help appreciated. My File control station *ATM* , qread $OSS.Jul13A.FI01 interval 1 intcount 1 control station *ATM* , qread $OSS.Jul13A.FI02 interval 1 intcount... (4 Replies)
Discussion started by: appu2176
4 Replies

9. Shell Programming and Scripting

Sed or Grep to delete line containing patter plus extra line

I'm new to using sed and grep commands, but have found them extremely useful. However I am having a hard time figuring this one out: Delete every line containing the word CEN and the next line as well. ie. test.txt blue 324 CEN green red blue 324 CEN green red blue to produce:... (2 Replies)
Discussion started by: rocketman88
2 Replies

10. Shell Programming and Scripting

grep the line only if next line matches

Hi I have an Input of following sort AAAA: ProgName="PROGRAM" BBBB: ProgName="BBBBBB" CCCC: DDDD: ProgName="PROGRAM" SSSS: ProgName="PROGRAM" ZZZZ: ProgName="PROGRAM" I want to find the Lines which are followed by ProgName="PROGRAM" Out Put AAAA: (11 Replies)
Discussion started by: pbsrinivas
11 Replies
Login or Register to Ask a Question