Printing Number of Fields with the line number

Hi,
How to print the number of fields in each record with the line number?

Lets saw I have

3212|shipped|received|
3213|shipped|undelivered|
3214|shipped|received|delivered

I tried the code

awk -F '|' '{print NF}'

This gives me ouput as

    3
    3
    4

I want to print the line numbers:

1 3
2 3
3 4

Can someone give me an idea on how to do it? :rolleyes:

awk -F '|' '{print NR,NF}'
1 Like

Be careful with your expectations, as your results will actually be;

1 4
2 4
3 4

Even though the first two lines have nothing after the last "|" there will still be a count of 4 for each line - even for an empty field. You will need to manage that by either not adding the trailing "|" or checking for "relevant content" of each field. Something like, if a field = /^$/ then take 1 away from the value of NF.

If you have a consistent number of "|" then your NF will always be the same regardless of what is between them.

1 Like

In my file, each record ends with a delimiter. So, after the last delimiter, I would not be having anything. In this case, I will use the code:

  awk -F '|' '{print NR,NF-1}' 

. This will give me the correct number of fields.

source file is

Habcd145623
D1234567
D1234568
Drrrr
D3333
D1234567
D123456789900
T12344343

i need the line numbers where the length of the line is not equal to 8 by omiting H, D starting Records

i need utput as

4,5,7

i used the command i cat able to omit H and T starting ..lines

awk '{if (length($0) != 8) {print NR}}' <file name>

Please give me the solution

Please start en new thread for new questions, anyhow:

awk '!/^[HT]/ && length != 8{s=s?s "," NR:NR}END{print s}' file