Printing Number of Fields with the line number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing Number of Fields with the line number
# 1  
Old 01-21-2012
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
Code:
awk -F '|' '{print NF}'

This gives me ouput as
Code:
    3
    3
    4

I want to print the line numbers:
Code:
1 3
2 3
3 4

Can someone give me an idea on how to do it? Smilie
# 2  
Old 01-21-2012
Code:
awk -F '|' '{print NR,NF}'

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 01-21-2012
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.
This User Gave Thanks to Android Control For This Post:
# 4  
Old 01-22-2012
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:
Code:
  awk -F '|' '{print NR,NF-1}'

. This will give me the correct number of fields.
# 5  
Old 05-23-2012
source file is
Code:
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
Code:
4,5,7

i used the command i cat able to omit H and T starting ..lines
Code:
awk '{if (length($0) != 8) {print NR}}' <file name>

Please give me the solution

Last edited by Franklin52; 05-23-2012 at 07:19 AM.. Reason: Please use code tags
# 6  
Old 05-23-2012
Quote:
Originally Posted by ragu.selvaraj
source file is
Code:
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
Code:
4,5,7

i used the command i cat able to omit H and T starting ..lines
Code:
awk '{if (length($0) != 8) {print NR}}' <file name>

Please give me the solution

Please start en new thread for new questions, anyhow:
Code:
awk '!/^[HT]/ && length != 8{s=s?s "," NR:NR}END{print s}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print line if values in fields matches number and text

datafile: 2017-03-24 10:26:22.098566|5|'No Route for Sndr:RETEK RMS 00040 /ZZ Appl:PF Func:PD Txn:832 Group Cntr:None ISA CntlNr:None Ver:003050 '|'2'|'PFI'|'-'|'EAI_ED_DeleteAll'|'EAI_ED'|NULL|NULL|NULL|139050594|ActivityLog| 2017-03-27 02:50:02.028706|5|'No Route for... (7 Replies)
Discussion started by: SkySmart
7 Replies

2. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

3. Shell Programming and Scripting

Printing the line number of first column found

Hello, I have a question on how to find the line number of the first column that contains specific data. I know how to print all the line numbers of those columns, but haven't been able to figure out how to print only the first one that is found. For example, if my data has four columns: 115... (3 Replies)
Discussion started by: user553
3 Replies

4. Shell Programming and Scripting

print the number of fields in each line

I am looking for equivalent of following awk command in perl # awk '{ print NF ":" $0 } ' junk1 8:VAH NIC_TYPE CONFIG SIZE_GB PILO KOM BHA_GRP DESCR 8:2 NIC6 cont 34 y n shal_orgrp /shal 8:4 NIC5 signa 52 n y shal_orgrp... (3 Replies)
Discussion started by: dynamax
3 Replies

5. Shell Programming and Scripting

Printing the line number in bash script

Hi, I would like to know how do I print the line # in a script. My requirement is, I have a script which is about ~5000 lines long. If there are any errors happen I just exit. And I would like to add the line # of the script where the error happened. Thanks, (6 Replies)
Discussion started by: suryaemlinux
6 Replies

6. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

7. Shell Programming and Scripting

regarding about printing line number

Hello, I am testing some data to get line number at cursor position 9 and found some problem, the code is below.Assume we got 3 attribute. At second attribute, there are some data(eg.A41/A6) missing like at the fourth and six line 11006 A41 1888 11006 ... (7 Replies)
Discussion started by: davidkhan
7 Replies

8. Shell Programming and Scripting

printing line number

hi, i have a file, i need to search for a string , if the line contains i need to print that line number and line , please help thanks in advance Satya (5 Replies)
Discussion started by: Satyak
5 Replies

9. Shell Programming and Scripting

Adding a columnfrom a specifit line number to a specific line number

Hi, I have a huge file & I want to add a specific text in column. But I want to add this text from a specific line number to a specific line number & another text in to another range of line numbers. To be more specific: lets say my file has 1000 lines & 4 Columns. I want to add text "Hello"... (2 Replies)
Discussion started by: Ezy
2 Replies

10. Shell Programming and Scripting

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 ... (3 Replies)
Discussion started by: rjsha1
3 Replies
Login or Register to Ask a Question