Create ranges on a file with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create ranges on a file with awk
# 1  
Old 05-10-2010
Create ranges on a file with awk

Hello all,

Let's say we have this file :

Code:
$ 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 :

Code:
 
Range1   2    4
Range2   7    8
Range3   11  11
Range4   15  17


Which means for every successive integers, we print only the first & the last of the range and by incrementing the range number.

Could someone help please... I'm trying to do it without success.
I seached on the forum and didn't find something similar.

Thx in advance,
# 2  
Old 05-10-2010
Code:
nawk '
    FNR==1{s=prev=$1;next}
    $1==(prev+1){prev=$1;next}
    {print "Range" ++r, s,prev;s=prev=$1}
    END {print "Range" ++r, s,prev}' inputFile

# 3  
Old 05-10-2010
Something like that ?
Code:
awk '
function printRange() {
   if (rangeCount) {
      printf "Range%d\t%d\t%d\n", rangeCount, rangeFrom, rangeTo;
   }
}
function newRange(n) {
   rangeCount++
   rangeFrom = rangeTo = n;
}
{
   if (rangeCount && $1 == (rangeTo+1)) {
      rangeTo = $1
   } else {
      printRange();
      newRange($1);
   }
}
END { printRange() }
' inputfile

Jean-Pierre.
# 4  
Old 05-10-2010
And another approach:
Code:
awk 'NR==1{p=$0; s="Range"++c FS $0; next}
$0-p==1{p=$0;next}
{print s FS p;p=$0; s="Range"++c FS $0}
END{print s FS p}
' file

# 5  
Old 05-10-2010
Ok, thx a lot for all ...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting a text file into smaller files with awk, how to create a different name for each new file

Hello, I have some large text files that look like, putrescine Mrv1583 01041713302D 6 5 0 0 0 0 999 V2000 2.0928 -0.2063 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 5.6650 0.2063 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 3.5217 ... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

2. 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

3. Shell Programming and Scripting

awk parsing file to create a database

Hi Guys, I have a list a hotels stored in many different text files. This list is kept in the following format: 20/03 Hotel: The Bear Hotel Honey Street Woodstock UK Tel:+44-xxxxxx Rate: 100 21/03 Hotel: The Bush Hotel Nice Street Farnham (4 Replies)
Discussion started by: freddie50
4 Replies

4. 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

5. Shell Programming and Scripting

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: 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? (8 Replies)
Discussion started by: ghoda2_10
8 Replies

6. Shell Programming and Scripting

How to create a file using awk

Hi there to the community, i have a bash script with an awk command inside, the awk program is in a separate file cause it's a bit big. Inside the awk program i calculate a filename from some data and i want to create a new file with this name into a directory whose path is passed in awk. Etc.... (4 Replies)
Discussion started by: beatblaster666
4 Replies

7. Shell Programming and Scripting

Select some lines from a txt file and create a new file with awk

Hi there, I have a text file with several colums separated by "|;#" I need to search the file extracting all columns starting with the value of "1" or "2" saving in a separate file just the first 7 columns of each row maching the criteria, with replacement of the saparators in the nearly created... (4 Replies)
Discussion started by: capnino
4 Replies

8. Shell Programming and Scripting

Read a file and search a value in another file create third file using AWK

Hi, I have two files with the format shown below. I need to read first field(value before comma) from file 1 and search for a record in file 2 that has the same value in the field "KEY=" and write the complete record of file 2 with corresponding field 2 of the first file in to result file. ... (11 Replies)
Discussion started by: King Kalyan
11 Replies

9. 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

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