'Want to show output as tabular form of bash scripts
I want to add below data under column name as
- Number
- Name
- Result
Here are the script
echo -e "01. testing 123 \\t PASS"
echo -e "02. testing 123 \\t PASS"
echo -e "03. testing 123 \\t PASS"
echo -e "04. testing 123 \\t PASS"
echo -e "05. testing 123 \\t PASS"
I have tried every command but didn't get result
Solution 1:[1]
Don't use echo use printf, e.g.
printf "%02d. %-12s %s\n" "1" "testing 123" "PASS"
You can substitute variables for "1", "testing 123" and "PASS". The number will be 0-padded and displayed as 2-digits up to 100 and then 3-digits are required. You can change the field-width modifier in "%02d" to "%03d" to handle alignment of 3 digits (4 for 4-digits, etc...)
The "testing 123" text will always be displayed left-justified ('-') and in a field 12-characters wide (adjust as needed). With the first two-fields displayed, you can simply output the last one and it will take whatever space it takes.
You can substitute '\t' for the space separator between the format-specifiers in the printf statement if you want the columns tab-separated, e.g.
printf "%02d.\t%-12s\t%s\n" "1" "testing 123" "PASS"
See man 3 printf for the complete use of printf.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | David C. Rankin |
