printing the first line in many files that meets a certain condition

I'd like to print the first line from each of many files in which a
particular field meets a certain condition.  For example, I'd like to
print the lines that, going from top, contain the first negative value
in column 2 (line 3 in the exmaple below):

1.5992E-02, 1.6482E-03
1.6306E-02, 7.1307E-04
1.6656E-02, -3.9010E-05
1.7031E-02, -6.4005E-04
1.7437E-02, -1.1658E-03

What if I wanted to print all the lines which contain the last
positive value in column 2?  I know I would have to evaluate and store
the previous value, but have not been able to write a working
script...

Can it be done using an awk one-liner?

--z.entropic
0
z
3/19/2009 12:32:24 PM
comp.lang.awk 3313 articles. 0 followers. Post Follow

7 Replies
283 Views

Similar Articles

[PageSpeed] 58
On Thursday 19 March 2009 13:32, z.entropic wrote:

> I'd like to print the first line from each of many files in which a
> particular field meets a certain condition.  For example, I'd like to
> print the lines that, going from top, contain the first negative value
> in column 2 (line 3 in the exmaple below):
> 
> 1.5992E-02, 1.6482E-03
> 1.6306E-02, 7.1307E-04
> 1.6656E-02, -3.9010E-05
> 1.7031E-02, -6.4005E-04
> 1.7437E-02, -1.1658E-03

awk '$2<0 {print;exit}'

> 
> What if I wanted to print all the lines which contain the last
> positive value in column 2?  I know I would have to evaluate and store
> the previous value, but have not been able to write a working
> script...

awk '$2>0 {lastpos=$0} END{print lastpos}'

0
pk
3/19/2009 1:06:56 PM
z.entropic <subPlanck@excite.com> wrote:
> I'd like to print the first line from each of many files in which a
> particular field meets a certain condition.  For example, I'd like to
> print the lines that, going from top, contain the first negative value
> in column 2 (line 3 in the exmaple below):
> 
> 1.5992E-02, 1.6482E-03
> 1.6306E-02, 7.1307E-04
> 1.6656E-02, -3.9010E-05
> 1.7031E-02, -6.4005E-04
> 1.7437E-02, -1.1658E-03

FNR == 1 { p = 0 }
p != 0   { next }
$2 < 0   { print $0 ; p = 1 }

> What if I wanted to print all the lines which contain the last
> positive value in column 2?  I know I would have to evaluate and store
> the previous value, but have not been able to write a working
> script...

FNR == 1 && h != "" { print h ; h = "" }
$2 > 0              { h = $0 }
END                 { if (h != "") print h }
0
dave
3/19/2009 4:56:09 PM
Dave Gibson <dave+news002@gibson-hrd.abelgratis.co.uk.invalid> wrote:
> z.entropic <subPlanck@excite.com> wrote:
[...]
>> What if I wanted to print all the lines which contain the last
>> positive value in column 2?  I know I would have to evaluate and store
>> the previous value, but have not been able to write a working
>> script...
> 
> FNR == 1 && h != "" { print h ; h = "" }
> $2 > 0              { h = $0 }

Correction: 0 is positive

  !($2 < 0)           { h = $0 }
> END                 { if (h != "") print h }
0
dave
3/19/2009 5:04:54 PM
On Mar 19, 9:06=A0am, pk <p...@pk.invalid> wrote:
> On Thursday 19 March 2009 13:32, z.entropic wrote:
>
> > I'd like to print the first line from each of many files in which a
> > particular field meets a certain condition. =A0For example, I'd like to
> > print the lines that, going from top, contain the first negative value
> > in column 2 (line 3 in the exmaple below):
>
> > 1.5992E-02, 1.6482E-03
> > 1.6306E-02, 7.1307E-04
> > 1.6656E-02, -3.9010E-05
> > 1.7031E-02, -6.4005E-04
> > 1.7437E-02, -1.1658E-03
>
> awk '$2<0 {print;exit}'
>
>
>
> > What if I wanted to print all the lines which contain the last
> > positive value in column 2? =A0I know I would have to evaluate and stor=
e
> > the previous value, but have not been able to write a working
> > script...
>
> awk '$2>0 {lastpos=3D$0} END{print lastpos}'

Thaks, but these expressions print only the first occurrence...  they
don't loop over many files.

--z.e.
0
z
3/19/2009 8:43:50 PM
On Thursday 19 March 2009 21:43, z.entropic wrote:

> On Mar 19, 9:06 am, pk <p...@pk.invalid> wrote:
>> On Thursday 19 March 2009 13:32, z.entropic wrote:
>>
>> > I'd like to print the first line from each of many files in which a
>> > particular field meets a certain condition.  For example, I'd like to
>> > print the lines that, going from top, contain the first negative value
>> > in column 2 (line 3 in the exmaple below):
>>
>> > 1.5992E-02, 1.6482E-03
>> > 1.6306E-02, 7.1307E-04
>> > 1.6656E-02, -3.9010E-05
>> > 1.7031E-02, -6.4005E-04
>> > 1.7437E-02, -1.1658E-03
>>
>> awk '$2<0 {print;exit}'

with GNU awk:

awk '$2<0 {print;nextfile}' path/to/files/*

otherwise you can use Dave Gibson's code.

>> > What if I wanted to print all the lines which contain the last
>> > positive value in column 2?  I know I would have to evaluate and store
>> > the previous value, but have not been able to write a working
>> > script...
>>
>> awk '$2>0 {lastpos=$0} END{print lastpos}'
> 
> Thaks, but these expressions print only the first occurrence...  they
> don't loop over many files.

awk 'FNR==1&&lastpos{print lastpos} $2>0 {lastpos=$0} END{print lastpos}' *

0
pk
3/19/2009 9:00:23 PM
On Mar 19, 12:56=A0pm, dave+news...@gibson-hrd.abelgratis.co.uk.invalid
(Dave Gibson) wrote:
> z.entropic <subPla...@excite.com> wrote:
> > I'd like to print the first line from each of many files in which a
> > particular field meets a certain condition. =A0For example, I'd like to
> > print the lines that, going from top, contain the first negative value
> > in column 2 (line 3 in the exmaple below):
>
> > 1.5992E-02, 1.6482E-03
> > 1.6306E-02, 7.1307E-04
> > 1.6656E-02, -3.9010E-05
> > 1.7031E-02, -6.4005E-04
> > 1.7437E-02, -1.1658E-03
>
> FNR =3D=3D 1 { p =3D 0 }
> p !=3D 0 =A0 { next }
> $2 < 0 =A0 { print $0 ; p =3D 1 }
>
> > What if I wanted to print all the lines which contain the last
> > positive value in column 2? =A0I know I would have to evaluate and stor=
e
> > the previous value, but have not been able to write a working
> > script...
>
> FNR =3D=3D 1 && h !=3D "" { print h ; h =3D "" }
> $2 > 0 =A0 =A0 =A0 =A0 =A0 =A0 =A0{ h =3D $0 }
> END =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 { if (h !=3D "") print h }

Thanks, that's almost it.  However, since I like one-liners, how do I
use it as a line command, not an awk script? That is, the following
works:

$ ls *.z|xargs -l awk -f fn.awk

where fn.awk is

FNR =3D=3D 1 { p =3D 0 }; p !=3D 0 { next }; NR > 10 && $6 < 0 { print
FILENAME, $0; p =3D 1}

but this does not as it prints only the first occurrence...

$ cat *.z | awk 'FNR =3D=3D 1 { p =3D 0 }; p !=3D 0 { next }; NR > 10 && $6=
 <
0 { print FILENAME, $0; p =3D 1 }'

--z.e.
0
z
3/19/2009 9:01:31 PM
z.entropic <subPlanck@excite.com> wrote:
[...]
> Thanks, that's almost it.  However, since I like one-liners, how do I
> use it as a line command, not an awk script? That is, the following
> works:
> 
> $ ls *.z|xargs -l awk -f fn.awk
> 
> where fn.awk is
> 
> FNR == 1 { p = 0 }; p != 0 { next }; NR > 10 && $6 < 0 { print
> FILENAME, $0; p = 1}

Either:

awk -f fn.awk *.z

Or insert something like this:

#! /usr/bin/awk -f

as the first line of the script (change the path to match whichever
awk you're using), then chmod 755 the script and:

fn.awk *.z

> but this does not as it prints only the first occurrence...
> 
> $ cat *.z | awk 'FNR == 1 { p = 0 }; p != 0 { next }; NR > 10 && $6 <
> 0 { print FILENAME, $0; p = 1 }'

Cat writes the contents of its arguments to standard output.  To let awk
see the filenames, use:

awk '...' *.z

Incidentally, if you want to skip the first ten lines of each input
file the "NR > 10" part of your script should be "FNR > 10".
0
dave
3/19/2009 9:47:02 PM
Reply:
Similar Artilces:

Send Command Line message
I have a program and this is the path to the program. c:\Program Files\NCH Swift Sound\IVM.exe Their manual says that it will accept the following command to add a number to the dialing list. IVM.exe -outbound 5551234567 -hide will add phone number 5551234567 to the list of outbound numbers and start dialling. The window will be hidden. I have created a text file and saved it as a batch file having FM 8.5 call the batch file but with no luck. I'm sure it is the way I have the batch file written. What would be the proper text in the batch file to make this work? Thanks JC Lewis May...

Re: OT: How to make a file transfer automatic?
Duck-Hye, Does SSH provide command language access? If so, you could either simply put that code in a .bat file, or run it from SAS (possibly with an X command), and then include the line for running the SAS code. 'Scheduled tasks' will simply run the code, whichever you choose, at designated intervals. I posted some code on the list, about a month or so ago, which showed how one can include code in a .bat file to test file dates and sizes. By using the same approach in running the task as a .bat file, you can set up a loop to continue the process until the file is actually replac...

business on line follow me !
hello: madam and sir . we sell all kinds of laptops and digital cam .our product is a quantity best, the price is the lowest in the world, i think you will be interested in our product . thanks a lot! Our Website:http://www.prs-123.com/ Msn: prs_cool_123@hotmail.com mail: prs_cool_123@yahoo.com.cn thanks for everyone good luck with everyone . @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ hello : madam and sir. we sell all kinds of laptops and digital cam .our product is a quantity best, the price is the lowest in the world, i think you will be interested in our product . thanks a lot! Our Websit...

Package `smtp'
I'm wondering, is it possible? Anyone tried it? -- Zbigniew ZB wrote: > I'm wondering, is it possible? Anyone tried it? Yes and yes. Did you check either wiki.tcl.tk or ActiveState's recipes for how to do it? -- +------------------------------------------------------------------------+ | Gerald W. Lester | |"The man who fights for his ideals is the man who is alive." - Cervantes| +------------------------------------------------------------------------+ ...

How to include more than 1 condition in an IF statement ??
hello am new here and new to SAS and the whole codes thing ... i have the following set of data: M 166 58 M 170 56 M 145 50 M 151 60 M 139 42 F 138 45 F 135 39 F 143 42 F 137 44 F 146 43 as SEX, HEIGHT, and WEIGHT and i want to create a character variable named CHARACTER, the following code worked very nicely with me: data Character; infile"C:\Documents and Settings\dalalh\My Documents\datab.txt"; input Sex $ Height Weight; if Height<145 then Character ="Short"; if Height>=145 then Character ="Tall"; Proc print; run; but i want to add other condition, ...

Generating dummy file handle
Hi, This is somewhat related to my previous post. My application loads a single archive file which contains a virtual file system. What I want to do is have the files be accessible within my application as if they really existed on the hard drive. I've started creating hooks for basic file i/o operations such as CreateFile,SetFilePointer, ReadFile, etc... These files will all be opened in read only mode. When CreateFile is called I first attempt to call the original function. If it fails then I check if the file exists in the archive. If it exists in the archive I want to retu...

SciPad 6.96.BP1 line numbering
Hi, first of all, many thanks for this new version of SciPad! I'm not sure I did something wrong while installing it, but I get some strange line numbering, like 1 function res=phi(V,p,use_median) if argn(2) < 3, use_median=%f, end 2 [nr,nc]= size(V) 3 .... Do I miss something or is there a patch for this? Many thanks, Helmut Jarausch Lehrstuhl fuer Numerische Mathematik RWTH - Aachen University D 52056 Aachen, Germany > first of all, many thanks for this new version of SciPad! You're welcome. Listening to users does not hurt. > I&#...

Compiler switches for directory in GNAT project files
I have a number of source files in a GNAT project which have been automatically generated. I want to compile these source files with different compiler switches from the rest of the project (e.g. less warnings). All of the automatically generated files are located in one single folder. Up until now I've specified them one by one in the project file: for Switches ("foo.adb") use ... However, the list of specified files have now grown well over a hundred. Is there a way to specify specific compiler switches for all files contained in a specific directory in GNAT project files? /...

How to print permutations?
If we have three variables a,b,c...which are char variables....then hoe to print there all permutations? for example....abc, bac, cab.......all of them.... Shraddha wrote: > If we have three variables a,b,c...which are char variables....then > hoe to print there all permutations? > for example....abc, bac, cab.......all of them.... > Are all of these questions school work or something? On 25 Maj, 14:44, Shraddha <shraddhajosh...@gmail.com> wrote: > If we have three variables a,b,c...which are char variables....then > hoe to print there all permutations? > for ex...

print math symbol into image created with GD
Hy! I am using the GD::Simple libs to draw a simple diagram. My problem is, that I have to put some math symbols in there (for example the symbol for delta and my). Is there an easy way to do that? Best regards, Kai Kai Schlamp schreef: > I am using the GD::Simple libs to draw a simple diagram. My problem > is, that I have to put some math symbols in there (for example the > symbol for delta and my). > Is there an easy way to do that? Yes, with a proper font. -- Affijn, Ruud "Gewoon is een tijger." Yep ... figured it out ... used a truetype font named symbol.tt...

Color Printing
Is there any other color printer I can use with the IIGS other than the Imagewriter II? I read through the FAQs and didn't see anything other than the Epson LQ mentioned. Is there anything else I can use? A Color Stylewriter, perhaps? Pat Patricia Glenn wrote: > Is there any other color printer I can use with the IIGS other than the > Imagewriter II? I read through the FAQs and didn't see anything other than > the Epson LQ mentioned. Is there anything else I can use? A Color > Stylewriter, perhaps? > > Pat I had my Epson Color Stylus 800 colour inkjet printe...

Uploading large files
Hi For a CMS admin tool I would like to provide a possibility to upload large files to a directory on the webserver. As HTTP uploads are limited in file size and also not comfortable in handling, I thought of an FTP solution. Displaying a link to ftp://mydomain.com/mydirectory is easy but limited to browsers who can act as FTP clients, such as Internet Explorer. When looking at PHP's FTP functions and googling for web-based FTP applications it looks like they all need an HTTP upload to get the data to the webserver which acts as an FTP client then. Java applets usually provide the...

file name with string
Hello! I need to append a string to a file name, but: fopen(fid,Results_%s,'w','',mystring) doesn't work. Could you give me any suggestions? Thank you. Carla Carla Cimatoribus: <SNIP down to syntax atrocity fopen(fid,Results_%s,'w','',mystring) one of the many solutions: fnam=sprintf('res_%s',mystr); fopen(fnam,'w'); us hi, same without function call: fopen(fid,['Results_' mystring],'w') J�r�me. "Carla Cimatoribus" <carla@ist.uni-stuttgart.de> wrote in message news:eef63f1.-1@web...

read file and print contents
hello, im new to ruby and i have a text file and want to read in the file and print it out. so far iv got the following. I'd greatly appreciate any help. thanks. text file (reference.txt): Tag: ref1 Type: Book Author: Little, S R ruby file: #!/usr/local/bin/ruby # # # read file and print # ARGV.each do |fn| begin (fn == '-' ? STDIN : File.open(fn)).each_line do |l| if $indent > 0 (1..$indent).each { print ' ' } end puts l end -- Posted via http://www.ruby-forum.com/. I would change if $indent > 0 (1..$indent).each { print ...

Re: help with print/page #3
tomarsin2015@comcast.net wrote on 03/03/2006 12:28:27 PM: > Thanks for replies. > AFRICA> sqf laserjet5l > Printer queue LASERJET5l, idle, on AFRICA::"10.120.201.200:9100", > mounted form DEFAULT > <laserjet 5l> > /BASE_PRIORITY=4 /DEFAULT=(FEED,FORM=DEFAULT) Lowercase > /OWNER=[SYSTEM] > /PROCESSOR=TCPIP$TELNETSYM /PROTECTION=(S:M,O:D,G:R,W:S) > > Entry Jobname Username Blocks Status > ----- ------- -------- ------ ------ > 103 TAPE0909 SYSTEM 3139 Holding > Submitt...

PyExcelerator: How does one close a file?
I use PyExcelerator to write data into Excel files, and repeatedly call the function writeData with different filenames fn from my main routine. Unfortunately, only one file - the last one - is created. The problem seems to lie with the fact that I do not close the existing file before calling the function again, and the existing file is therefore just saved with a new name. Is there a way to close a PyExcelerator Excel file so that each call to the function results in a new file? There is no close() method as far as I can tell. def writeData(fn, Data): """Write the data in...

NYC LOCAL: Wednesday 30 July 2003 Jobs and Procurement Meeting at Baruch: Gale A. Brewer Chair of the New York City Council's Select Committee on Technology in Government
<blockquote from="official WWWAC announcement" edit-level="zero"> Subject: [wwwac] WWWAC Supported Event: Jobs & Procurement: Where is the tech work? - 7/30 (fwd) Date: Wed, 23 Jul 2003 16:56:18 -0400 From: Bob Frankel <rfrankel2@nyc.rr.com> To: wwwac@lists.wwwac.org Subject: [wwwac] WWWAC Supported Event: Jobs & Procurement: Where is the tech work? - 7/30 Trying to expound on the relevance of this event would be the equivalent of waxing eloquent on what color is the white horse. Many in our community need jobs, clients, better jo...

Awk
I have a log file with lines like these... 2012-10-08 00:00:05,005 INFO [main] [com.ibm.stats.StatsCollector] STAT TPS PARAM1 990 PARAM2 685 PARAM3 0 total 1675 2012-10-08 00:01:05,005 INFO [main] [com.ibm.stats.StatsCollector] STAT TPS PARAM1 995 PARAM2 678 PARAM3 1 total 1673 Where the PARAM rows are more than 3 and there are more columns to each param row. Is there a way to treat the date line (2012...) and the total line as beginning and end of a record? How would I address each separate field (column) in the param lines? Thanks! On Frida...

i need a batch file to move folders
i tried to make a batch file to move subfolders and their contents from within a folder in one drive to another folder to another drive but it didn't seem to work... i want to run the file at scheduled times... using the windows scheduler in start | accessories can someone please tell me how to write this simple script to move subfolders... or does ms-dos only move files rather than folders... thanks Mike Henley wrote: >i tried to make a batch file to move subfolders and their contents >from within a folder in one drive to another folder to another drive >but it d...

I wanna to print two copies of a print job from two different trays
Hi , I have a very critical printing incidence, At one of my customer's premises, we need to print two copies of every invoice, and we need to direct each copy to different tray of the HP 4250tn printer. Can your know a software that has a feature to avail it? Please guide ASAP IF YOU HAVE TO POST THAT YOU GOOTA BE REAL DUMB mannubhai wrote: >Hi , >I have a very critical printing incidence, >At one of my customer's premises, we need to print two copies of >every invoice, and we need to direct each copy to different tray of the > >HP 4250tn pri...

Re: output question--one long row into many rows #10
Thanks=2C Nat! D.T. > Date: Tue=2C 12 May 2009 15:00:36 -0400 > From: Nathaniel.Wooding@DOM.COM > Subject: Re: [SAS-L] output question--one long row into many rows > To: SAS-L@LISTSERV.UGA.EDU >=20 > dt >=20 > Try something like this >=20 > Data =3B > set wanted=3B > file print=3B > array able _numeric_=3B >=20 > do i =3D 1 to dim(able) by 2=3B > v1 =3D able{i} =3B > v2 =3D able{i+1}=3B > output=3B > end=3B > keep v1 v2=3B * use whatever variable names you like=3B > run=3B >=20 > Nat Wo...

Convert image to binary file
Hi. I'm trying to use Matlab to convert a black and white image into a sting of bits which can then be used as the input to an artificial neural network. I have the code below, but when I execute it the output file is blank. BW = imread('image1.bmp'); fid = fopen('exp.txt', 'w'); count = fwrite(fid, BW); fclose(fid); Does anyone know what I am doing wrong? I need the matrix to be read line by line into a string of bits with a space between each bit. e.g. for a 2*2 image with a white pixel in the top left hand corner the output should be 1 0 0 0. Thanks, ...

Transferring files from MAC to Win XP
How can I transfer files from MAC 8.5.1 to Win XP using FMP 4.1. The media available on the MAC end is a usb zip drive, although I would like to use a usb flash drive but the MAC doesn't accommodate it. We have Filemaker pro 4.1 loaded on both systems. Any suggestions to accomplish this would be appreciated. Ozzie Matics wrote: > How can I transfer files from MAC 8.5.1 to Win XP using FMP 4.1. The media > available on the MAC end is a usb zip drive, although I would like to use a > usb flash drive but the MAC doesn't accommodate it. We have Filemaker pro > 4.1 ...

MP4 Files
Is there any RISC OS application that can play MP4 files? Peter In message <4d65b816d4Peter@nildram.co.uk> Peter Dunkley <Peter@nildram.co.uk> wrote: > Is there any RISC OS application that can play MP4 files? Cineroma is claimed to support MP4, but David McEwen has not announced a release date yet. Apart from that: No. The only other candidate, KinoAmp, only supports MPEG1/2 video stream files. Martin -- --------------------------------------------------------------------- Martin Wuerthner MW Software http://www.mw-software.com/ spamtrap@mw-soft...

cvs file write bug
Casually, I had devel/cvs installed on my system. Therefore audit-packages told me about the file-write-bug in cvs<1.11.10 [1] But for those who did not install devel/cvs (this is likely because of /usr/bin/cvs being in base system) there should be a security advisory on netbsd.org, I think. The base system of NetBSD 1.6.2_RC3 contains cvs-1.11.5. This versions seems to be affected. Was it wrong to notify security-alert (at) netbsd.org about that? Regards, Heiko [1] http://ccvs.cvshome.org/servlets/NewsItemView?newsID=84 ...