LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to set tab space (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-set-tab-space-4175423400/)

abhinav4 08-22-2012 06:21 AM

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

druuna 08-22-2012 06:42 AM

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
More details can be found here: http://www.gnu.org/software/gawk/man...tf.html#Printf

tronayne 08-22-2012 06:52 AM

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.

abhinav4 08-22-2012 07:10 AM

Quote:

Originally Posted by tronayne (Post 4760996)
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.

Could you give me the exact code

tronayne 08-22-2012 07:28 AM

Thought I did, but, OK
Code:

awk -F':' '{printf("%10s\t%s\n",$1,$7)}' /etc/passwd

David the H. 08-22-2012 10:17 AM

"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.