|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
AWK Compare previous value with current.
Hi, I have one small doubt how to go ahead and process the below requirement. File Content Code:
1,abc,10 2,xyz,11 3,pqr,12 4,pqr,13 5,pqr,14 Output file expected: Code:
1,mnq,1 1,ddd,2 1,qqq,3 1,sss,4 1,ddd,5 1,eee,6 1,fff,7 1,ddr,8 1,rrd,9 1,der,10 2,dwe,11 3,pqr,12 4,pqr,12 5,pqr,12 12,adhe,0 13,add,1 . . . . . . . thousands of records Note that the pqr is repeated 3 times in this case for the 3rd column we have the the 3rd column value of the 1st occurence of pqr copied for the 2nd and 3 occurenct i.e 4,pqr,13 gets changed to 4,pqr,12 and 5,pqr,14 gets changed to 5,pqr,12 Once the output file is generated then then i want something like Code:
cat 1 >10 cat 2>11 cat 3 4 5 > 12 I want this to be done dynamically since the number of records might be in thousands atleast Would appreciate if we can an awk solution for the same. Regards, Dikesh Shah
Last edited by vgersh99; 03-30-2011 at 11:13 AM.. Reason: code tags, please! |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
Well, fields 1 and 2 are preserved, save fields of each record out for interrogation with the next, and if last matches for 1 and 2, discard 3 and use old 3.
|
| The Following User Says Thank You to DGPickett For This Useful Post: | ||
dikesm (04-07-2011) | ||
| Sponsored Links | ||
|
|
#3
|
||||
|
||||
|
How come your file-in and file-out do not match?
Your sample file has five sample lines, but three of them do not show up in your output. But, there are many lines in the output that are not in the input. Please clarify. |
| The Following User Says Thank You to joeyg For This Useful Post: | ||
dikesm (04-07-2011) | ||
|
#4
|
|||
|
|||
|
Init the input file: Code:
$ cat infile 1,mnq,1 1,ddd,2 1,ddd,3 1,sss,4 1,ddd,5 1,add,6 1,fff,7 1,ddr,8 1,rrd,9 1,der,10 2,mnq,11 3,pqr,12 4,pqr,13 5,pqr,14 12,adhe,0 13,add,1 Get the output: Code:
$ awk -F , '{if (!a[$2]) {a[$2]=$3} else {$3=a[$2]}}1' OFS="," infile
1,mnq,1
1,ddd,2
1,ddd,2
1,sss,4
1,ddd,2
1,add,6
1,fff,7
1,ddr,8
1,rrd,9
1,der,10
2,mnq,1
3,pqr,12
4,pqr,12
5,pqr,12
12,adhe,0
13,add,6For your second request, I guess you need export to different files depend on the number on column 3. Code:
awk -F , '{if (!a[$2]) {a[$2]=$3} else {$3=a[$2]}}1' OFS="," infile |awk -F , '{print >$3 ".txt"}'after that, you will get file 1.txt, 12.txt, etc. |
| The Following User Says Thank You to rdcwayx For This Useful Post: | ||
dikesm (04-07-2011) | ||
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Awk to print data from current and previous line | awk_noob_456 | UNIX for Dummies Questions & Answers | 2 | 03-28-2011 09:37 PM |
| Compare two files using awk or sed, add values in a column if their previous fields are same | yerruhari | Shell Programming and Scripting | 3 | 11-08-2009 09:53 PM |
| How to use sed to search for string and Print previous two lines and current line | nmadhuhb | Shell Programming and Scripting | 1 | 07-30-2009 08:35 AM |
| to write a script to compare the file size in the current directory and previous dir | tweety | Shell Programming and Scripting | 5 | 02-10-2009 02:24 AM |
| Print previous, current and next line using sed | ysrinu | Shell Programming and Scripting | 8 | 12-08-2008 03:05 PM |
|
|