AWK - if last line/record do something


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK - if last line/record do something
# 1  
Old 06-05-2008
AWK - if last line/record do something

Hello:

I am trying to perform a certain action if the current record is the last line of the input file. But I am unable to figure out how to determine the last line of a file in awk.

I need to do something like this:
awk '{ if (lastline == NR) Do Something}' myfile.txt

I have tried the following in a Korn Shell script.

lastRec=`wc -l myfile.txt | awk '{print $1}'`
print $lastRec

awk -v aLastRec="$lastRec" '{if (NR == $aLastRec) print NR ": This is the last record!"}'


But I could not get it to recognize the $lastRec or $aLastRec.

Can you please help?

Thanks.
# 2  
Old 06-05-2008
You can use the END statement to access the last record of the file:

Code:
awk  'END{ print "this is the last line :  ", $0, "  | and NR  is -> " NR, "do something else here..." }' myfile.txt

And don't use $ sign inside awk:

Code:
lastRec=`awk 'END{print NR}' myfile.txt`

awk -v aLastRec="$lastRec" '{if (NR == aLastRec) ... }' myfile.txt

# 3  
Old 06-05-2008
Cool. I overlooked the $ error in awk part of the script. Your suggested solution works! Thanks a lot for your help Rubin.
# 4  
Old 06-06-2008
tail can do the job here
Code:
test `tail -1 data.file`=="pattern" && echo OK || echo NOK

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk multiple line record retrieves only one?

I have a file with a structure like this: Database sequence: some data Database position: number Query: identifier Location: number E-value: number 0 . : . : . STRINGSTRINGSTRINGSTRING |||||||||||||||||||||||||||| ... (5 Replies)
Discussion started by: bdeads
5 Replies

2. Shell Programming and Scripting

Record terminator on alternate line

Hello All, I have csv file like this 1,"suzi","305 Barehills","Vancover",34256 2,"molly","456 beverleyhills","Minisotta",234876 I want to put a customized record terminator on every alternate lines of this file. The file is a input to database and the record terminator is by default... (3 Replies)
Discussion started by: nnani
3 Replies

3. Shell Programming and Scripting

How to compare current record,with next and previous record in awk without using array?

Hi! all can any one tell me how to compare current record of column with next and previous record in awk without using array my case is like this input.txt 0 32 1 26 2 27 3 34 4 26 5 25 6 24 9 23 0 32 1 28 2 15 3 26 4 24 (7 Replies)
Discussion started by: Dona Clara
7 Replies

4. Shell Programming and Scripting

Print first and last line from multiline record

Hi - I'm new to working with multiline records and I'm going nuts trying to do something that seems simple. Input: Tue May 1 14:00 Header Record 1 is valid. Tue May 1 14:00 processing data to 25-Mar-2012 09:00:23.15 Tue May 1 14:03 Header Record 1 is valid. Tue May 1 14:03 processing data... (4 Replies)
Discussion started by: Catullus
4 Replies

5. Shell Programming and Scripting

Reject the record if the record in the next line does not begin with 2.

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten 2eleven 2twelve 1thirteen 2fourteen The output should be: (5 Replies)
Discussion started by: supchand
5 Replies

6. Shell Programming and Scripting

Reject the record if the record in the next line does not satisfy the pattern

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten The output should be 1one 2two 3three 1four 2five 3six (2 Replies)
Discussion started by: supchand
2 Replies

7. Shell Programming and Scripting

Get Nth record from last line

I have a growing file . I knew only last few lines which is constant for evey job run. I'd need to pull the Nth record from the last line. In this case i should not use search pattern. (2 Replies)
Discussion started by: ford2020
2 Replies

8. Shell Programming and Scripting

Finding a character in first line of a record

HI, I am pretty new to Unix scripting. I will need help in Finding a character in first line of a file or a set of files. The scenario is as follows: Lets consider a set of files which is having a character "ID"(without quotes) in the first line of each file.I need to find this character... (14 Replies)
Discussion started by: bsandeep_80
14 Replies

9. Filesystems, Disks and Memory

Showing an extra record/line

Hello everybody, I am working on ETL side. My job is to load the data from Oracle table to flat file and from flat file to oracle table using ETL tool Informatica. My flat files are fixed width. In the first phase, it is loading 66351 records into data file through tool. When I checked through... (1 Reply)
Discussion started by: srivsn
1 Replies

10. Shell Programming and Scripting

how to extract last line in record

Hi all!! After experiencing great helpfulness the last time I posted a problem at this site, I once again turn to the forum for expert help. The problem: I have a file.dat containing x, y, and z coordinates, like: x y z 1 1 4 1 2 3 1 3 9 2 1 7 2 2 2 2 3 8 3 1 ... (7 Replies)
Discussion started by: bjorb
7 Replies
Login or Register to Ask a Question