printing a line number using awk | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

printing a line number using awk

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-01-2006
rjsha1 rjsha1 is offline
Registered User
 
Join Date: Dec 2005
Last Activity: 17 April 2013, 6:22 AM EDT
Posts: 33
Thanks: 1
Thanked 0 Times in 0 Posts
printing a line number using awk

Hi Chaps,
I'm trying to print the line number of a comma delimited file where the second field in the line is blank using AWK. Here is the code I have so far where am I going wrong. It is the last column in the file.

nawk -v x==0 'BEGIN {FS=",";OFS=","} x++ if ($2 == " ") print $x' bob.tst

Do I need to more the variable declaration or is my if a bit dodgy.

Please help.

Regards
Rob....
Sponsored Links
    #2  
Old 09-01-2006
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
AFK
 
Join Date: Dec 2004
Last Activity: 10 October 2013, 8:46 AM EDT
Location: UK
Posts: 2,351
Thanks: 0
Thanked 6 Times in 6 Posts
Hey Rob,
No variable declaration required... awk tracks things like that for you:

Code:
# cat test
this,is,a,test,line
this, ,a,test,line,with,the,second,field,blank
this, is,another,test,line,with,a,blank,in,the,second,field,but,the,field,isnt,blank
this, ,is,another,line,with,a,blank,second,field
# awk -F, '{if($2==" ") print NR}' test
2
4

To do it your way, try this:

Code:
nawk -F, 'BEGIN {x=1}; {if($2==" ")print x; x++}' test

I suggest incrementing x after you check so that you can start with x=1 (first line).

Last edited by blowtorch; 09-01-2006 at 07:14 AM..
Sponsored Links
    #3  
Old 09-01-2006
rjsha1 rjsha1 is offline
Registered User
 
Join Date: Dec 2005
Last Activity: 17 April 2013, 6:22 AM EDT
Posts: 33
Thanks: 1
Thanked 0 Times in 0 Posts
Hi blowtorch,
thats great - Ihave another question what happens if does not have a space and it just ,, or is , - which means the end of the line I have tried "" but it does nit like that
    #4  
Old 09-01-2006
rjsha1 rjsha1 is offline
Registered User
 
Join Date: Dec 2005
Last Activity: 17 April 2013, 6:22 AM EDT
Posts: 33
Thanks: 1
Thanked 0 Times in 0 Posts
don't worry guys

awk -F, '{if($2=="") print NR}' test


does the job thanks
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Printing Number of Fields with the line number machomaddy Shell Programming and Scripting 5 05-23-2012 06:31 AM
Printing the line number of first column found user553 Shell Programming and Scripting 3 03-14-2012 02:24 AM
Printing the line number in bash script suryaemlinux Shell Programming and Scripting 6 02-04-2011 04:57 PM
regarding about printing line number davidkhan Shell Programming and Scripting 7 07-13-2009 06:19 AM
printing line number Satyak Shell Programming and Scripting 5 09-29-2008 01:46 PM



All times are GMT -4. The time now is 10:30 AM.