LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 08-22-2012, 06:21 AM   #1
abhinav4
Member
 
Registered: Jun 2010
Location: India
Distribution: Fedora/Cent OS
Posts: 123

Rep: Reputation: 0
Exclamation 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.
 
Old 08-22-2012, 06:42 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
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.
Old 08-22-2012, 06:52 AM   #3
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060
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.
 
Old 08-22-2012, 07:10 AM   #4
abhinav4
Member
 
Registered: Jun 2010
Location: India
Distribution: Fedora/Cent OS
Posts: 123

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by tronayne View Post
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
 
Old 08-22-2012, 07:28 AM   #5
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060
Thought I did, but, OK
Code:
awk -F':' '{printf("%10s\t%s\n",$1,$7)}' /etc/passwd
 
1 members found this post helpful.
Old 08-22-2012, 10:17 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
"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).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] date space in between not tab ted_chou12 Linux - Newbie 2 03-29-2011 07:05 PM
[SOLVED] how to distinguish between tab and space in a file akhand jyoti Linux - Newbie 3 03-22-2011 02:12 AM
Sed adds a space instead of tab at end of line kaprasanna1 Linux - Newbie 12 01-14-2011 09:24 AM
awk - working with space and tab indiancosmonaut Programming 7 07-10-2007 12:46 PM
Tab space in VI (M) ar1 Linux - Software 1 12-07-2003 11:47 PM


All times are GMT -5. The time now is 01:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration