Help required in printing in specific format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help required in printing in specific format
# 1  
Old 12-15-2015
Help required in printing in specific format

Hi All,

I 'm matching two files based on the first 2 columns and then populate other fields along with subtraction of few fields.
I have managed to get the output. However, is there a easier way in formatting the output as shown below instead of using additional printf for getting fixed width output file?

file1:
Code:
20150731|05|0000000132|000001182401644.77
20150731|08|0000000000|000000000000000.00
20150731|09|0000026468|000001189347835.97
20150731|10|0000332300|000013254549890.91

file2:
Code:
20150731|05|0000000100|000001000000000.00
20150731|09|0000099999|000009999999999.99

Code:
Code:
awk -F'|' 'FNR==NR{a[$1,$2]=$0;next}{if(b=a[$1,$2]){print $0"|"b}}' file2 file1 | awk 'BEGIN { FS="|"; OFS = "|";OFMT = "%.2f"} {print $1,$2,$3,$4,$7,$8,($3 - $7),($4 - $8)}'

Output:
Code:
20150731|05|0000000132|000001182401644.77|0000000100|000001000000000.00|32|182401644.77
20150731|09|0000026468|000001189347835.97|0000099999|000009999999999.99|-73531|-8810652164.02

Desired output is:
Code:
20150731|05|0000000132|000001182401644.77|0000000100|000001000000000.00|+0000000032|+000000182401644.77
20150731|09|0000026468|000001189347835.97|0000099999|000009999999999.99|-0000073531|-000008810652164.02

Thanks
Shash
# 2  
Old 12-15-2015
Here is an awk approach:-
Code:
awk -F\| '
        NR == FNR {
                A[$1 FS $2] = $0
                next
        }
        ($1 FS $2) in A {
                split(A[$1 FS $2], T)
                m = ( $3 - T[3] ); f_m = ( m < 0 ? m * -1 : m )
                n = ( $4 - T[4] ); f_n = ( n < 0 ? n * -1 : n )
                printf "%s|%s|%s|%s|%s|%s|%c%010d|%c%018.2f\n", $1, $2, $3, $4, T[3], T[4], ( m > 0 ? "+" : "-" ), f_m, ( n > 0 ? "+" : "-" ), f_n
        }
' OFS=\| file2 file1


Last edited by Yoda; 12-15-2015 at 01:39 PM..
This User Gave Thanks to Yoda For This Post:
# 3  
Old 12-15-2015
Bit simpler? Try
Code:
awk -F'|'  '
                {IX = $1 FS $2
                }
FNR==NR         {T3[IX] = $3
                 T4[IX] = $4
                 next
                }
IX in T3        {printf "%s|%s|%s|%+011d|%+019.2f\n", $0, T3[IX], T4[IX], $3-T3[IX], $4-T4[IX]
                }
' file2 file1
20150731|05|0000000132|000001182401644.77|0000000100|000001000000000.00|+0000000032|+000000182401644.77
20150731|09|0000026468|000001189347835.97|0000099999|000009999999999.99|-0000073531|-000008810652164.02

This User Gave Thanks to RudiC For This Post:
# 4  
Old 12-16-2015
Many thanks Yoda & RudiC. Both your solutions worked!

---------- Post updated at 11:58 AM ---------- Previous update was at 04:04 AM ----------

Hi RudiC,

Is there a way to have thousand separator for numeric fields?
I tried using the following code but not able to use it in the code you provided
Code:
printf "%'.2f\n" 000001182401644.77

Can you please help?

Thanks
Shash
# 5  
Old 12-16-2015
While the single-quote-flag is discussed in e.g. my (linux) man printf it is not in FreeBSD. So its behaviour depends on the implementation.
bash (4.3.42(1)-release):
Code:
printf "%'.2f\n" 10000001182401644
10.000.001.182.401.644,00

