我正在开发一个程序,将多个文件中的所有所需数据输出到列中。问题是,我的程序无法将多个输出对齐到一个整齐的列中。
这是我的程序功能:
#!/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
输出:注意到屏幕左侧的那些数字了吗?这就是采购订单号。
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
我希望它是这样的: INV PO 编号也是如此
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
答案1
您可以按如下所示执行此操作:
grepFile | column -t -s $'\t' -o $'\t'
假设函数 grepFile 已根据我给出的建议进行修改。
再创建一个用户定义的函数,例如“alignCols”,并将“grepFile”函数的输出传递给它。
另外,稍微修改一下语句中的“grepFile”函数printf
:
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
结果
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