File updation on matching key


 
Thread Tools Search this Thread
# 8  
Inferring from lines 5 and 8, the division should be done by constant 2, not the line count per key. Under the assumption that field 8, embedded in field separators, is NOT matched anywhere else in the line, try
Code:
awk 'NR == FNR {SUM[$3]+=$7+$9; next} {sub (FS $8 FS, FS sprintf ("%06d", SUM[$3]/2) FS)}1' file file
RRD       0Z91YUn000000Lk 9000100001 103020151117   STMT151117155527001            0000 2 000006 000004
RRD       0Z91YUn00000ysj 9000100001 103020151117   STMT151117155527001            0000 3 000006 000003
RRD       0Z91YUn00001vGh 9000100002 103020151117   STMT151117155527002            0000 1 000007 000007
RRD       0Z91YUn00002tng 9000100002 103020151117   STMT151117155527002            0000 0 000007 000006
RRD       0Z91YUn00003qBe 9000100003 103020151117   STMT151117155527003            0000 1 000004 000007
RRD       0Z8xgNq000000Lk 9000100005 33820160129    STMT151117155527001            0000 5 000010 000006
RRD       0Z8xgNq00000hs6 9000100005 33820160129    STMT151117155527001            0000 3 000010 000006
RRD       0Z8xgNq00001HxO 9000100006 33820160129    STMT151117155527002            0000 0 000003 000006

Should the line count matter, try
Code:
awk 'NR == FNR {SUM[$3]+=$7+$9; CNT[$3]++; next} {sub (FS $8 FS, FS sprintf ("%06d", SUM[$3]/CNT[$3]) FS)}1' file file

This User Gave Thanks to RudiC For This Post:
# 9  
Hi Rudic,

Thanks for your update on this. But it is not working for my case. And one more thing I have not got your code
Code:
awk 'NR == FNR {SUM[$3]+=$7+$9; next} {sub (FS $8 FS, FS sprintf ("%06d", SUM[$3]/2) FS)}1' file file

Here I have not got "file" means which file ? I have used your code for my input but it not worked.
I brief u about input and output . Input as below
Code:
RRD       0Z9He4r000000Lk 200741     00120160129    STMT151117055527001            0000 1 000000 000001
RRD       0Z9He4r00001Gnl 200745     00120160129    STMT151117055527001            0000 1 000000 000001
RRD       0Z9He4r00001IPC 3025339512 33820160129    STMT151117055527001            0000 1 000000 000005
RRD       0Z9He4r00000pTO 4025339512 33820160129    STMT151117055527001            0000 0 000000 000002
RRD       0Z9He4r000001xE 5025339518 33820160129    STMT151117055527001            0000 0 000000 000004
RRD       0Z9He4r00000SHB 7025339518 33820160129    STMT151117055527001            0000 1 000000 000003
RRD       0Z9He4r00000XXX 7025339518 33823333333    STMT151117055527001            0000 3 000000 000002

in the Input given column 3 is the key column. consider row 1
Code:
RRD       0Z9He4r000000Lk 200741     00120160129    STMT151117055527001            0000 1 000000 000001

Here key is 200741. So search for 200741 in remaining rows of column 3. Here is no other key like 200741. So here calculation will be like
((7th column value + 9th column value)of row 1)/2 ie (1+1)/2=1. So update column 8 of row 1 with value 000001.


Explanation 2 : Consider row 6
Code:
RRD       0Z9He4r00000SHB 7025339518 33820160129    STMT151117055527001            0000 1 000000 000003

here key is 7025339518. So search for 7025339518 in remaining rows of column 3. Here you will find key 7025339518 in row 7 as well. so for this the calculation
will be like ((7th column value + 9th column value)of row 6 + (7th column value + 9th column value)of row 7)/2 => ((1+3)+(3+2))/2=5. So update column 8 of row 6
and 7 with value 000005


So Expected output for above result is :

Code:
RRD       0Z9JS1l000000Lk 200741     00120160129    STMT151117055527001            0000 1 000001 000001
RRD       0Z9JS1l00001Gnl 200745     00120160129    STMT151117055527001            0000 1 000001 000001
RRD       0Z9JS1l000001xE 5025339518 33820160129    STMT151117055527001            0000 0 000002 000004
RRD       0Z9JS1l00000pTO 4025339512 33820160129    STMT151117055527001            0000 0 000001 000002
RRD       0Z9JS1l00001IPC 3025339512 33820160129    STMT151117055527001            0000 1 000003 000005
RRD       0Z9JS1l00000SHB 7025339518 33820160129    STMT151117055527001            0000 1 000005 000003
RRD       0Z9He4r00000XXX 7025339518 33823333333    STMT151117055527001            0000 3 000005 000002

