Capture the last record number using "awk" NR variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Capture the last record number using "awk" NR variable
# 1  
Old 02-08-2017
Capture the last record number using "awk" NR variable

Hi Team. I am trying to capture the last record number from a file using the below command ( assuming abc.txt has 21 records and I want 21 as output )
Code:
awk'{c=NR;print c}'abc.txt

But it is printing all the record number. Can someone please help modify the above command?
# 2  
Old 02-08-2017
Consider
Code:
wc -l

which is meant to give number of records.

Code:
count=`wc -l < abc.txt`

awk is a great tool. However there are lots of useful unix tools, which you may want to consider
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 02-08-2017
If it is a trivial count, then commands like wc can give you this, however I think the easiest way using awk (if you are really doing some processing of the data in awk) is to have a count incremented each time you read a record, then in the END section, display the count.

Would this work for you?:-
Code:
awk '{c=NR;print c ; records++} END {print records}' abc.txt

Obviously adjust it to your preferences. The above will print a line number for every record read AND the final record count, so this might not be what you want. I would only go with awk if you are doing some other processing. wc is far simpler.



I hope that this helps,
Robin
This User Gave Thanks to rbatte1 For This Post:
# 4  
Old 02-08-2017
Thanks Jim. Actually I want to use the last record number in a for loop within awk command.

Ex :
Code:
awk'{for(i=1;i<=NR;i++) <action> }'abc.txt

Actually am assuming that the loop is running from 1 to 21 for the above

Last edited by rbatte1; 02-09-2017 at 05:21 AM.. Reason: Changed some CODE tags to ICODE tags
# 5  
Old 02-08-2017
The number of records which is NR contains the current line number. awk has an END{} block, where when it is reached NR will have the number 21 as value in your case.
Anyway, I second what the others say, that the least complex tool needed to do the job might be the tool of choice.

If I got it right, you just want to read an input file and do something with every line.

This could generally also be done with something like this:

Code:
while read LINE; do
    echo $LINE
done < infile

...where the echo could be something else of course.
This way you don't even need to know how many lines you have to use it in a for-loop, as it cycles over every line anyway.
This User Gave Thanks to zaxxon For This Post:
# 6  
Old 02-08-2017
Quote:
Originally Posted by chatwithsaurav
Thanks Jim. Actually I want to use the last record number in a
Code:
for

loop within
Code:
awk

command.
awk already does that, implicitly.

Pseudocode for an awk program:

Code:
Execute BEGIN statement

for(FILENAME in ALL_FILES)
{
        for($0 in ALL_RECORDS)
        {
                YOUR_CODE
        }
}

Execute END statement

So you just need something to save $0 in YOUR_CODE, and something to print it in the END statement.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 02-08-2017
If you just want to print the number of lines in a file, and you're unwilling to use:
Code:
wc -l < file

and have to use awk, the simple way to do it would be:
Code:
awk 'END{print NR}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk variable into shell command "date -d": possible...?

Hello, there! I am trying to pass an awk variable into a shell command in order to collect the result into an awk variable; in Bash it does work, as in: v='2'; date -d "now + $v weeks" But in awk it does not, as in: v="2" "date -d 'now + v weeks'" | getline newdate close ("date -d 'now... (3 Replies)
Discussion started by: fbird3
3 Replies

2. Shell Programming and Scripting

Variable interpolation in "print" of awk command

Hi, I have a snippet like below. Based on variable i, i wish to print 1,2,3,4,5th columns to Sample files. For each loop, one column from contetn and results will be pused to sample files. But i have a problem here i=1 while ; do `awk -F"\t" '{print $($i)}' $content > Sample_${i}_original`;... (4 Replies)
Discussion started by: forums123456
4 Replies

3. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

6. Shell Programming and Scripting

help for saving vertical datas to horizontal with "awk" or "cut"

hi, i have a file having datas like that ./a.txt 12344 12345 12346 12347 ..... ..... ... i want to save this datas to another file like that ./b.txt 12344 12345 12346 12347 ... ... ... i think awk can make this but how? :) waiting for ur help. (3 Replies)
Discussion started by: mercury
3 Replies

7. Shell Programming and Scripting

Sed - How to escape variable number of "/" (slash) ?

Hi, Can you tell me how to escape a variable number of slash characters in sed "/" ? In the script the code looks like this: cat $file_to_update | sed s/^$param/$param=$tab2*\#\*/1 And the $tab2 value is a path so it will have a number of "/" charracters. # cat db.cfg | sed... (4 Replies)
Discussion started by: majormark
4 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

9. Shell Programming and Scripting

How do "if [ $variable -eq "a number" ] ?

In particular I'm trying to check if a line contains an IP address and then do something with it. The file contains some lines that have no numbers, some blank lines, and some lines with only IP addresses. #!/bin/bash touch file2.txt cat file1.txt | \ while read line do if *.*.*.* ];... (4 Replies)
Discussion started by: earnstaf
4 Replies

10. UNIX for Dummies Questions & Answers

How to count number of occurrences of a "|" from a variable?

I have a variable, var="some1|some2|some3" I want to know how many "|" are in $var. When I say echo $var | grep -c '|' I am getting only 1 :confused: :confused: :confused: ? (4 Replies)
Discussion started by: jingi1234
4 Replies
Login or Register to Ask a Question