Getting line number while using AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting line number while using AWK
# 1  
Old 04-15-2005
Getting line number while using AWK

Using AWK, while I am reading the file, I am separating fields based on the ':' & using NF. I also would like to mention line numbers from the file they are originally from.
How would I take out the line number for them?

I am trying something like following ,

awk -F":" '{
j=1
for (i=1; i<= NF; i++)
{
printf("\t%s\t%s\t%s\n",$j,$i,$(i+1))
}
j=j+1
}' filename

Can anyone please rectify?
# 2  
Old 04-15-2005
Just like NF is the number of the field in the line, NR is "record number" of the record (which, in this case, is the line).
Code:
awk -F":" '{
printf("\t%s\n",NR)
for (i=1; i<= NF; i++)
{
printf("\t%s\t%s\n",$i,$(i+1))
}
}' filename

I am not too sure about the syntax, but NR works for sure!

Cheers!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

awk for line number 2

This is what I have so far. xrandr | grep connected | grep -v disconnected | awk '{print $1}' This is my output. LVDS1 TV1 How can I awk for line number 2. The only output I want is TV1. (11 Replies)
Discussion started by: cokedude
11 Replies

3. Shell Programming and Scripting

Help on Sed/awk/getting line number from file

I Have file1 with below lines : #HostNameSelection=0 :NotUsed #HostNameSelection=1 :Automatic #HostNameSelection=3 :NotForced I have file2 which has similar lines but with different values I want to copy the changes from file1 to file2 ,line by line only if line begins with '#'. for... (7 Replies)
Discussion started by: mvr
7 Replies

4. 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

5. Shell Programming and Scripting

help: Awk to control number of characters per line

Hello all, I have the following problem: My input is two sorted files: file1 >1_19_130_F3 T01220131330230213311013000000110000 >1_23_69_F3 T01200211300200200010000001000000 >1_24_124_F3 T010203113002002111111200002010 file2 >1_19_130_F3 24 18 9 18 23 4 11 4 5 9 5 8 15 20 4 4 7 4... (9 Replies)
Discussion started by: DerSeb
9 Replies

6. UNIX for Dummies Questions & Answers

awk - display from line number to regex

Hi. Is there a way in awk to show all lines between a line number and the next line containing a particular regex? We can do these, of course: awk '/regex1/,/regex2/' filename awk 'FNR > X && FNR < Y' filename But can they be combined? Thanks. (3 Replies)
Discussion started by: treesloth
3 Replies

7. Shell Programming and Scripting

AWK: generate new line number

Hi. I have a script wich reads 1 file and generates 4. If the original file has 10 lines the the sum of the 4 generated files must have the 10 original lines. So far this works. Now what I need is to numerate the lines wtithin each generated file. I tried with NR but it prints the line... (2 Replies)
Discussion started by: mrodrig
2 Replies

8. Shell Programming and Scripting

awk help,line number

I am grep-ing the word "this" in all the files in my dir. $ awk '/this/' * this is this this I want the output as: 1)this is 2)this 3)this How can I achieve this ? Please help. HTH, jkl_jkl (4 Replies)
Discussion started by: jkl_jkl
4 Replies

9. 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

10. Shell Programming and Scripting

awk to select a column from particular line number

The awk command awk -F: '{print $1}' test1 gives the first columns of all the lines in file ,is there some command to get a particular column from particular line . Any help is appreciated. thanks arif (4 Replies)
Discussion started by: mab_arif16
4 Replies
Login or Register to Ask a Question