Please update code accordingly..


---------- Post updated at 08:42 PM ---------- Previous update was at 08:00 PM ----------

Hi Ridic,
your code worked. Smilie
Code:
awk 'NR == FNR {SUM[$3]+=$7+$9; next} {sub (FS $8 FS, FS sprintf ("%06d", SUM[$3]/2) FS)}1' $input_file $input_file

for below input :
Code:
RRD       0Z9He4r000000Lk 200741     00120160129    STMT151117055527001            0000 1 000000 000001
RRD       0Z9He4r00001Gnl 200745     00120160129    STMT151117055527001            0000 1 000000 000001
RRD       0Z9He4r00001IPC 3025339512 33820160129    STMT151117055527001            0000 1 000000 000005
RRD       0Z9He4r00000pTO 4025339512 33820160129    STMT151117055527001            0000 0 000000 000002
RRD       0Z9He4r000001xE 5025339518 33820160129    STMT151117055527001            0000 0 000000 000004
RRD       0Z9He4r00000SHB 7025339518 33820160129    STMT151117055527001            0000 1 000000 000003
RRD       0Z9He4r00000XXX 7025339518 33823333333    STMT151117055527001            0000 3 000000 000002

output as below :
Code:
RRD       0Z9He4r000000Lk 200741     00120160129    STMT151117055527001            0000 1 000001 000001
RRD       0Z9He4r00001Gnl 200745     00120160129    STMT151117055527001            0000 1 000001 000001
RRD       0Z9He4r00001IPC 3025339512 33820160129    STMT151117055527001            0000 1 000003 000005
RRD       0Z9He4r00000pTO 4025339512 33820160129    STMT151117055527001            0000 0 000001 000002
RRD       0Z9He4r000001xE 5025339518 33820160129    STMT151117055527001            0000 0 000002 000004
RRD       0Z9He4r00000SHB 7025339518 33820160129    STMT151117055527001            0000 1 000004 000003
RRD       0Z9He4r00000XXX 7025339518 33823333333    STMT151117055527001            0000 3 000004 000002

Just one minor updation needed. If the division result is decimal value ie for row 6 and 7 it is 4.5 but it shows it as 4 . Can we make it as round value as 5. ie if result is 6.5 then it should be display 7

Last edited by RudiC; 06-09-2016 at 12:25 PM.. Reason: Removed unnecessary quote tags.
# 10  
Quote:
Originally Posted by PRAMOD 96
.
.
.
But it is not working for my case.
WHAT is not working for your case?

Quote:
And one more thing I have not got your code
What code then have you got?

Quote:
Please update code accordingly.
The approach of these fora could be "Teach me to do it myself" ("Hilf mir, es selbst zu tun"; is a quote from Maria Montessori). So the preferred method would be to show an effort by yourself and ask for improvement potential, not a request like above.


It is appreciated that you learned your lesson reg. code tags! But ... quote tags are for quotes, i.e. if you copy other posts or threads, or even outside contributions, use those. NOT for your own contributions!


Reg. your problems:
For "file" read "your input file". Supply it twice to the script.

my solution applied to your new sample data yields:
Code:
RRD       0Z9He4r000000Lk 200741     00120160129    STMT151117055527001            0000 1 000001 000001
RRD       0Z9He4r00001Gnl 200745     00120160129    STMT151117055527001            0000 1 000001 000001
RRD       0Z9He4r00001IPC 3025339512 33820160129    STMT151117055527001            0000 1 000003 000005
RRD       0Z9He4r00000pTO 4025339512 33820160129    STMT151117055527001            0000 0 000001 000002
RRD       0Z9He4r000001xE 5025339518 33820160129    STMT151117055527001            0000 0 000002 000004
RRD       0Z9He4r00000SHB 7025339518 33820160129    STMT151117055527001            0000 1 000004 000003
RRD       0Z9He4r00000XXX 7025339518 33823333333    STMT151117055527001            0000 3 000004 000002

You seem to want the result rounded, not truncated? Put SUM[$3]/2+.5) into the sprintf statement.


How about the specification: I want field 8 to be the rounded sum of the $7 and $9 averages per key value?

Last edited by RudiC; 06-09-2016 at 12:38 PM..
This User Gave Thanks to RudiC For This Post:
# 11  
Obviously your (late) update crossed my post referring to your former post.
# 12  
Thank you Ridic for your update. I will remember your quotes.
And rounding result value code also worked. I liked "Teach me to do it myself" thing and will do the same in future whenever needed.

