1

I'm developing a program that outputs all the needed data from multiple files into columns. The problem is, my program can't align multiple output into a neat column.

This is my program functions:

#!/bin/sh

function findFile
{
    while read -r LINE
    do
            find . -name "${LINE}" | grep "${LINE}" || echo "${LINE} not found"
    done < /path/to/fnames.txt > /path/to/Files.txt
}

findFile

function grepFile
{
    printf "ISA\t\tGS\tTrans\tSender ID\tReceiver ID\tINV PO Number\tASN PO Number\n"
    fmt="%-16s%-8s%-8s%-8s%-24s%-9s%-12s\n"
    while read -r LINE
    do
            gs=$(zgrep -oP 'GS.[^*]*.[^*]*.[^*]*.[^*]*.[^*]*.\K[\w\s\d]*' < $LINE | sort -u)
            isa=$(zgrep -oP 'ISA.[^*]*.[^*]*.[^*]*.[^*]*.[^*]*.[^*]*.[^*]*.[^*]*.[^*]*.[^*]*.[^*]*.[^*]*.\K[\w\s\d]*' < $LINE | sort)
            trans=$(zgrep -oP 'GE.\K[\w\s\d-]*' < $LINE | sort -u)
            sender=$(zgrep -oP 'ISA.[^*]*.[^*]*.[^*]*.[^*]*.[^*]*.\K[\w\s\d-]*' < $LINE | sort -u)
            receiver=$(zgrep -oP 'ISA.[^*]*.[^*]*.[^*]*.[^*]*.[^*]*.[^*]*.[^*]*.\K[\w\s\d-]*' < $LINE | sort -u)
            bigponumber=$(zgrep -oP 'BIG.[\w\s\d-]*.[\w\s\d]*.[\w\s\d]*.\K[\w\s\d-]*' < $LINE | sort -u)
            asnponumber=$(zgrep -oP 'PRF.\K[\w\s\d-]*' < $LINE | sort -u)
            printf "$fmt" "${isa}" "${gs}" "${trans}" "${sender}" "${receiver}" "${bigponumber}" "${asnponumber}"
    done < /path/to/Files.txt
    rm /path/to/Files.txt
}

grepFile

output: Notice those numbers that are populating the left side of the screen? That's the PO Numbers.

ISA             GS      Trans   Sender ID       Receiver ID     INV PO Number   ASN PO Number
100000107       1107    1       2035735845         AMAZON                       2IJW4EUG
100000018       1018    1       00116520M      5032337598                       0082313829
100000096       1096    7       001165208      6111470100                       0026-7684347-0551
0026-7684347-0557
0026-7684347-0580
0026-7684347-0587
0026-7684347-0589
0026-7684347-3803
0026-7684347-3804
100000580       1580    6       2035735845     TGTDVS                           1310548379
1310570020
1310590953
1310596065
1310598477
1310609258

I want it to be like this: The same goes to INV PO Numbers

ISA             GS      Trans   Sender ID       Receiver ID     INV PO Number   ASN PO Number
100000107       1107    1       2035735845         AMAZON                       2IJW4EUG
100000018       1018    1       00116520M      5032337598                       0082313829
100000096       1096    7       001165208      6111470100                       0026-7684347-0551
                                                                                0026-7684347-0557
                                                                                0026-7684347-0580
                                                                                0026-7684347-0587
                                                                                0026-7684347-0589
                                                                                0026-7684347-3803
                                                                                0026-7684347-3804
100000580       1580    6       2035735845     TGTDVS                           1310548379
                                                                                1310570020
                                                                                1310590953
                                                                                1310596065
                                                                                1310598477
                                                                                1310609258
3

You can do this as shown:

grepFile | column -t -s $'\t' -o $'\t'

Provided the function grepFile has been modified as per my suggestions as given .

Create one more user-defined function, say, "alignCols" and pass the output from your "grepFile" function to it.

