How to set tab space
How can I format the below data.
Command uset to get the O/P awk -F':' 'BEGIN{OFS="\t";} {print $1,$7;}' /etc/passwd root /bin/bash bin /sbin/nologin daemon /sbin/nologin adm /sbin/nologin lp /sbin/nologin sync /bin/sync shutdown /sbin/shutdown halt /sbin/halt mail /sbin/nologin uucp /sbin/nologin operator /sbin/nologin games /sbin/nologin |
I'm not sure if you can set the tab width in (g)awk.
Have a look at printf and all its possibilities: Code:
awk -F: '{ printf "%-12s %s\n", $1, $7}' /etc/passwd |
You can do a fixed-length field for the account name using printf() formatting.
messagebus seems to be the longest (at 10 characters) so you'd want to change your print to, say, printf("%10s\t%s\n"),$1, $7; (and remove the BEGIN{OFS="\t";} because the tab is in the format); that forces the first field to be 10 characters wide, left justified (you could also make it right justified if you want). Hope this helps some. |
Quote:
|
Thought I did, but, OK
Code:
awk -F':' '{printf("%10s\t%s\n",$1,$7)}' /etc/passwd |
"Tab" is an ascii character just like any other, taking up one byte in a text file. It's up to the programs that display the text to decide how wide the tab appears visually. Awk doesn't do any displaying, it just processes the ascii value, so it has nothing to do with how "wide" the tab is.
Also, the tab display width calculation is a complex one that depends on the font width as well as the number of "spaces" it's set for, which is why the exact same text file can have completely different tab alignments under different fonts or programs. If you want text to always line up, you have to use physical spaces (and ideally a monospace display font). |
All times are GMT -5. The time now is 02:10 PM. |