AWK Sorting with range values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK Sorting with range values
# 8  
Old 10-20-2012
Quote:
Originally Posted by alister
... ... ...

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
In section 5.2.1 of the C Standard titled Character Sets it says:
Quote:
In both the source and execution basic character sets, the
value of each character after 0 in the above list of decimal
digits shall be one greater than the value of the previous.
In the POSIX Standards, XBD section 6.1 titled Portable Character Set places this requirement from the C Standard on all locales supported by a system conforming to the POSIX Standards or to the more stringent requirements of the Single UNIX Specification.

So, the -n isn't needed in sort when all of the strings of digits being sorted are all the same length.

Although it doesn't matter in this case, note that the same can't be said for uppercase letters nor lowercase letters. Adding 0 through 26 to 'a' ('A') to get the lowercase (uppercase) letters in sequence happens to work in supersets of ASCII, but it won't work in EBCDIC. There are gaps between 'i' and 'j' ('I' and 'J') and between 'r' and 's' ('R' and 'S') in EBCDIC.
This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 10-20-2012
Quote:
Originally Posted by alister
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).
Yes... But after fixing missing new line, length got reduced by one(still behind you and elixir..Smilie)
Now 84..Smilie

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

And with this 83..
actually i m not getting what i can call this, may be cheating/Not good way of programming but it also helps to reduce one count.Smilie
Still few more required..Smilie

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

# 10  
Old 10-22-2012
Hello Don, your solution worked very nicely. Thanks for precise code.
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