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
# 1  
Old 11-21-2008
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 323323 4343434435 545
8 4345435435 5454545 55
7 323323 4343434435 545
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
6 yes 3 5383653 373833
7 323323 4343434435 545
8 4345435435 5454545 55

so the output should be

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
# 2  
Old 11-22-2008
can you please explain bit more..like what are you searching for and from which to which line nos.... i think i can help you.......

thnx shishir
# 3  
Old 11-22-2008
Given your sample data it seams that $4 will work fine.
Use nawk or /usr/xpg4/bin/awk on Solaris:

Code:
awk '/^1 / { s = $0 }
/^6 / && !t[$4]++ {
  c++ && $0 = s RS $0
  }1' infile

# 4  
Old 11-23-2008
perl:
Code:
open FH,"<d:/a.txt";
while(<FH>){
	if(m/^1.*/){
		$line=$_;
		print;
		next;
	}
	if(m/^6.*/){
		@t=split;
		if($temp eq ""){
			$temp=$t[3];
			print;
		}
		else{
			if(!($t[3] eq $temp)){
				print $line;
				print $_;
				$temp=$t[3];
			}	
			else{
				print;
			}		
		}
	}
	else{print;}
}

# 5  
Old 11-23-2008
hi
The field is at fixed position 10-20 not the 4th field necessarily. Could you please help

Thanks
# 6  
Old 11-24-2008
But you put in red the positions 9-15, not 10-20!

With 9-15 it should be:

Code:
awk '/^1 / { s = $0 }
/^6 / && !t[substr($0,9,7)]++ {
  c++ && $0 = s RS $0
  }1' infile

If you really want 10-20, you should modify the arguments passed to substr.
# 7  
Old 11-24-2008
That works, Thanks very much.
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