The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
SED command to change ./ to a space bobk544 Shell Programming and Scripting 11 04-05-2007 09:46 AM
To display 1 or 2 space bar output missutoomuch Shell Programming and Scripting 2 02-11-2006 04:25 PM
Command to list used space in VG? Stephan UNIX for Dummies Questions & Answers 2 11-03-2005 03:34 PM
Preserving space during command substitution Sabari Nath S UNIX for Dummies Questions & Answers 1 10-04-2005 10:16 AM
How to trim space in output variable ? vj_76 Shell Programming and Scripting 5 06-12-2005 04:03 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-13-2007
lalelle lalelle is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 43
space in output from awk command

Hi
I have tried this command
cat /etc/passwd | awk -F: '{print$5}'

note that the $5 is the column that displays full name- i.e. Kevin Kambell

To the point, my output is fine except one thing I do not understand that some of output lines have space in front of them which I checked in /etc/passwd and there were no space before those names.

For example:
Matt damon
Ben Jones
-Kevin Kambell
Steward Note
Joke Chain
-William Ink

(Note that "-" is the space in the real output

However, the output has no space if I use the command:
awk -F: '{print$5}' /etc/passwd

the output was shown as follows:

Matt damon
Ben Jones
Kevin Kambell
Steward Note
Joke Chain
William Ink


Thanks a lot

Last edited by lalelle; 08-13-2007 at 05:47 PM.. Reason: Spaces were trimmed
  #2 (permalink)  
Old 08-13-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,954
Quote:
cat /etc/passwd | awk -F: '{print$5}'
This is called as UUOC - Useless Use of Cat

As you have used, it could just go with
Code:
awk -F":" '{ print $5 }' /etc/passwd
  #3 (permalink)  
Old 08-14-2007
lalelle lalelle is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 43
Thanks but I would like to know why this happens.
I can't figure it out where those blanks come from.

cheers
  #4 (permalink)  
Old 08-14-2007
lorcan lorcan is offline
Registered User
  
 

Join Date: May 2007
Posts: 219
The space in your output means that for few of the lines in the /etc/passwd file there is no 5th field defined.

Check the file...
  #5 (permalink)  
Old 08-14-2007
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
  
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 393
Thats because u changed the Fs to : , check this:

Code:
echo "ddf: sasa"|awk -F\: '{print ">>"$1"<<>>"$2"<<"}'
  #6 (permalink)  
Old 08-14-2007
lalelle lalelle is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 43
Quote:
Originally Posted by Klashxx View Post
Thats because u changed the Fs to : , check this:

Code:
echo "ddf: sasa"|awk -F\: '{print ">>"$1"<<>>"$2"<<"}'
I tried this and I saw the blank before sasa. What does it come from?
I am sorry. I am very new to awk.
  #7 (permalink)  
Old 08-14-2007
lalelle lalelle is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 43
Moreover, I also tried

awk -F: '{print $5}' /etc/passwd | sort

some blanks occur again
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:15 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0