简体   繁体   中英

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.

for now, I'm dumping output in one text file [table.txt] then read line by line to print in formated order.

also want to print the location of file [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):

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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