How to find line number?

I have a data file (which has five columns) from which im finding column count of all the records and writing into separate file say "colcnt.txt". And I find one (or more) records have less column counts (i.e split records). I need to know which record(s) have that split scenario. Is there any way to find out line number from data file which has split records ?

Datafile.txt
1,abc,mon,589,hi
2,qwe,tue,456,bye
3,mnb,wed,145,pppp
4,asd,thurs
5,lkj,fri,675,hhhh
 
colcnt.txt
5
5
5
3
5
 
Output-
Line number : 4 has less number of columns which is a split record.

Thanks in Advance..!

Assuming if number of fields is less than 4 then it is a split record:

awk -F"," '{if(NF<4) {print "Line number :  "NR" has less number of columns which is a split record." }}' filename

If you need to count number of fields on each line, use

awk -F"," '{print NF}' filename
1 Like

Thanks..thats working..!

But when I tried passing runtime value for column count its not working

awk -F"," '{if(NF<$ColumnCnt) {print "Line number :  "NR" has less number of columns which is a split record." }}' filename

You need to use awk variables.

awk -F"," -v v1=$ColumnCnt '{if(NF<v1) {print "Line number :  "NR" has less number of columns which is a split record." }}' filename
1 Like

Thanks krishmaths and that was working as expected...!
Learned how to declare variable in "awk".