LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 10-14-2009, 01:50 PM   #1
maxxum600si
LQ Newbie
 
Registered: Oct 2009
Posts: 3

Rep: Reputation: 0
Cool Awk question (How to print a line other than the first or the last line)


I have an awk question. Below is an awk code in my ksh script. It searches a file called wdA for the fields that have NAME and prints out field $5, but only saves the last line of those that are found in a file called labelA.

How do I set this up to plot the 'middle' line instead of the last line? I can easily save the first or last line, but I want approximately the middle line. Example, A file has 10 lines & I'd like the code to save the ~5th line. My problem is that the file will NOT always have a fixed number of lines and can sometimes have odd or even lines.


Code:
awk /NAME/'{print $5}' wdA   |  awk ' {line=$0} END {print line}' >> labelA
Thanks!
Maxxum600si
 
Old 10-14-2009, 11:02 PM   #2
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,266

Rep: Reputation: 99
Hi,

Could you please post the contents of your file wdA too. Paste few lines if it is a very long file.
 
Old 10-15-2009, 07:14 AM   #3
maxxum600si
LQ Newbie
 
Registered: Oct 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Vikas027,
Thanks for the reply. Sorry I forgot to show the few lines of the file wdA. Also there was a typo in the my previous post (corrected here).

Code:
awk '/NAME/ {print $5}' wdA   |  awk ' {line=$0} END {print line}' >> labelA

Few lines of file wdA:
101 777 2009 DD NAME
172 788 2009 JJ NAME
151 655 2009 SS NAME
111 222 2009 VV POSE
142 101 2009 DD POSE
255 450 2009 JJ NAME
<snip>
 
Old 10-15-2009, 08:23 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
You want to print the line nearest the middle of the file that has NAME for the 5th field? How big are these files (meaning are you looking for an efficient solution or a simple solution?
 
Old 10-15-2009, 08:47 AM   #5
maxxum600si
LQ Newbie
 
Registered: Oct 2009
Posts: 3

Original Poster
Rep: Reputation: 0
catkin,
Thanks for the reply. Yes I'd like to print the line nearest the middle of the file that has NAME for the 5th field. If you have an efficient solution that's not too far from the simple, then I'd love the efficient one. File wdA varies from 300 to 1000 lines.

maxxum600si
 
Old 10-15-2009, 11:48 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
OK. Efficient makes it a little harder.

Presuming you are running the awk from a shellscript, use wc -l to count the number of lines in the input file. Pass that information to awk using the -v var=val command line option.

What follows is pseudo-code to describe the algorithm. If you have problems translating it into awk, please ask.

In the awk program's BEGIN section initialise variables:
  • middle_NR to (integer division) half of the number of lines in the file (as passed in using -v var=val).
  • offset to the number of lines in the file. This is an impossibly large value so that any "proper" setting is sure to be smaller.
In the awk program's pattern match section, match lines with with NAME in the 5th field and when you find it:
Code:
if middle_NR > NR  /* We are before the middle -- there may be a closer match to come */
    offset = middle_NR - NR /* Being before the middle, this must be the closest */
    line = $0
else if middle_NR == NR  /* Bingo! */
    line = $0
    exit
else /* We are past the middle so the line we want is the closest of this or the last one */ 
    if (NR - middle_NR < offset) /* This is the one we are looking for */
        line = $0
    exit
In the awk program's END section print an error message if variable "line" is empty (there was no match NAME in the 5th field) or print variable "line".

Last edited by catkin; 10-15-2009 at 11:50 AM. Reason: In search of consistency
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
how to replace line of file with another line using awk in shell script amit_pansuria Programming 3 03-29-2009 09:43 AM
how to read input line by line (awk) slcalice Programming 4 03-09-2009 02:12 PM
grab the line below a blank line and the line above the next blank line awk or perl? Pantomime Linux - General 7 06-26-2008 08:13 AM
awk Question? Search by line, using 2 files? micksul Linux - Software 4 06-06-2007 06:34 AM
sed / awk command to print line number as column? johnpaulodonnell Linux - Newbie 2 01-22-2007 07:07 AM


All times are GMT -5. The time now is 05:56 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration