I need to find in a file a list of number where last two digit end in a range

I all
I am tryng to find a way to sort a list of number in a file by the value of last two digit.

i have a list like this

313202320388
333202171199
373202164587
393202143736
323202132208
353201918107
343201887399
363201810249
333201805043
353201791691
323201765095
303201710295
393201663659
313201634149
32201583126
313201578038
333201575834
323201537847
333201467070
33201175281
345201143053
343201134806
373200967235

and I should have on output the numbers where last two digit end in the range 00 to 33

is there a way with awk sed grep or whatelse?
thank you!

Yes, of course there is a way... there is always "a way".

The "trick" is for you to try to write some code and experiment to learn how to do it.

So, please try to solve this on your own and post back your code and any error messages you got along the way.

:slight_smile:

awk 'length<12{print " " $0;next}1' listacos.txt | sort -k1.12 -k1.11

this is the way i found to sort correctly adding a space and justify any of the line... but not grepping the range....

Like this?

awk '{ last2=substr($NF,length($NF)-1); if (last2 <= 33) printf "%12s\n", $NF }' listacos.txt 
1 Like

wow!

seems this work much better than how i do! :slight_smile:

many thanks my friend!

How about:-

output_width=30                    # Adjust this to get sensible output
while read value
do
   printf "%${output_width}s\n" "${value}"
done < input_file | sort -nk 1.$[output_width-1]  |  tr -d " "

This forces it all to 30 (configurable at the top) characters wide (or numeric with %${output_width}d if you prefer) and then sorts on that. At the end in green, I have taken the liberty to strip the blanks out.

I hope that this option helps,
Robin

awk '/([0-2][0-9]|3[0-3])$/ {printf "%12s\n", $0}'

--- Post updated at 12:08 ---

column -t -R 1 file | egrep '([0-2][0-9]|3[0-3])$' | sort -k1.11,1.12

--- Post updated at 12:11 ---

that's how it will be cheaper

grep '[0-2][0-9]$\|3[0-3]$' file | column -t -R1 | sort -k1.11,1.12

Hi.

All in a single perl code: extraction of rightmost 2 digits, omit any containing lines greater than 33, numerically sort left-over lines based on right-most 2 characters:

#!/usr/bin/env perl

# @(#) p1       Demonstrate filtering, collecting, ordering.

use strict;
use warnings;

my (@a);
while (<>) {    # Eliminate numbers ending in > 33, collect rest
  chomp;
  next if substr( $_, -2, 2 ) > 33;
  push( @a, $_ );
}

# Sort numerically by right-most 2 digits
print join( "\n", sort { substr( $a, -2, 2 ) <=> substr( $b, -2, 2 ) } @a ),
  "\n";

exit(0);

producing:

343201134806
353201918107
323202132208
32201583126

On system like:

OS, ker|rel, machine: Linux, 3.16.0-7-amd64, x86_64
Distribution        : Debian 8.11 (jessie) 
perl 5.20.2

Best wishes ... cheers, drl

2 Likes