Saving ranges using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Saving ranges using awk
# 1  
Old 12-21-2010
Saving ranges using awk

Hi All,

Is there a way to save a range in variable for later printing?

for example write somthing like this:
Code:
awk '
/pattern1/,/pattern2/{f=range}
/pattern3/{print f}
'

I don't know excatly what "range" could be but is there a way to do this?

Last edited by Franklin52; 12-21-2010 at 10:24 AM.. Reason: Please use code tags, thank you
# 2  
Old 12-21-2010
Code:
awk '
/pattern1/,/pattern2/{f?f=f RS $0:f=$0}
/pattern3/{print f}
' file

# 3  
Old 12-21-2010
something like this?

Quote:
awk '/pattern1/,/pattern2/ { f=f"\n"$0 } /pattern3/ { print f}' input_file
# 4  
Old 12-21-2010
Quote:
Originally Posted by scottn
Code:
awk '
/pattern1/,/pattern2/{f?f=f RS $0:f=$0}
/pattern3/{print f}
' file

a slight mod:
Code:
awk '
/pattern1/,/pattern2/{f?f=f RS $0:f=$0}
/pattern3/{print f;f=""}
' file

This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 12-21-2010
Quote:
Originally Posted by vgersh99
a slight mod:
Code:
awk '
/pattern1/,/pattern2/{f?f=f RS $0:f=$0}
/pattern3/{print f;f=""}
' file

to allow multiple range matches. Good idea Smilie
# 6  
Old 12-21-2010
Thanks alot for all your replies but when I tried the below one:
Code:
awk '
/pattern1/,/pattern2/{f?f=f RS $0:f=$0}
/pattern3/{print f;f=""}
' file

there was an error in the second line:
Code:
awk: syntax error near line 2
awk: illegal statement near line 2


Last edited by Scott; 12-21-2010 at 10:36 AM.. Reason: Code tags, please...
# 7  
Old 12-21-2010
Use nawk on Solaris.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk to get the maximum of a value in two different ranges

Dear Unix gurus, I have sample data organised like this and containing 6 columns (with headers): label c2 c3 c4 c5 c6 where c2 to c6 are numeric values in columns c2 to 6. I am trying to create a simple output in a new file containing 3 columns: label max(c2 c3) max(c4 c5 c6) ... (4 Replies)
Discussion started by: ksennin
4 Replies

2. Shell Programming and Scripting

awk working inside specific pattern ranges

Hi, I have a text file, which I am trying to parse. File contents: BEG Id Job1 Id Stage1 1 EN Id Job2 Id Stage2 BEG Id2 Job3 Id Stage4 2 EN I have to process the data in this between every BEG and EN. so I am trying to restrict the range and inside every... (1 Reply)
Discussion started by: Kulasekar
1 Replies

3. Shell Programming and Scripting

awk saving field of first file into array

Hello guys, I just start trying out AWK and encounter a problem, I try to think a bit but seems my way is incorrect. I have two input file, with the first file has only one field, the second file has 3 fields, I suppose to do stuffs to them by writing an awk program, kinda sort them out. Since I... (15 Replies)
Discussion started by: RozenKristal
15 Replies

4. Shell Programming and Scripting

Saving an AWK match

Is it possible to save the result of an AWK match to use later in a BASH script. Thanks, Jordon (4 Replies)
Discussion started by: jhirshon
4 Replies

5. Shell Programming and Scripting

Create ranges on a file with awk

Hello all, Let's say we have this file : $ cat inputfile 2 3 4 7 8 11 15 16 17 I'm trying to make a script with awk which gives as output the following : (4 Replies)
Discussion started by: rany1
4 Replies

6. Shell Programming and Scripting

subtracting 1.5 from column using awk and saving the changes

# foreach sub ( sub001 ) sub=sub001 cd /mnt/stor/smith/recog/$sub/event_files/timecorrected/ awk '{$1-1.5}' $sub_CR end What im trying to do is: 1. open a 1D file that consists of lists of integers in rows a columns 2. subtract 1.5 from each integer in the first column 3. save the file... (1 Reply)
Discussion started by: ac130pilot
1 Replies

7. UNIX for Dummies Questions & Answers

Awk ranges for selecting numbers

Hi, I am trying to use AWK to do some editing and formating of large tables of numbers and I am having trouble getting it to work. For brevities sake, I won't show the whole table, but I have a sample set of code: und$ awk '{($2+0) > 50;print $1}' temp 2000 147 2008 128 2002 100 1999 47... (2 Replies)
Discussion started by: ikerrin1@gmail.
2 Replies

8. Shell Programming and Scripting

Saving output from awk into a perl variable

How would I pass awk output to a perl variable? For example, I want to save the value in the 4th column into the variable called test. My best guess is something as follow, but I am sure this isn't correct. $test = system("awk '/NUMBER/{print \$4}' $_"); (8 Replies)
Discussion started by: userix
8 Replies

9. UNIX for Dummies Questions & Answers

Extracting lines and saving - awk

Hi All, I am trying to extract lines bsed on pattern matching../mp straight-flow/ Extracted output should be saved in meta_string , but the code is not working in that manner,saving repeated lines. can anyone please suggest where am i going wrong. /mp straight-flow/ {... (6 Replies)
Discussion started by: madhaviece
6 Replies

10. Shell Programming and Scripting

awk to print mon and max values of ranges

HI all I'm trying to write an awk script to print the min and max value in a range(s) contained in another file - the range values are in $2 EG 114,7964,1,y,y,n 114,7965,1,y,y,n 114,7966,1,y,y,n 114,7967,1,y,y,n 114,7969,1,y,y,n 114,7970,1,y,y,n 114,7971,1,y,y,n 114,7972,1,y,y,n... (3 Replies)
Discussion started by: Mudshark
3 Replies
Login or Register to Ask a Question