LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-24-2003, 12:54 PM   #1
brian0918
Member
 
Registered: Apr 2003
Posts: 87

Rep: Reputation: 15
Exclamation bash grep help


I have a file that may contain information such as this:

99 0.00012
100 0.00011
101 0.00010
102 0.00010
103 0.00009
104 0.00007


--------------------------------------------------------------------------------------------------------


sample text

more text




Besides this little chunk, the file contains tons of other information, and multiple iterations (some form of this chunk occurs multiple times in the file). What I need to do is, once the second column goes below 0.0001, I need the number in the first column (so in this case, I need to somehow grab the number 103)

NOTE: The number "0.00010" may appear more than once in any iteration, such as above, or it may not appear at all (skip from 0.00011 to 0.00009 or something)

In order to get near this info, I figured I should do something like:

grep "sample text" FILENAME -B 100

so that I could grab the 100 lines before each occurrence of "sample text" (which only occurs after these columns of numbers).

But, as far as grabbing the lines below .0001, I'm stumped.


Thanks.

Last edited by brian0918; 06-24-2003 at 01:10 PM.
 
Old 06-24-2003, 01:26 PM   #2
jvannucci
Member
 
Registered: Jan 2003
Location: Connecticut, US
Distribution: Red Hat 9.0
Posts: 98

Rep: Reputation: 15
Use perl.:

# perl -ne'/^\s*(\d+)\s(\d+\.\d+)/ and $2<0.0001 and print "$1\n";' FILENAME

You could use awk in a similar fashion.
 
Old 06-24-2003, 03:03 PM   #3
brian0918
Member
 
Registered: Apr 2003
Posts: 87

Original Poster
Rep: Reputation: 15
Thanks. How would I go about doing it with awk?

Last edited by brian0918; 06-24-2003 at 03:09 PM.
 
Old 06-24-2003, 03:05 PM   #4
Crashed_Again
Senior Member
 
Registered: Dec 2002
Location: Atlantic City, NJ
Distribution: Ubuntu & Arch
Posts: 3,503

Rep: Reputation: 57
Hey jvannucci, that command really scares me. Its very intimidating.
 
Old 06-24-2003, 03:14 PM   #5
brian0918
Member
 
Registered: Apr 2003
Posts: 87

Original Poster
Rep: Reputation: 15
The perl command doesn't seem to be working. Could someone please explain what the various characters mean?

Thanks.
 
Old 06-24-2003, 03:30 PM   #6
brian0918
Member
 
Registered: Apr 2003
Posts: 87

Original Poster
Rep: Reputation: 15
Ok, using a command similar to the one below....


grep "sample text" FILENAME -B 50 | grep "sample text" -v | grep "-" -v | awk '{if ($2 < 0.0001) print $1}' > newfile

I was able to get something like this in newfile:

123
124
125
126
127
128
129
130




124
125
126
127
128
129
130




122
123
124
125
126
127
128
129
130


and so on and so forth....


How can I just grab the first line from each of these groups of numbers and put it into a file?

Thanks.
 
Old 06-24-2003, 03:36 PM   #7
new_user10
Member
 
Registered: Jun 2003
Posts: 30

Rep: Reputation: 15
it's a regular expression matching pattern. you could try writing a short perl script to do it. here goes:

1) open a text file in an editor and name it parse.pl (or whatever)
2) type this:

#!/usr/bin/perl (or wherever perl is - do "whereis perl" to find it)

open(FILE, "file_path.txt"); (or whatever file path is)

while (<FILE>) {
if (/(\d*) (\d\.\d*)/) {
if ($2 < 0.0001) {
print "$1\n";
}
}
}


3) Save this file.
4) At terminal, type:

perl parse.pl

5) This will then print all the numbers (103, 104, etc) whose second number is less than 0.0001.

I hope that helps. I don't really understand the part about "grab the 100 lines before each occurence of 'sample text'"
 
Old 06-24-2003, 03:42 PM   #8
brian0918
Member
 
Registered: Apr 2003
Posts: 87

Original Poster
Rep: Reputation: 15
But, the only number I want is the first one that is below .0001 in each group of numbers. See my previous message.
 
Old 06-24-2003, 04:10 PM   #9
new_user10
Member
 
Registered: Jun 2003
Posts: 30

Rep: Reputation: 15
you could use a switch variable--for example,

$switch = 0;

...same code...
if ($2 < 0.0001 && $switch == 0) {
print "$1\n";
$switch = 1;
}

then reset the switch to 0 at the end of every block
 
Old 06-24-2003, 04:16 PM   #10
brian0918
Member
 
Registered: Apr 2003
Posts: 87

Original Poster
Rep: Reputation: 15
Thanks
 
  


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
$LINENO can't be modified in bash 3.0, while it can be in bash 2.05b Darwish Linux - Software 1 11-07-2005 03:57 PM
bash my little bash alaios Linux - Newbie 4 01-11-2005 12:59 AM
bash + html + javascript or just bash ? rblampain Programming 4 12-01-2004 08:53 AM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 07:42 PM
Bash Half_Elf Linux - General 2 05-01-2002 07:39 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:18 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration