LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-09-2010, 08:25 AM   #1
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
How to run code on a certain line number in AWK


I want to be able to run a piece of AWK code on a record specified by number, not by a regex.

This is my current solution, but i think there must be a better way:

Code:
{
        if (NR == 3)
        {
                # do stuff
        }
}
Is it possible, and how is it done?
 
Old 05-09-2010, 09:03 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Code:
 NR == 3 { # do stuff }
 
1 members found this post helpful.
Old 05-09-2010, 09:14 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
As ntubski said! Take in mind that rules in awk have the following format
Code:
pattern { action }
where pattern can be a regular expression or a comparison expression. If the pattern is not specified, the action is performed on every line (record) of the input file. If the action is not specified, the default one (print $0) is applied.
 
1 members found this post helpful.
Old 05-09-2010, 09:37 AM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
And will this work for a range of records?:

Code:
(NR >= 3 && NR <= 8) { print }
EDIT: I tried it and it does work.

Last edited by MTK358; 05-09-2010 at 09:40 AM.
 
Old 05-09-2010, 09:40 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Yes! And parentheses are not necessary:
Code:
NR >= 3 && NR <= 8 { action }
 
1 members found this post helpful.
Old 05-09-2010, 09:41 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I didn't know that you can put something other than a regex to match.

Is it possible to match something not even related to the current record?
 
Old 05-09-2010, 09:50 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by MTK358 View Post
Is it possible to match something not even related to the current record?
Well, not really. Built-in variables as NR, NF, and so on are related to the current record. Regular expression are matched against the current record. However you can test for variables that have been assigned before, for example:
Code:
/regexp/ { count++ }
count >= 10 { action }
In this poor example, the variable count is incremented every time a record matches the regexp, whereas the second rule is executed only after count has reached a value of 10. In this case the answer is yes: the value of count is not strictly related to the current record.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Create File Listing in C++ that will generate a line number on every line of code marzrocks Programming 11 04-12-2010 06:10 AM
[SOLVED] Help with awk finding line number and deleteing text puth Programming 1 02-24-2010 11:29 AM
how to replace line of file with another line using awk in shell script amit_pansuria Programming 3 03-29-2009 09:43 AM
sed / awk command to print line number as column? johnpaulodonnell Linux - Newbie 2 01-22-2007 07:07 AM
C program code to run a Linux command line Linh Programming 10 06-11-2003 01:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration