awk query


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk query
# 1  
Old 03-11-2014
awk query

Hi,
I have a sample file in the following format.

Code:
000013560240|000013560240|001|P|155|99396|0||SS00325665|
000013560240|000013560240|002|P|17|99000|0||SS00325665|
000013560240|000013560240|002|F|-17|99000|0|R|SS00325665|
000013560240|000013560240|003|P|20|82270|0||SS00325665|
000013560240|000013560240|004|F|17|99000|0||SS00325665|
000012817460|000012817460|001|P|129.98|V2202|0||SS00425539|
000012817460|000012817460|001|F|-129.98|V2202|0|R|SS00425539|
000016132850|000016132850|001|P|59.99|E0607|0||SS00154667|
000016132850|000016132850|001|F|-59.99|E0607|0|R|SS00154667|
000016132850|000016132850|002|P|24|E1399|0||SS00154667|
000016132850|000016132850|002|F|-24|E1399|0|R|SS00154667|
000016132850|000016132850|003|F|59.99|E0607|0||SS00154667|
000016132850|000016132850|004|F|24|E1399|0||SS00154667|

I need the output file as below :

Code:
000013560240|000013560240|001|P|155|99396|0||SS00325665|
000013560240|000013560240|003|P|20|82270|0||SS00325665|
000013560240|000013560240|004|F|17|99000|0||SS00325665|
000016132850|000016132850|003|F|59.99|E0607|0||SS00154667|
000016132850|000016132850|004|F|24|E1399|0||SS00154667|

The logic is tom compare fields 3 and 5 and if field 5 has corresponding negative value which matched to the positive value based on field 3 , those records should be ignored.

This can be done in Perl using arrays but was wondering if this can be achieved through awk or sed or a shell script. Any help is appreciated.
# 2  
Old 03-11-2014
The logic seems to be more than just comparing fields 3 and 5 in previous lines. Column one also seems important as well as column 4. The requirements are not clear,
# 3  
Old 03-11-2014
Try (reading file twice, original order)
Code:
awk -F\| 'NR==FNR{if(($3,-$5) in A) delete A[$3,-$5]; else A[$3,$5]; next} ($3,$5) in A' file file

The input file is specified twice..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 03-12-2014
As long as the lines with matching field 3 values are on adjacent lines (as they are in the sample input), there is no need to read the input file twice:
Code:
awk -F '|' '
save {	if($3 == last3 && $5 == -last5) {
		save = 0
		next
	}
}
{	if(save) print last0
	save = 1
	last0 = $0
	last3 = $3
	last5 = $5
}
END {	if(save) print last0
}' sample

produces the desired output only reading the input once.

If you want to try this on a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of the default /usr/bin/awk.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Query on awk

Hello, I am trying to find the RAM usage for one of the process from a group of servers. xx-Process name The command i used by logging to the individual server is as below ps aux | grep xx | awk '{sum=sum+$6}; END {print sum/1024 " MB"}' And the result is 0.824219 MB This... (4 Replies)
Discussion started by: nextStep
4 Replies

2. UNIX for Dummies Questions & Answers

Awk - query about arrays

Hello again, I have example config file with two arrays: tab1="name1 surname1" tab1="name2 surname2" tab1="name3 surname3" tab2="First" tab2="Second"and csv file: "aaaaa","surname1","name1","ddddd,eeeee","ffffff","ggggg","3","2012/02/22 12:25:21","2012/02/22... (7 Replies)
Discussion started by: haczyk
7 Replies

3. Shell Programming and Scripting

awk query

Dear all, I find a shell script about awk command in the forum regarding "Find and replace duplicate column values in a row" In this thread, there is a command line as shown below to replace duplicate awk -F, '{for (i=1;i<=NF;i++){if (v++){b=$i;$i=""}};print $0} END { print "dups are"... (1 Reply)
Discussion started by: eldonlck
1 Replies

4. Shell Programming and Scripting

AWK Query

i have a log file where in i have 3 columns a 11 test1 b 22 test2 in a script, i want to pass first column as an argument to awk so that i can get the second and third column respectively Example : If i pass ($1 i.e 'a' to an awk, the result should be 11 test1) i tried different awk... (2 Replies)
Discussion started by: vkca
2 Replies

5. Shell Programming and Scripting

sed or awk query

I have a set of multiple files out of which one i am showing below : A0120110124102329 BED. B01***DETALLADA 43984 The BO1 is the starting pattern of the second line. In the 2nd line in position 30,31,32 i need to replace 111, instead of 3... (2 Replies)
Discussion started by: dazdseg
2 Replies

6. Solaris

awk output query

Hi Everyone, When i issue command like ls -l | awk '/udtts/ {print $9}' =============================== I am getting output as udttsGEHLNAR.6864 udttsGEHLNAR.7921 udttsNARALAX.15415 udttsNARALAX.18016 But I want output after dot i.e like 6864 7921 15415 18016 (3 Replies)
Discussion started by: gehlnar
3 Replies

7. Shell Programming and Scripting

Awk Gsub Query

Hi, Can some one please explain the following line please throw some light on the ones marked in red awk '{print $9}' ${FTP_LOG} | awk -v start=${START_DATE} 'BEGIN { FS = "." } { old_line1=$0; gsub(/\-/,""); if ( $3 >= start ) print old_line1 }' | awk -v end=${END_DATE} 'BEGIN { FS="." } {... (3 Replies)
Discussion started by: crosairs
3 Replies

8. UNIX for Dummies Questions & Answers

some query in awk

Hi , I want to assign a value to variable which will have size of the file that is we have following files for eg: ls -ltr -rw-rw-r-- 1 dsadmin dstage 34 Oct 29 12:14 some.txt -rw-rw-r-- 1 dsadmin dstage 0 Oct 29 14:52 eg.txt -rwxrwxr-x 1 dsadmin dstage 1453 Oct... (2 Replies)
Discussion started by: Amey Joshi
2 Replies

9. UNIX for Dummies Questions & Answers

Query in awk

file "batsmen" contains Bradman;Australia;52;6996 Gavaskar;India;124;10122 Sobers;West;Indies;93;8032 Border;Australia;109;8075 Miandad;Pakistan;97;7121 Gower;England;94;7023 Boycott;England;108;8114 My awk script contains awk '{FS=";"} {len=length($1)}; ... (1 Reply)
Discussion started by: raguramtgr
1 Replies

10. UNIX for Dummies Questions & Answers

Query In awk

Is it possible to have a pattern as RS in awk. For Example pl. go through the statement; " Account Serial Number: 88888888 TT00X000000XXXXXXXXXXXXX SS00X000000XX.000,XXXXXXXXXXXXXXXXXX0000XXXXXXX0000000000 WW00X0000000XX000000000000MMMMMMM MMMMMMM0000AA11110000000000000000000000000... (1 Reply)
Discussion started by: raguramtgr
1 Replies
Login or Register to Ask a Question