awk:
Code:
awk 'BEGIN {printf  "%\047.2f\n", 10000001182401644}'
awk: run time error: improper conversion(number 1) in printf("%'.2f

So - you need to test what happens on your system / tool...
This User Gave Thanks to RudiC 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

Echo not printing the variables with delimiters as required

This is the file which contains only neccessary values from the output of curl command i.e TEMP_FILE Proxy Hostname server0123.domain.com Proxy IP address XXX.XXX.XX.XX port 0000 Proxy Version SGOS X.X.X.X Proxy Serial # ... (5 Replies)
Discussion started by: ramprabhum
5 Replies

2. UNIX for Dummies Questions & Answers

Printing files in a specific format

Hi, I have following files in a directory with '.meta' extension, which have data in follwoing patterns. i need to print data from these files in below metioned format. please provide a script for this solution. file names: TEST_HISTORY_MTH.meta AB_TEST_1.meta cat... (2 Replies)
Discussion started by: AAHinka
2 Replies

3. Shell Programming and Scripting

Searching and printing only required pattern

Hi all, i am trying to count the number of logical processors from the below output: # print_manifest | grep "logical processors" 8 cores, 16 logical processors per socket 2 logical processors (2 per socket) i just want to have below output : 16 2 also... (11 Replies)
Discussion started by: omkar.jadhav
11 Replies

4. Shell Programming and Scripting

Need help to format one txt file to required format

Hello Everyone, I have one source file which is genarated by SAP in different format(Which I've never seen). I need to convert that file to required format and I need to read this target file from Datastage to use this in my Jobs. So I do not have any other options except to use Unix script to... (4 Replies)
Discussion started by: Prathyu
4 Replies

5. UNIX for Dummies Questions & Answers

Printing lines with specific strings at specific columns

Hi I have a file which is tab-delimited. Now, I'd like to print the lines which have "chr6" string in both first and second columns. Could anybody help? (3 Replies)
Discussion started by: a_bahreini
3 Replies

6. Shell Programming and Scripting

Concatenating two files in required format

Firstly one of my mysql queries will yeild following output +-------+---------------------+-------------------+----------------------------------------------------------------------------+ | ID | PLATFORM | SORT_NAME | DESCRIPTION ... (2 Replies)
Discussion started by: vivek d r
2 Replies

7. Shell Programming and Scripting

Assigning a specific format to a specific column in a text file using awk and printf

Hi, I have the following text file: 8 T1mapping_flip02 ok 128 108 30 1 665000-000008-000001.dcm 9 T1mapping_flip05 ok 128 108 30 1 665000-000009-000001.dcm 10 T1mapping_flip10 ok 128 108 30 1 665000-000010-000001.dcm 11 T1mapping_flip15 ok 128 108 30... (2 Replies)
Discussion started by: goodbenito
2 Replies

8. UNIX for Dummies Questions & Answers

Help required on Printing of Numbers, which are missing in the range

Hi Experts, Need help on printing of numbers, which are missing in the range. Pls find the details below Input 1000000002 1000000007 1234007940 1234007946 Output 1000000003 1000000004 1000000005 1000000006 1234007941 (2 Replies)
Discussion started by: krao
2 Replies

9. Shell Programming and Scripting

Help required on grep command(Skip the first few lines from printing in the output)

Hi experts I want the proper argument to the grep command so that I need to skip the first few lines(say first 10 lines) and print all the remaining instances of the grep output. I tried to use grep -m 10 "search text" file*. But this gives the first 10 instances(lines) of the search string.... (7 Replies)
Discussion started by: ks_reddy
7 Replies

10. UNIX for Dummies Questions & Answers

Help required on printing the line if third field is not a empty space

Hi Experts, I want to print the lines whose third field in non-empty/blank space. i.e. INPUT FILE/B] dcdccghjrry0yd cont dcdccttrk820529 cont rdekedfsSCr dcdccttrdky2d0y cont rdekedfsSC2 ... (3 Replies)
Discussion started by: krao
3 Replies
Login or Register to Ask a Question