1849950 Members
3304 Online
104049 Solutions
New Discussion

Re: unix scripting

 
SOLVED
Go to solution
Kerilyn O'Donnell
Contributor

unix scripting

I left my scripting book at work and need a quick script to read in my text file and put the numbers in order which aren't necessarily at the beginning of the line. For instance, the text file looks like this:

Me9999ne
dog189
Horse8454

I want the new file to look like this:
Dog189
Horse8454
Me9999

Can someone please help me out here?

Thanks
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor

Re: unix scripting

Hi:

If the numeric portion of the record (line) is separated from the alphabetic string, then treat it as the sort key and sort. For instance if the field separator is a blank and the numbers are the second field you could code:

# sort -k 2nb

Take a look at the man pages for 'sort' for more details.

...JRF...
vtpaulson
Frequent Advisor

Re: unix scripting

Hi,

Use sort command to do this task...

cat filename | sort

Regards,

Paulson
James R. Ferguson
Acclaimed Contributor

Re: unix scripting

Hi:

Oops, forgot to show the input/output to the sort:

# sort -k 2nb /tmp/input > /tmp/output

...JRF...
Robin Wakefield
Honored Contributor
Solution

Re: unix scripting

sed 's/[0-9]/ &/' {file} | sort -k 2nb | sed 's/ //'

will give you the blank before the 1st number, sort it, then remove the blank.

Robin.