简体   繁体   English

如何在shell脚本/bash中将.txt文件输出转换为表格格式[不是html表格格式]

[英]how to convert .txt file output in to table format in shell script / bash [not in html table format]

I want to convert a text file into the MySQL type proper table format in the bash script.我想在 bash 脚本中将文本文件转换为 MySQL 类型的正确表格式。

for now, I'm dumping output in one text file [table.txt] then read line by line to print in formated order.现在,我将输出转储到一个文本文件 [table.txt] 中,然后逐行读取以按格式顺序打印。

also want to print the location of file [toatl path]还想打印文件的位置 [toatl path]

but I want a better solution.但我想要一个更好的解决方案。

#!/bin/bash
search_dir="$1"

path=$(ls -l $search_dir)

echo "$path" | awk -v OFS='\t''|''\t' 'BEGIN{print "          Owner         |      Size     |        Name     "}; {print"|" $3,  $5, $9 "\t""|"}' > table.txt
    

while IFS= read -r line; do
    echo "$line"
    echo "-----------------------------------------------------------------"
done < table.txt

所需的输出是这样的

Try this example as a template:试试这个例子作为模板:

#/bin/bash
header="Perm         D Owner  Group   Size Date         Name"
data=$(ls -l|sed 's/total.*//')
whiptail --title "Scrollbox" --scrolltext --msgbox "$header $data" 30 100

Use printf (adjust the format to your needs):使用printf (根据需要调整格式):

echo "$path" | awk -v 'BEGIN{print "          Owner         |      Size     |        Name     "}; {printf("|%-20s|%-20s|%-20s|\n", $3, $5, $9)}' > table.txt

you can also use the same format for the header if you like.如果您愿意,您也可以对标题使用相同的格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM