Hi
Im looking for a way, hopefully a one-liner to sort words in a line
e.g
"these are the words in a line"
to
"a are in line the these words"
Thanks!
Hi
Im looking for a way, hopefully a one-liner to sort words in a line
e.g
"these are the words in a line"
to
"a are in line the these words"
Thanks!
perl -e '{$_="these are the words in a line"; s/(.) (.) (.) (.) (.) (.) (.*)/$6 $2 $5 $7 $3 $1 $4/; print}'
> echo "these are the words in a line" | tr " " "\n" | sort | tr "\n" " " ;echo
a are in line the these words
That last echo command is to simply force a new line.
nice one. although my line will have more than 9 terms, seperated by commas
e.g
csoTopic=Topics: Artists Exact Match, queryTerm=new kids on the block, defaultDisplayName=New Kids On The Block, topic=vid
eos, artistName=New Kids On The Block, weight=50,
I think Im gonna have to go down the road of splitting them in a temp file , ordering them, and joining them up again.
edited... this was in response to jimmy_y's fine perl skills.
echo "these are the words in a line"|awk -F- '{print $6 $2 $5 $7 $3 $1 $4}'
echo "these are the words in a line"|sed 's/\(.\) \(.\) \(.\) \(.\) \(.\) \(.\) \(.*\)/\6 \2 \5 \7 \3 \1 \4/g'
from
I thought the OP wanted to sort the fields - not simply manually rearrange them!
Did not realize he wants to sort instead of manual arrange.
that's what the title said
$
$ echo "these are the words in a line"|perl -ne '@x=split/\s/; print join " ",sort @x'
a are in line the these words$
$
$
$ echo "csoTopic=Topics: Artists Exact Match, queryTerm=new kids on the block, defaultDisplayName=New Kids On The Block, topic=videos, artistName=New Kids On The Block, weight=50,"|perl -ne '@x=split/,\s*/; print join ", ",sort @x'
artistName=New Kids On The Block, csoTopic=Topics: Artists Exact Match, defaultDisplayName=New Kids On The Block, queryTerm=new kids on the block, topic=videos, weight=50$
$
tyler_durden
perl:
my @arr=split(" ", "these are the words in a line");
$,=" ";
print sort @arr;
You may consider this...
echo "these are the words in a line" | awk -v RS=' ' -v ORS="\n" '{print}' | sort | xargs
GNU awk
awk 'BEGIN{
w = "these are the ,words in a . line"
gsub(/[[:punct:]]| +/,"",w)
m=split(w,words," ")
b=asort(words, dest)
for(i=1;i<=b;i++) printf dest" "
}'
Thanks Joey, it worked like a dream
I split on the comma like this
echo $line | tr "," "\n" | sort | tr "\n" " "
Would there be a way to move one of the terms to the top of the list after sorting.
e.g. I always want the e parameter (e=5) to be at the top of the list.
before
d=4,e=5,a=1,b=2,c=3,f=6
after
e=5,a=1,b=2,c=3,d=4,f=6
if your $line has punctuations, it will not work
# echo "one two ,three four,five" | tr "," "\n" | sort | tr "\n" " "
I think it will be ok for me. I really only want to split by comma and then sort alpahbetically
between each comma are name value pairs and I only care about the first letter in the name.
Hi.
If you use Linux, there is a handy utility msort that can be used -- it allows one to specify the record separator:
#!/usr/bin/env bash
# @(#) s4 Demonstrate sort on words in string with msort.
echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) msort
set -o nounset
IN="these are the words in a string"
echo
echo " Input data line (colons as markers):"
echo ":$IN:"
echo
echo " Results:"
OUT=$( echo -n "$IN" | msort -q -n 1,1 -r " " )
echo ":$OUT:"
echo
echo " Input with comma separator:"
IN="queryTerm=new kids on the block,defaultDisplayName=New Kids On The Block,"
echo ":$IN:"
echo
echo " Results:"
OUT=$( echo -n "$IN" | msort -q -n 1,1 -r "," )
echo ":$OUT:"
exit 0
Producing:
% ./s4
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution : Debian GNU/Linux 5.0
GNU bash 3.2.39
msort - ( /usr/bin/msort Apr 24 2008 )
Input data line (colons as markers):
:these are the words in a string:
Results:
:a are in string the these words :
Input with comma separator:
:queryTerm=new kids on the block,defaultDisplayName=New Kids On The Block,:
Results:
:defaultDisplayName=New Kids On The Block,queryTerm=new kids on the block,:
Best wishes ... cheers, drl