Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-22-2012, 06:21 AM
|
#1
|
Member
Registered: Jun 2010
Location: India
Distribution: Fedora/Cent OS
Posts: 123
Rep:
|
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
Last edited by abhinav4; 08-22-2012 at 06:23 AM.
|
|
|
08-22-2012, 06:42 AM
|
#2
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
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
|
|
1 members found this post helpful.
|
08-22-2012, 06:52 AM
|
#3
|
Senior Member
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
|
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.
|
|
|
08-22-2012, 07:10 AM
|
#4
|
Member
Registered: Jun 2010
Location: India
Distribution: Fedora/Cent OS
Posts: 123
Original Poster
Rep:
|
Quote:
Originally Posted by tronayne
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
|
|
|
08-22-2012, 07:28 AM
|
#5
|
Senior Member
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
|
Thought I did, but, OK
Code:
awk -F':' '{printf("%10s\t%s\n",$1,$7)}' /etc/passwd
|
|
1 members found this post helpful.
|
08-22-2012, 10:17 AM
|
#6
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
"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 03:21 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|