AWK Compare previous value with current. | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

AWK Compare previous value with current.

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-30-2011
dikesm dikesm is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 7 August 2013, 6:44 AM EDT
Posts: 9
Thanks: 8
Thanked 0 Times in 0 Posts
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
Moderator's Comments:
Please use code tags when posting data and code samples!

Last edited by vgersh99; 03-30-2011 at 11:13 AM.. Reason: code tags, please!
Sponsored Links
    #2  
Old 03-30-2011
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 4 April 2014, 4:57 PM EDT
Location: Southern NJ, USA (Nord)
Posts: 4,359
Thanks: 8
Thanked 534 Times in 513 Posts
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  
Old 03-30-2011
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Last Activity: 22 April 2014, 11:31 AM EDT
Location: Out running a Marathon.
Posts: 2,309
Thanks: 66
Thanked 150 Times in 140 Posts
Question

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  
Old 03-30-2011
rdcwayx rdcwayx is offline Forum Advisor  
Use nawk in Solaris
 
Join Date: Jun 2006
Last Activity: 22 March 2014, 12:27 PM EDT
Posts: 2,759
Thanks: 44
Thanked 418 Times in 406 Posts
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,6

For 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
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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



All times are GMT -4. The time now is 07:29 AM.