Computing.Net > Forums > Unix > sort single line input

sort single line input

Reply to Message Icon

Original Message
Name: Guy
Date: November 2, 2001 at 02:45:21 Pacific
Subject: sort single line input
Comment:

i would like to sort a single line input with a script
input=$( echo "$@" | sort -ngr )
echo $input
but it not work


Report Offensive Message For Removal

Response Number 1
Name: James Boothe
Date: November 2, 2001 at 06:51:38 Pacific
Subject: sort single line input
Reply: (edit)

First of all Guy, if you want to nest commands in the middle of other commands, the nested commands need to be delimited with backward quotes, such as:

echo I am `whoami`, logged into `uname -n`

Secondly, sort command is for sorting multiple lines, not for sorting words on a single line. One approach is to pipe these words into awk to output each word on separate lines, which can then be sorted:

input=`echo $@ |
awk '{for(w=1;w<=NF;w++)print $w}' |
sort`
echo $input

And $input ends up as a single line because the newline characters get stripped.

James


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: sort single line input

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software