用shell脚本实现,读取txt内容输出为带边框的表格样式,表格中含三列内容,并固定每列宽度
时间: 2024-05-08 20:18:20 浏览: 7
很高兴为您解答问题。以下是一个简单的Shell脚本,可用于读取txt文件并输出带有边框的三列表格:
```bash
#!/bin/bash
# 设置每个列的宽度
column1_width=10
column2_width=20
column3_width=30
# 打印表格顶部边框
printf "+-%${column1_width}s-+%-${column2_width}s-+%-${column3_width}s-+\n" "" "" "" | tr ' ' '-'
# 打印表头
printf "| %-${column1_width}s | %-${column2_width}s | %-${column3_width}s |\n" "Column 1" "Column 2" "Column 3"
# 打印表格头部和尾部之间的内容
while read -r line; do
column1=$(echo "$line" | awk '{ print $1 }')
column2=$(echo "$line" | awk '{ print $2 }')
column3=$(echo "$line" | awk '{ print $3 }')
printf "| %-${column1_width}s | %-${column2_width}s | %-${column3_width}s |\n" "$column1" "$column2" "$column3"
done < your_file.txt
# 打印表格底部边框
printf "+-%${column1_width}s-+%-${column2_width}s-+%-${column3_width}s-+\n" "" "" "" | tr ' ' '-'
```
此脚本将会读入名为 `your_file.txt` 的文件,并在屏幕上输出一个带有边框的三列表格。您可以更改列的宽度和表格的标题和内容来满足您的要求。
希望这能帮到您!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)