First I'd like to apologize if I opened a thread which is already open somewhere.
I did a bit of searching but could quite find what I was looking for, so I will try to explaing what I need.
I'm writing a script on our server, got to a point where I have two files with results. Example:
File1 (2 columns):
23 DEST_LOCN:35191
29 DEST_LOCN:36709
138 DEST_LOCN:38269
323 DEST_LOCN:38591
25 DEST_LOCN:38595
289 DEST_LOCN:39349
31 DEST_LOCN:42060
453 DEST_LOCN:43664
File2 (2 columns):
191 DEST_LOCN:38269
388 DEST_LOCN:38591
25 DEST_LOCN:38598
167 DEST_LOCN:39349
545 DEST_LOCN:43664
If a value in column2 is the same in both files, i'd like to print results like column1.file1, column1.file2, column2.file(1 or 2). Example:
138 191 DEST_LOCN:38269
323 388 DEST_LOCN:38591
289 167 DEST_LOCN:39349
453 545 DEST_LOCN:43664
Can you help me or point to correct thread?
Thank you!
M
rovf
November 11, 2016, 7:59am
2
join -j 2 -o 1.1,2.1,1.2 file1 file2
Thank you!
Is it possible to be that simple
---------- Post updated at 02:15 PM ---------- Previous update was at 02:01 PM ----------
I'd like to add one more question of I can.
After the join I also added a math function:
awk '{print ($1/$2)}'
I'd like to limit the output to two decimals. Trying somehow with printf
but not successful yet.
Thanks in advance!
RudiC
November 11, 2016, 8:31am
4
It's the correct approach. How would your attempt "somehow with printf
" look like? Which format string did you specify?
And, please format your posts correctly with code tags for code and data.
faked it a little, like that
awk '{print substr(($1/$2),0,4)'
but that's not the correct roundup
Try:
awk '{print $1/$2}' OFMT="%.2f" file
or
awk '{printf "%.2f\n",$1/$2}' file
--
And combined with the join suggested earlier (assumes sorted files)
join -12 -22 -o 1.1,2.1,0 file1 file2 | awk '{print $1/$2, $3}' OFMT="%.2f"
or
join -12 -22 -o 1.1,2.1,0 file1 file2 | awk '{printf "%.2f %s\n", $1/$2, $3}'
or combined in one awk (sorted order not required)
awk 'NR==FNR{A[$2]=$1; next} $2 in A{print A[$2]/$1, $2}' OFMT="%.2f" file1 file2
RudiC
November 11, 2016, 9:08am
7
Do you know the difference between printf
(your comment in post#3) and print
?