awk script to compare and insert a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script to compare and insert a line
# 8  
Old 11-24-2008
Hi
I have another question on the same file. I want to copy a field at position 9:15 on the line starting with 6(in red color) and replace it with the field at position 10:14 on line starting with 1(in blue)

1 test 1 765533 7643743
6 yes 3 5363653 373833
7 323323 4343434435 545
8 4345435435 5454545 55
7 323323 4343434435 545
1 test 1 765533 7643743
6 yes 3 5373653 373833
7 323323 4343434435 545
8 4345435435 5454545 55
6 yes 3 5373653 373833
7 323323 4343434435 545
8 4345435435 5454545 55
1 test 1 765533 7643743
6 yes 3 5383653 373833
7 323323 4343434435 545
8 4345435435 5454545 55


so the output will be


1 test 1 5363653 7643743
6 yes 3 5363653 373833
7 323323 4343434435 545
8 4345435435 5454545 55
7 323323 4343434435 545
1 test 1 5373653 7643743
6 yes 3 5373653 373833
7 323323 4343434435 545
8 4345435435 5454545 55
6 yes 3 5373653 373833
7 323323 4343434435 545
8 4345435435 5454545 55
1 test 1 5383653 7643743
6 yes 3 5383653 373833
7 323323 4343434435 545
8 4345435435 5454545 55
# 9  
Old 11-25-2008
Considering your example (assuming no @ in the data):

(use nawk or /usr/xpg4/bin/awk on Solaris)
Code:
awk '/^1 / { s = $0; next }
s { sub(/........./, "&@", s)         
    sub(/@....../, substr($0, 9, 7), s)
    $0 = s RS $0; s = 0
  }
1' infile

You can rewrite the code with re-interval and gensub if your awk implementation supports them.
If I knew sed, I would have used it in this case Smilie.
# 10  
Old 11-25-2008
A sed version:

Code:
sed '/^1 /{
N
s/\(.\{9\}\)\(.\{6\}\)\(.*\n\)\(.\{8\}\)\(.\{7\}\)/\1\5\3\4\5/
}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

5. Shell Programming and Scripting

awk to insert new line conditionally

Dear All, I have a file like: If $4=2001, a duplicated line will be inserted and $4 will be assigned value 2011; so that the new file would be: How to write awk to accomplish this? thank you very much! (5 Replies)
Discussion started by: littlewenwen
5 Replies

6. Shell Programming and Scripting

how to compare file line by line with awk

im a newbee to unix. I have a requirement to compare two files with awk. file1.txt a b c d e file2.txt a b d e here i want to compare each line in file1 with corresponding line in file2 and prinf the line with difference. ie to check required result as shown below a=a (dont... (3 Replies)
Discussion started by: kiranps
3 Replies

7. Shell Programming and Scripting

Shell script to count number of ~ from each line and compare with next line

Hi, I have created one shell script in which it will count number of "~" tilda charactors from each line of the file.But the problem is that i need to count each line count individually, that means. if line one contains 14 "~"s and line two contains 15 "~"s then it should give an error msg.each... (3 Replies)
Discussion started by: Ganesh Khandare
3 Replies

8. Shell Programming and Scripting

compare three files and insert a blank line at each mismatch

i need to compare three files in unix a.txt b.txt c.txt 1 2 1 2 5 3 4 6 5 5 6 6 i need to insert a blank line in the file if i don't find a match and put the items at the same column if found a match The items in the files... (4 Replies)
Discussion started by: mad_man12
4 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