Also, modify slightly the "grepFile" function in the printf statement:

function grepFile
{

    #*************** ADD THIS ********
    t=$'\t'; # TAB char
    #*********************************

    printf "ISA\t\tGS\tTrans\tSender ID\tReceiver ID\tINV PO Number\tASN PO Number\n"

    #*************** MODIFY THIS ********
    fmt="%s\t%s\t%s\t%s\t%s\t%s\t%s\n"
    #*********************************

    while read -r LINE
    do
     # ... keep the other statements just like before #

     # ************** MODIFY printf ****************
     printf "$fmt" \
        "${isa:-$t}"         \
        "${gs:-$t}"          \
        "${trans:-$t}"       \
        "${sender:-$t}"      \
        "${receiver:-$t}"    \
        "${bigponumber:-$t}" \
        "${asnponumber:-$t}";
     # *********************************************

    done < /path/to/Files.txt
    rm /path/to/Files.txt
}

function alignCols
{
sed -e '
1i\
.TS\
tab('$'\t'');\
l l l l l l l.
$a\
.TE
' - | tbl - | nroff -Tascii -ms | grep .
}

# And then...

grepFile | alignCols

Results

ISA         GS     Trans   Sender ID    Receiver ID   INV PO Number   ASN PO Number
100000107   1107   1       2035735845   AMAZON                        2IJW4EUG
100000018   1018   1       00116520M    5032337598                    0082313829
100000096   1096   7       001165208    6111470100                    0026-7684347-0551
                                                                      0026-7684347-0557
                                                                      0026-7684347-0580
                                                                      0026-7684347-0587
                                                                      0026-7684347-0589
                                                                      0026-7684347-3803
                                                                      0026-7684347-3804
100000580   1580   6       2035735845   TGTDVS                        1310548379
                                                                      1310570020
                                                                      1310590953
                                                                      1310596065
                                                                      1310598477
  • It discards the last Column which is the ASN PO Number and also removes the first character it queried from the PO Numbers. And unfortunately it doesn't solve the alignment of the PO numbers. It's still stuck on left side. It returns this in all lines it reads. I only posted one example here: tbl:<standard input>:4: excess data entry ASN PO Number' discarded – WashichawbachaW Jul 28 '17 at 3:57
  • @WashichawbachaW Can you upload your file Files.txt on https://pastebin and also what script you ran . – user218374 Jul 28 '17 at 7:42
  • no need to upload Files.txt. It only contains the names of files. What my program do is to output all the data from the contents of each of those files. See my comment on M4rty's answer. – WashichawbachaW Aug 1 '17 at 0:18
1

The key here is to use the column program cf. man 1 column

It will allow you align your data as needed through a few simple option like defining a separator

You also can take a look at this question : Formatting lists with the column command in *nix

  • 1
    This is in the right direction, but would be much better if it showed at least a minimal working example from the question. – Jeff Schaller Jul 27 '17 at 10:21
  • I tried it with the code I saw from the example but still, the PO numbers are aligned in the left side of the screen. (printf "ISA GS Trans Sender_ID Receiver_ID INV_PO_NUMBER ASN_PO_NUMBER\n${isa} ${gs} ${trans} ${sender} ${receiver} ${bigponumber} ${asnponumber}\n") | column -t – WashichawbachaW Jul 28 '17 at 5:26
  • I think the problem is with empty var which do not fill the column, you should ether try to fill blank with other characters or try with a different separator using the -s option – M4rty Jul 28 '17 at 8:17
  • Take a look at the command in bigponumber & asnponumber zgrep -oP 'BIG.[\w\s\d-]*.[\w\s\d]*.[\w\s\d]*.\K[\w\s\d-]*' < $LINE | sort -u. It outputs all the data gathered from the content of $LINE(text.file). Most of the time it has more than a hundred output. That I see is the problem because every output of data prints in default position which is in the left side of the screen. – WashichawbachaW Jul 28 '17 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.