Awk extract a range of values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk extract a range of values
# 1  
Old 06-21-2010
Awk extract a range of values

Hi
Input
Code:
10    131
11    179
11    170
20    142
20    131
20    144
21    178
22    155
22    196
23    144
23    184
24    194
24    191
24    218
25    167
25    131
26    189

Output
Code:
11    179
11    170
23    144
23    184
24    194
24    191
24    218
25    167
25    131

I can try
Code:
awk '{if($1==11|$1==23|$1==24...){print $0}

But I am trying to see if there is a more elegant formula with substring selection.. even if the values many more than one would usually like to type out like
if ($1==11,23..25) Smilie
# 2  
Old 06-21-2010
Code:
awk ' $1 == 11 ; $1 == 23, $1 == 25 ' file

# 3  
Old 06-21-2010
Code:
egrep '^(11|23|24|25)[[:space:]]' infile

or e.g.
Code:
egrep '^(11|2[345])[[:space:]]' infile

# 4  
Old 06-21-2010
Hi Anbu23,
I am trying to figure out if I can do say 25 such values without having to do each time #1==value1; $2==value2 etc.
can I put a range say 21 thru 25 in the case above or say 101 thru 199 or 101 thru 150 and 152 thru 155?

---------- Post updated at 04:59 PM ---------- Previous update was at 04:45 PM ----------

I feel egrep may be inefficient when files are very large. (trying to run on actual data and is taking a bit of time)



---------- Post updated at 05:11 PM ---------- Previous update was at 04:59 PM ----------

Scrutinizer,
Thanks your code worked fine.
# 5  
Old 06-21-2010
Range syntax is not available in awk
This User Gave Thanks to anbu23 For This Post:
# 6  
Old 06-21-2010
If your ranges get bigger, then it as an alternative to egrep you can use anbu's solution provided your input file is numerically sorted on the first column, e.g.:
Code:
awk '$1 == 21, $1 == 25;  $1 == 101, $1 == 199 ' infile

Code:
awk '$1 == 101, $1 == 150;  $1 == 152, $1 == 155 ' infile



---------- Post updated at 23:20 ---------- Previous update was at 23:13 ----------

Or you can do this, without the sort prerequisite
Code:
awk '21<=$1 && $1<=25 || 101<=$1 && $1<=199 ' infile

This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 06-21-2010
Thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to match file1 and extract specific tag values

File2 is tab-delimeted and I am trying to use $2 in file1 (space delimeted) as a search term in file2. If it is found then the AF= in and the FDP= values from file2 are extracted and printed next to the file1 line. I commented the awk before I added the lines in bold the current output resulted. I... (7 Replies)
Discussion started by: cmccabe
7 Replies

2. Shell Programming and Scripting

Extract repetead values in date range

Gents, Using the following values: 3687650458 08/29/2017 1.67581 3687650388 08/29/2017 1.67581 3687650330 08/29/2017 1.67581 3687650330 08/29/2017 1.67581 3687650330 08/28/2017 2.67581 3687650330 08/28/2017 2.67581 3687650330 08/27/2017 3.67581 3687650330 08/27/2017 3.67581 3687650330... (6 Replies)
Discussion started by: jiam912
6 Replies

3. Shell Programming and Scripting

Create range of values and count values.

Gents, It is possible to generate a range of values according to column 1 and count the total of rows in the range. example input 15.3 15.5 15.8 15.9 16.0 16.1 16.8 17.0 17.5 18.0 output desired 15.0 - 15.9 = 4 (10 Replies)
Discussion started by: jiam912
10 Replies

4. Shell Programming and Scripting

awk to extract multiple values from file and add two additional fields

In the attached file I am trying to use awk to extract multiple values and create the tab-delimited desired output. In the output R_Index is a the sequential # and Pre_Enrichment is defaulted to .. I can extract from the values to the side of the keywords, but most are above and I can not... (2 Replies)
Discussion started by: cmccabe
2 Replies

5. Shell Programming and Scripting

Extract values in a line using awk

hello all, I need your help in extracting values of some parameter within a line using awk. for example: i have the below line available in a file and i want to extract the values of only CustomerId, s_PackageId and s_HZINumbers in order the result to be as ... (13 Replies)
Discussion started by: nael_najib
13 Replies

6. Shell Programming and Scripting

How to extract xml attribute values using awk inline.?

I am trying to extract specific XML attribute values for search pattern <factories.*baseQueueName' from resources.xml. my scripts works ok,, but to extract 3 values this code does echo $line three times, it could be 'n' times. How can I use awk to extract matching pattern values in-line or... (11 Replies)
Discussion started by: kchinnam
11 Replies

7. Shell Programming and Scripting

Convert Column Values to a Range of Values

I have a list of columns with values that I need to transform into a row containing the range of each column. For example: "Column A" 1 2 3 4 10 12 14 15 16 17 18 "Column B" 1 4 5 6 (4 Replies)
Discussion started by: newbio
4 Replies

8. Shell Programming and Scripting

AWK Sorting with range values

Hello, I am looking for some help on GAWK script. I have a list of phone numbers as below. I need to sort these in the range of first 6 digits. 2402170338 2402170387 2402170478 2402170744 2403100025 2403100026 2403100027 2403100028 So for the above sample data, I require an output... (9 Replies)
Discussion started by: Diwakar9
9 Replies

9. Shell Programming and Scripting

for each different entry in column 1 extract maximum values from column 2 in unix/awk

Hello, I have 2 columns (1st column has multiple entries but the corresponding values in the column 2 may be the same or different.) however I want to extract unique values for each entry in column 1 by assigning the max value from column 2 SDF4 -0.211654 SDF4 0.978068 ... (1 Reply)
Discussion started by: Diya123
1 Replies

10. Shell Programming and Scripting

To Create range of values

Hi, I have a file with the below like values with integers only in sorted order (May or may not be in sequence) Eg: File1.txt ----------- 1 2 3 4 5 6 . . . . . 10000 My requirement here is to create a range of values out put to a temp k (4 Replies)
Discussion started by: shiva447
4 Replies
Login or Register to Ask a Question