在 bash 中将输出格式化为动态表的模板

在 bash 中将输出格式化为动态表的模板

我创建了一个简单的脚本,它使用 ps aux 命令为用户输入的进程提供信息,并以类似表格的格式显示输出(类似于 mysql shell 为表格格式所做的事情)。

这在某种程度上是有效的,唯一的问题是如何使单元格根据值的长度动态地适应内容。如果值太长,则会换行并破坏表格。

是否有一些更智能的方法将值包装在其单元格中?

#!/bin/bash
# Main function
    main() {
        read -p "Enter the name of the process: " process
        output=$(ps aux | awk -v process="$process" '$0 ~ process && !/awk/ {print}')
        
        if [ -n "$output" ]; then
            printf "+------------+------------+------------+------------+------------+----------------------------+\n"
            printf "| %-10s | %-10s | %-10s | %-10s | %-10s | %-100s |\n" "USER" "PID" "%CPU" "%MEM" "START" "COMMAND"
            printf "+------------+------------+------------+------------+------------+----------------------------+\n"
    
            echo "$output" | awk '{ printf "| %-10s | %-10s | %-10s | %-10s | %-10s | %-100s |\n", $1, $2, $3, $4, $9, substr($0, index($0,$11)) }'
    
            printf "+------------+------------+------------+------------+------------+----------------------------+\n"
        else
            echo "No such process found: $process"
        fi
    }
    # Call the main function
    main

上面的电流输出:

Enter the name of the process: bash
+------------+------------+------------+------------+------------+----------------------------+
| USER       | PID        | %CPU       | %MEM       | START      | COMMAND                                                                                              |
+------------+------------+------------+------------+------------+----------------------------+
| userrt     | 1072       | 0.0        | 0.1        | 09:04      | -bash                                                                                                |
| userrt     | 1438       | 0.0        | 0.0        | 09:04      | bash                                                                                                 |
| userrt     | 1575       | 0.0        | 0.1        | 09:04      | /bin/bash --init-file /home/userrt/.vscode-server/bin/0ee08df0cf4527e40edc9aa28fdety5656bbff2b2/out/vs/workbench/contrib/terminal/browser/media/shellIntegration-bash.sh |
| userrt     | 3255       | 0.0        | 0.0        | 11:59      | /bin/bash ./process_monitoring.sh                                                                    |
| userrt     | 3286       | 0.0        | 0.0        | 11:59      | /bin/bash ./process_monitoring.sh                                                                    |
+------------+------------+------------+------------+------------+----------------------------+

在较小的屏幕上所需的输出,如下所示:

Enter the name of the process: bash
+------------+------------+------------+------------+------------+-----------------------------------+
| USER       | PID        | %CPU       | %MEM       | START      | COMMAND                           |
+------------+------------+------------+------------+------------+-----------------------------------+
| userrt     | 1072       | 0.0        | 0.1        | 09:04      | -bash                             |
| userrt     | 1438       | 0.0        | 0.0        | 09:04      | bash                              |
| userrt     | 1575       | 0.0        | 0.1        | 09:04      | /bin/bash --init-file /home/      |
|            |            |            |            |            |  userrt/.vscode-server/bin/       |
|            |            |            |            |            |  0ee08df0cf4527e40edc9aa28fdety   |
|            |            |            |            |            |  5656bbff2b2/out/vs/workbench/    |
|            |            |            |            |            |  contrib/terminal/browser/media/  |
|            |            |            |            |            |  shellIntegrtion-bash.sh          |
| userrt     | 3255       | 0.0        | 0.0        | 11:59      | /bin/bash ./process_monitoring.sh |
| userrt     | 3286       | 0.0        | 0.0        | 11:59      | /bin/bash ./process_monitoring.sh |
+------------+------------+------------+------------+------------+-----------------------------------+

答案1

#!/bin/bash

main() {
    read -p "Enter the name of the process: " process
    output=$(ps aux | awk -v process="$process" '$0 ~ process && !/awk/ {print}')

    if [ -n "$output" ]; then
        # Print header
        printf "| %-10s | %-10s | %-10s | %-10s | %-10s | %-80s |\n" "USER" "PID" "%CPU" "%MEM" "START" "COMMAND"
        
        # Print separator
        printf "|%s|\n" "-------------------------------------------------------------------------------------------"
    
        # Print data, use column command for formatting
        echo "$output" | awk '{ printf "| %-10s | %-10s | %-10s | %-10s | %-10s | %-80s |\n", $1, $2, $3, $4, $9, substr($0, index($0,$11)) }' | column -t -s "|" | fold -w 80 -s
    else
        echo "No such process found: $process"
    fi
}

main

exit 0

输出通过 传递column,它会自动调整列宽。指定-t表格,-s“|”选项设置分隔符。

基于内容长度的动态表格布局。

相关内容