AWK Compare previous value with current.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK Compare previous value with current.
# 1  
Old 03-30-2011
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:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 03-30-2011 at 12:13 PM.. Reason: code tags, please!
# 2  
Old 03-30-2011
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.
This User Gave Thanks to DGPickett For This Post:
# 3  
Old 03-30-2011
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.
This User Gave Thanks to joeyg For This Post:
# 4  
Old 03-30-2011
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.
This User Gave Thanks to rdcwayx For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to compare previous and current item in for loop in bash?

Hey, I am trying to compare formated login and logout dates from one user at a host which I have stored in a tmp directory in order to find out the total login time. I need to compare them in order to find overlapping intervals. At first I tried to store each log in and logo date in an array... (3 Replies)
Discussion started by: Mumu123
3 Replies

2. Shell Programming and Scripting

How to compare the current result with previous line result.?

Hi Gurus, I have requirement to compare current result with previous reuslt. The sample case is below. 1 job1 1 1 job2 2 1 job3 3 2 job_a1 1 2 job_a2 2 2 job_a3 3 3 job_b1 1 3 job_b2 2 for above sample file, GID is group ID, for input line, the job run... (1 Reply)
Discussion started by: ken6503
1 Replies

3. Shell Programming and Scripting

Perl to send previous and current value

For example, I have a file called number.txt. x y 1 1 2 4 3 9 4 6 5 5 6 6 7 9 8 4 9 1 10 0 ... And I want to print out the value of x and y, if y%4==0 and the next value of y%4==0. Thus, the sample output is: 1 1 *because the previous x before 2 is 1 2 4 *because 4%4 == 0 7 9... (2 Replies)
Discussion started by: Tzeronone
2 Replies

4. Shell Programming and Scripting

How to compare current record,with next and previous record in awk without using array?

Hi! all can any one tell me how to compare current record of column with next and previous record in awk without using array my case is like this input.txt 0 32 1 26 2 27 3 34 4 26 5 25 6 24 9 23 0 32 1 28 2 15 3 26 4 24 (7 Replies)
Discussion started by: Dona Clara
7 Replies

5. Shell Programming and Scripting

Compare Field in Current Line with Field in Previous

Hi Guys I have the following file Essentially, I am trying to find the right awk/sed syntax in order to produce the following 3 distinct files from the file above: Basically, I want to print the lines of the file as long as the second field of the current line is equal to the... (9 Replies)
Discussion started by: moutaye
9 Replies

6. UNIX for Dummies Questions & Answers

Awk to print data from current and previous line

Hi guys, I have found your forum super useful. However, right now I am stuck on a seemingly "simple" thing in AWK. I have two columns of data, the first column in Age (in million years) and the second column is Convergence Rate (in mm/yr). I am trying to process my data so I can use it to... (2 Replies)
Discussion started by: awk_noob_456
2 Replies

7. Shell Programming and Scripting

Compare two files using awk or sed, add values in a column if their previous fields are same

Hi All, I have two files file1: abc,def,ghi,5,jkl,mno pqr,stu,ghi,10,vwx,xyz cba,ust,ihg,4,cdu,oqw file2: ravi,def,kishore ramu,ust,krishna joseph,stu,mike I need two output files as follows In my above example, each row in file1 has 6 fields and each row in file2 has 3... (3 Replies)
Discussion started by: yerruhari
3 Replies

8. UNIX for Dummies Questions & Answers

Compare two files using awk or sed, add values in a column if their previous fields are same

Hi All, I have two files file1: abc,def,ghi,5,jkl,mno pqr,stu,ghi,10,vwx,xyz cba,ust,ihg,4,cdu,oqw file2: ravi,def,kishore ramu,ust,krishna joseph,stu,mike I need two output files as follows In my above example, each row in file1 has 6 fields and each row in file2 has 3... (1 Reply)
Discussion started by: yerruhari
1 Replies

9. UNIX for Advanced & Expert Users

Compare two files using awk or sed, add values in a column if their previous fields are same

Hi All, I have two files file1: abc,def,ghi,5,jkl,mno pqr,stu,ghi,10,vwx,xyz cba,ust,ihg,4,cdu,oqw file2: ravi,def,kishore ramu,ust,krishna joseph,stu,mike I need two output files as follows In my above example, each row in file1 has 6 fields and each row in file2 has 3... (1 Reply)
Discussion started by: yerruhari
1 Replies

10. Shell Programming and Scripting

to write a script to compare the file size in the current directory and previous dir

hi, i am new to this site. i want to write a script to compare the file size of the files in the current dir with the files in the previous directory. the files name will be same, but the filename format will be as xyzddddyymm.txt. the files will arrive with the month end date(i want to... (5 Replies)
Discussion started by: tweety
5 Replies
Login or Register to Ask a Question