space in output from awk command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting space in output from awk command
# 1  
Old 08-13-2007
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  
Old 08-13-2007
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  
Old 08-14-2007
Thanks but I would like to know why this happens.
I can't figure it out where those blanks come from.

cheers
# 4  
Old 08-14-2007
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  
Old 08-14-2007
Thats because u changed the Fs to : , check this:

Code:
echo "ddf: sasa"|awk -F\: '{print ">>"$1"<<>>"$2"<<"}'

# 6  
Old 08-14-2007
Quote:
Originally Posted by Klashxx
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  
Old 08-14-2007
Moreover, I also tried

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

some blanks occur again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Gawk --- produce the output in pattern space instead of END space

hi, I'm trying to calculate IP addresses and their respective calls to our apache Server. The standard format of the input is HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST... (2 Replies)
Discussion started by: busyboy
2 Replies

2. Shell Programming and Scripting

awk output is space delimeted not tab delimeted

In the below awk the output is space delimited, but it should be tab delimited. Did I not add the correct -F and OFS? Thank you :). The input file are rather large so I did not include them, but they are tab-delimeted files as well. awk awk -F'\t' -v OFS='\t' 'FNR==1 { next } > ... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Solaris

No output from awk command

Hi all, In my SunOS 5.10, the command awk using BEGIN option doesn't print output variables like expected here is my test: with the following code everything is alright ps -eo pcpu,args | grep "httpd" | awk ' { print $1 } ' the result is 0.1 However, with this command ps -eo... (3 Replies)
Discussion started by: Elmassimo
3 Replies

4. Shell Programming and Scripting

Grep output to awk command

Hi Team(Solaris 5.8/Ksh), How can we save grep output to awk variable when grep returns more than one line or word. abc.log # more abc.log Hi Makarand How r u bye Makarand Hello when grep returns only 1 word below command works nawk -v var=`cat abc.log |grep "Hello"` 'BEGIN { if... (6 Replies)
Discussion started by: Makarand Dodmis
6 Replies

5. Shell Programming and Scripting

Use of awk to filter out the command output

Hi All, I am trying to find out number of cores present for hp-ux server from the output of print_manifest (as shown below). i suppose awk will be best tool to use for filtering. output of print_manifest is : System Hardware Model: ia64 hp Integrity Virtual Partition ... (6 Replies)
Discussion started by: omkar.jadhav
6 Replies

6. Shell Programming and Scripting

usage of Awk command for output

Hi Experts, I have a Text file generated as below; <NAME> NEW#<technicalName><TAB> <Version> OLD#<technicalName><TAB> <Version> e.g. CH_PPV_AUDIT_DISTRIBUTOR NEW#EL_CFG_FTP_DISTRIBUTOR 2.1.0.upc2 OLD#EL_CFG_FTP_DISTRIBUTOR 2.1.0.upc1... (19 Replies)
Discussion started by: rajangupta2387
19 Replies

7. Shell Programming and Scripting

Format output in AWK command

hi Friends , I have a file as below s.txt 1~2~~4 2~6~~7 3~8~~9 t.txt 1~2~~4 2~5~8~7 3~8~~7 header for both files is common (2 Replies)
Discussion started by: i150371485
2 Replies

8. Shell Programming and Scripting

awk or sed command to print specific string between word and blank space

My source is on each line 98.194.245.255 - - "GET /disp0201.php?poc=4060&roc=1&ps=R&ooc=13&mjv=6&mov=5&rel=5&bod=155&oxi=2&omj=5&ozn=1&dav=20&cd=&daz=&drc=&mo=&sid=&lang=EN&loc=JPN HTTP/1.1" 302 - "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR... (5 Replies)
Discussion started by: elamurugu
5 Replies

9. Emergency UNIX and Linux Support

getting wrong output with AWK command!!!

i have a file which gets appended with 9 records daily and the file keeps growing from then...i use to store the previous day files count in a variable called oldfilecount and current files count as newfilecount.my requirement is that i need to start processing only the new records from the... (3 Replies)
Discussion started by: ganesh_248
3 Replies

10. Shell Programming and Scripting

Format Output with AWK command

Hi - I have a file with contents as below. 12.1 a.txt 12.1 b.txt 12.1 c.txt 13.2 a.txt 13.2 d.txt 14.3 f.txt 15.4 a.txt 15.4 b.txt 15.4 z.txt I need to print the contents like this. 12.1 a.txt <&nbsp><&nbsp><&nbsp>b.txt <&nbsp><&nbsp><&nbsp>c.txt (7 Replies)
Discussion started by: guruparan18
7 Replies
Login or Register to Ask a Question