-2

I have a created a simple script which is using the ps aux command to give information for a process entered by a user, and presents the output in a table like format (something like the mysql shell is doing for table format).

This is working to some extent, the only problem is how to make the cells fit the content dynamically depending on the value's length. If a value is too long, it goes on a new line and breaks the table.

Is there some smarter way to wrap the value within its cell?

#!/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 

Current output from above:

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 | +------------+------------+------------+------------+------------+----------------------------+ 

Desired output on smaller screens, something like this:

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 | +------------+------------+------------+------------+------------+-----------------------------------+ 
6
  • 1
    Please don't multi-post (stackoverflow.com/q/77713985/1745001). Copying your question to a different site doesn't make it easier for us to help you parse input when you won't show us the input anyway.
    – Ed Morton
    CommentedDec 28, 2023 at 12:37
  • I don't understand what kind of input I should supply, it's just a name of a process that is currently running on the machine. Can be anything, it doesn't matter.
    – Avocado
    CommentedDec 28, 2023 at 12:49
  • 1
    It certainly does matter and it's not just the name of a process, it's mainly example output of ps aux that you need to parse to look for the name of a process in. It should include partial substring matches (foo when foobar is present), no matches (foo when not present), full string matches, matches against other parts of the ps aux output (foo is in grep foo) so we can see how you want all of those handled and so we have something we can copy/paste to test a potential solution against and get a simple pass/fail result if it produces the output you show from the input you show.
    – Ed Morton
    CommentedDec 28, 2023 at 12:58
  • 1
    For example, your existing command would fail if you wanted to find processes named bash but a user named bash (short for Sebastian) was running a process named grep or anyone was running a process named foobashell or you wanted to find a process named awk, or various other things so make sure to cover all of those in your sample input/output.
    – Ed Morton
    CommentedDec 28, 2023 at 13:49
  • The current command will not fail, it will only show results for anything lets say "bash" related in the USER or COMMAND columns of the ps aux. It is fixable, but that's not the point of my question.
    – Avocado
    CommentedDec 28, 2023 at 14:33

1 Answer 1

0
#!/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 

Output is passed through column, which automatically adjusts the column width. The -t specifies tabular, and the -s "|" option sets the delimiter.

Dynamic table layout based on the content length.

2
  • Had the OP provided sample input you'd have been able to test that to see that, among other issues, not including the header line in the pipe to column and wrapping each whole line after column has aligned the fields rather than wrapping long fields before calling column will just output a mess.
    – Ed Morton
    CommentedJan 2, 2024 at 14:20
  • Agreed. This is an actual approach, I figured, being aware of column. It's a direction of some kind, at least, instead of just echo's to awk.CommentedJan 2, 2024 at 15:19

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.