|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
4To do it your way, try this: Code:
nawk -F, 'BEGIN {x=1}; {if($2==" ")print x; x++}' testI 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
|
|||
|
|||
|
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
|
|||
|
|||
|
don't worry guys
awk -F, '{if($2=="") print NR}' test does the job thanks |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|