AWK Sorting with range values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK Sorting with range values
# 1  
Old 10-19-2012
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.

Code:
2402170338
2402170387
2402170478
2402170744
2403100025
2403100026
2403100027
2403100028

So for the above sample data, I require an output as below:

Code:
240217	0338	0744
240310	0025	0028

The pattern is:
<first six digits> <range start> <range end>

Please suggest a solution for this.

Last edited by Scrutinizer; 10-20-2012 at 09:47 AM.. Reason: code tags
# 2  
Old 10-19-2012
Code:
 
sort -n infile | awk '
{
  n=substr($0,1,6);
  r=substr($0,7);
  if (!a[n]++) {
    p[pc++]=n;
    l[n]=9999999;
    h[n]=-9999999;
  }
  r < l[n] ? l[n]=r : 0;
  r > h[n] ? h[n]=r : 0;
}
END {
  for (i=0; i<pc; i++) print p[i], l[p[i]], h[p[i]];
}'

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 10-19-2012
The script provided by rdrtx1 can be simplified since we know the input is sorted numerically by the time awk sees it:
Code:
sort -n infile | awk '
{       if(last != substr($0, 1, 6)) {
                if(last) print last, low, high
                last = substr($0, 1, 6)
                low = substr($0, 7)
        }
        high = substr($0, 7)
}
END {   if(last) print last, low, high}'

If you know that your input file will never be an empty file, you can omit if(last) from the last line of the script.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 10-20-2012
I don't recommend the following over Don's suggestion. My approach is less efficient and less maintainable. I offer it only for your amusement, as my attempt at a shortest solution which restricts itself to POSIX-standard utilities.
Code:
sort -n infile | sed 's/./& /6' | awk '$1""!=n {print _; n=$1} 1' | awk '{print $1,$2,$NF}' RS=

Note: The pair of double-quotes after $1 shouldn't be necessary, but the oooold mawk that I was testing with wouldn't treat $1 as a string (which current POSIX rules require in that context).

If anyone can conjure something shorter, I'd love to see it. Smilie

Regards,
Alister
# 5  
Old 10-20-2012
Dropping the sed (and consequently, a process and making it just a little shorter Smilie):
Code:
sort -n infile|awk '{sub(/.{6}/,"& ")} $1!=n{print _; n=$1}1'|awk '{print $1,$2,$NF}' RS=

I have dropped the double-quotes around $1 as the OP seems to be using gawk, which I think will use string comparison in that case.

Last edited by elixir_sinari; 10-20-2012 at 06:12 AM..
# 6  
Old 10-20-2012
My try on it.....Smilie

Code:
sort -n file | sed 's/./& /6' | awk '!X[$1]++{printf b?" "b"\n"$0:$0}{b=$2}END{printf " "b}'

# 7  
Old 10-20-2012
Quote:
Originally Posted by elixir_sinari
Dropping the sed (and consequently, a process and making it just a little shorter Smilie):
Nope. Your suggestion is actually longer. It may be more efficient without the extra process in the pipeline, but it uses more characters.

Quote:
Originally Posted by pamu
My try on it.....Smilie
Nice try, pamu, but yours is even a bit longer than elixir's. Also, your suggestion does not output a valid text file (it's missing the final newline).

Removing unnecessary whitespace and using filename "f":
Code:
alister: 79
sort -n f|sed 's/./& /6'|awk '$1!=n{print _;n=$1}1'|awk '{print $1,$2,$NF}' RS=

elixir: 82
sort -n f|awk '{sub(/.{6}/,"& ")}$1!=n{print _;n=$1}1'|awk '{print $1,$2,$NF}' RS=

pamu: 85 (but not quite a correct solution)
sort -n f|sed 's/./& /6'|awk '!X[$1]++{printf b?" "b"\n"$0:$0}{b=$2}END{printf " "b}'

While I've not removed it, I believe there is no need to use sort's -n option. Unless there exists a locale in which the digits do not sort from 0 to 9 -- if such a locale exists, I would truly appreciate being made of aware of it -- if the numbers are the same length, lexicographical and numerical sorting will yield identical results.

Regards,
Alister

Last edited by alister; 10-20-2012 at 12:26 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Find values within range and output

Dear All, I am stacked and I ask for your help. Briefly, I have two files, one like this one (file1): 1 101 5 1 102 6 1 103 2 1 104 9 1 105 10 2 301 89 2 302 4 2 303 13 2 304 34 2 305 1 and the other like this one (file2): 1 103 2 303well, what I am trying to do is obtain a... (2 Replies)
Discussion started by: giuliangiuseppe
2 Replies

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

4. Shell Programming and Scripting

Help with awk sorting with different values

Hello, I have a file as follows: BTA Pos KLD 4 79.7011 5.7711028907 4 79.6231 5.7083918219 5 20.9112 4.5559494707 5 58.0002 3.4423546273 6 38.2569 4.7108176788 6 18.3889 7.3631759258 (1 Reply)
Discussion started by: Homa
1 Replies

5. Programming

grepping a range of values

I need to return all records in a file starting with a row that says TABLE: <tabl name> lists of hexadecimal records TABLE: <some table> TABLe is a key word in the file. I know the name of the table I want to start with. I do not know the name of the table that I will end with. I just... (4 Replies)
Discussion started by: guessingo
4 Replies

6. Programming

Finding range of values in an array

I have an array containing distances in ascending order, for example: distances = 100 120 150 170 200 280 300 .... I have a number, let's say v = 170 and a variation value, let's say var = 100 . I want to return the array indexes for which the distances cover the range (v - var) to (v +... (3 Replies)
Discussion started by: kristinu
3 Replies

7. Shell Programming and Scripting

Awk extract a range of values

Hi Input 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 (6 Replies)
Discussion started by: genehunter
6 Replies

8. Shell Programming and Scripting

sorting null values

Hi I have a file with the values abc res set kls lmn ops i want to sort this file with the null values at the bottom of the file OUTPUT should look like this abc kls lmn ops (6 Replies)
Discussion started by: vickyhere
6 Replies

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

10. UNIX for Dummies Questions & Answers

How to Display a range of values in the output of cat

When I use this command I get an output of some numbers cat ac.20070511 | cut -d" " -f19 Is there any way for me to display only the numbers that are greater than 1000 but not all the numbers in the ouput. Can any one help me with this. :) (8 Replies)
Discussion started by: venu_nbk
8 Replies
Login or Register to Ask a Question