Thanks again for help.
# 13  
I'm glad you got it to work. If you'd like to show us how you changed the code to perform the rounding you wanted, we might be able to offer suggestions for alternative ways to do it. (And, having your completed solution in the thread helps others who may have a similar problem in the future if they can see how you solved the problem.)

If field 8 always 6 characters? Or, can the length of that field vary from file to file too? (Making that field variable length isn't complicated, if the length varies.)

Can the original value in field 8 ever appear as the value of field 2, 3, 4, 5, 6, or 7 in any of your input files? (As long as the 1st field is always left aligned and the aren't any empty fields in your input files, it wouldn't be too hard to be sure that field 8 is modified in the output and not an earlier field with the same contents. But, the code is faster and simpler as provided in RudiC's suggestion if this isn't a concern for your input files.)
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Matching 2 files based on key

Hi all I have two files I need to match record from first file and second file on column 1,8 and and output only match records on file1 File1: 020059801803180116130926800002090000800231000245204003160000000002000461OUNCE000000350000100152500BM01007W0000 ... (5 Replies)
Discussion started by: arunkumar_mca
5 Replies

2. UNIX for Dummies Questions & Answers

awk - Print lines if only matching key is found

I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. Thanks a lot. Any help is appreciated. Script I am using: awk 'FNR == NR && ! /^]*$/ {... (9 Replies)
Discussion started by: High-T
9 Replies

3. Shell Programming and Scripting

Rsa public private key matching

Hi All, I have a requirement where i need to check if an rsa public key corresponds to a private key and hence return success or failure. Currently i am using the command diff <( ssh-keygen -y -e -f "$PRIVKEY" ) <( ssh-keygen -y -e -f "$PUBLICKEY" ) and its solving my purpose. This is in... (1 Reply)
Discussion started by: mritusmoi
1 Replies

4. Shell Programming and Scripting

Config file auto-updation

Hello All, I need to update my .cfg file which is used in the script for almost all runs. myfile.cfg file: var=1 var1=1 run=0 script: #! /bin/sh . /mydir/myfile.cfg echo $var"\t" $var1 exit So, the requirement is that the myfile.cfg should update every time I run the... (10 Replies)
Discussion started by: PikK45
10 Replies

5. Shell Programming and Scripting

check files updation

Hi All, Can anyone help to write the script to check files updation? i have files as mentioned below. which will be updated some time. i just want to check the last file is updating the data for last 15 mins or not. if its not updating i want to print NOT OK. if its updating data i want... (1 Reply)
Discussion started by: steve2216
1 Replies

6. AIX

Problem with updation of 'quota'

Hi, We have recently implemented 'quota' concept for the unix users. softlimit - 230MB hardlimit - 250MB We have applied the quota when few of users are more than the hardlimit,issue is that even though the users cleared the space, still its 'quota' was not updating properly. For some... (0 Replies)
Discussion started by: girish_satyam
0 Replies

7. Shell Programming and Scripting

awk should output if one input file doesnt have matching key

nawk -F, 'FNR==NR{a= $3 ;next} $2 in a{print $1, 'Person',$2, a}' OFS=, filea fileb Input filea Input fileb output i am getting : (2 Replies)
Discussion started by: pinnacle
2 Replies

8. Shell Programming and Scripting

Matching by key fields

I have a file (key.dat) that contains two columns: AA|1234| BB|567| CC|8910| I have another file (extract.dat) that contains some data: SD|458|John|Smith| AA|3345|Frank|Williams| AA|1234|Bill|Garner| BD|0098|Yu|Lin| BB|567|Gail|Hansen| CC|8910|Ken|Nielsen| I want to compare the... (5 Replies)
Discussion started by: ChicagoBlues
5 Replies

9. Linux

gcc updation on Linux machine

Hi All, I already have gcc complier installed in my machine. Its version is : gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5) I am not sure whethere it's is latest gcc version available. I want to update my gcc version. Can anyone please suggest me what is the latest and stable gcc... (1 Reply)
Discussion started by: bisla.yogender
1 Replies

10. Programming

Directory updation Notification?

Hi, I'm a UNIX newbie .. so forgive me if this question sounds dumb. :) Is it possible for Unix to notify a process that a particular directory has been updated? Rather that the process constantly polling the directory ... Awaiting your replies .. Thanks, VJ (6 Replies)
Discussion started by: vjsony
6 Replies

Featured Tech Videos