rany1
1
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 :
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,
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
Something like that ?
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.
And another approach:
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
rany1
5
Ok, thx a lot for all ...