awk to insert new line conditionally


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to insert new line conditionally
# 1  
Old 01-31-2013
awk to insert new line conditionally

Dear All,

I have a file like:

Quote:
1 1 1 2011
2 2 2 2001
3 3 3 2011
4 4 4 2001
5 5 5 2011
If $4=2001, a duplicated line will be inserted and $4 will be assigned value 2011; so that the new file would be:

Quote:
1 1 1 2011
2 2 2 2001
2 2 2 2011
3 3 3 2011
4 4 4 2001
4 4 4 2011
5 5 5 2011
How to write awk to accomplish this? thank you very much!
# 2  
Old 01-31-2013
try:
Code:
awk '1;$4==2001 {$4=2011; print}' infile

or
Code:
awk '1;$4==x {$4=y; print }' x=2001 y=2011 infile

or
Code:
sed -n 'p;/2001$/s/2001$/2011/p' infile


Last edited by rdrtx1; 01-31-2013 at 05:50 PM.. Reason: fixed. (copied from wrong buffer)
This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 01-31-2013
Code:
awk '$4==2001{d=$0;sub("2001","2011",d);print $0;print d;}$4!=2001{print}' filename

This User Gave Thanks to Yoda For This Post:
# 4  
Old 01-31-2013
Thank you very much.
By the way what does "1" means in
awk '1;.......}' infile
# 5  
Old 01-31-2013
1 = true = print (in awk)
This User Gave Thanks to rdrtx1 For This Post:
# 6  
Old 01-31-2013
Code:
awk '1;$4==2001 && $4=2011'  myFile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to conditionally display and remove first line only?

I have a maildir hierarchy of 90k eml files and; 1) I would like to walk the tree and display the first line from any file, whose first line begins with; From - That's "From space dash space" and only if it's the first seven characters, of the first line in the file. 2) I would also... (12 Replies)
Discussion started by: jasn
12 Replies

2. Shell Programming and Scripting

Conditionally add character at end of line

Hi, I would like have a shell script to check every line in a file to see if it ends with ";". If this is NOT the last character ";" should be added. MyFile.csv : web9331801;01/01/2014 23:39:35;;"93962";353150256; web9331802;01/01/2014 23:44:29;;"479288";353153538; web9331803;01/01/2014... (14 Replies)
Discussion started by: vg77
14 Replies

3. Shell Programming and Scripting

Insert a value in a pipe delimited line (unsig sed,awk)

Hi, I want to insert a value (x) in the 3rd position of each line in a file like below a|b|c|d|1 a|b|c|d a|b|c|d|e|1 a|b|cso that output file looks like a|b|x|c|d|1 a|b|x|c|d a|b|x|c|d|e|1 a|b|x|cI can do that using perl as below #!/usr/bin/perl -w use strict; #inserting x at... (5 Replies)
Discussion started by: sam05121988
5 Replies

4. Shell Programming and Scripting

sed and awk to insert a line after a para

hi I am having a file like this ############################## mod1 ( a(ll) , b( c), try(o) , oll(ll) go(oo) , al(ll) mm(al) , lpo(kka) kka(oop) ); mod2 ( jj(ll) , c( kk), try1q(o1) , ofll(lll) gao(oo1) , ala(llaa) mmf(adl) , lddpo(kkad) kkda(oodp) );... (20 Replies)
Discussion started by: kshitij
20 Replies

5. Shell Programming and Scripting

Awk, if line after string does not match insert

I have a large file with interface records. I need to check every record that has the string "encapsulation bridge1483" and if the next line after this does not have "ip description" then I need to insert a line to add "ip description blah_blah_blah. Sample file: interface atm 1/0.190158... (3 Replies)
Discussion started by: numele
3 Replies

6. Shell Programming and Scripting

awk to insert line previous to a pattern?

I have a very long line with certain patters embedded in there. I need to be able to read that line, and when it encounters that pattern, create a new line. I want the pattern to be the beginning of the new line. I thought sed or awk could do this, but everything I try in sed gives me a "sed... (2 Replies)
Discussion started by: Drenhead
2 Replies

7. Shell Programming and Scripting

Add text at the end of line conditionally

Hi All, I have a file as below: cat myfile abcdef NA rwer tyujkl na I wish to add the text ".txt" at the end of all lines except the lines starting with NA or na. I know i can add text at the end of line using following command but I am not sure how to valiate the condition. (14 Replies)
Discussion started by: angshuman
14 Replies

8. Shell Programming and Scripting

awk script to compare and insert a line

Hi I want to compare a string at fixed position 10-20 for all the lines starting with 6. if they dont match it should take a copy of a line starting with 1 and insert it before the line starting with 6. How do i this? Please help Eg 1 test 1 765533 7643743 6 yes 3 5363653 373833 7... (9 Replies)
Discussion started by: appsguy616
9 Replies

9. Shell Programming and Scripting

sed/awk to insert comment at defined line number

Hi there, may someone easily help me on this : I want to insert a text in a specific line number like : linenumb2start=`cat memory_map.dld | nl -ba | egrep -i "label" | cut -f1` line2insert=`expr $linenumb2start + 2` and now I need to replace something like {} with {comment} at... (8 Replies)
Discussion started by: homefp
8 Replies

10. Shell Programming and Scripting

awk insert character in the middle of a line

I'm trying to insert a single character at position 11 in everyline of a file. My input file looks like this: 456781 ~Y~12345 456782 ~N~12300 and I want my output to look like this: 45678~1 ~Y~12345 45678~2 ~N~12300 I tried the following awk code, but it's not working:... (3 Replies)
Discussion started by: mmarino
3 Replies
Login or Register to Ask a Question