How would I rearange lines of text in unix? (eg, a b c would become c b a)

LordJezo

Banned
May 16, 2001
8,140
1
0
Say I have a text file with a bunch of lines, each line being divided into a bunch of different entries, like:

a b hejw 9393 glto

how would I get unix to rearange those lines in a specific order like:
b glto 9393 a hejw


I know it could be done simple with some array entries but I am unsure as how to do it exactly.
 

Haden

Senior member
Nov 21, 2001
578
0
0
Out of many things, you can use awk, I don't know how you want to order them, so example is very primitive:

cat /var/log/messages | awk '{print $3" "$2" "$1}'
would give you time date and month,
awk is for string manipulation, any string entering is separated word by word to $1...$N, $0 - whole line,
separator by default is space, can be changed thought.
Here is one of my awk scripts (totaly unrelated) but you might find it good example for syntax.

{
if ( NR==2 ) server=$2;
if ( ($1=="+" || $1=="-") && ($3!="4096") && ($3!="0") )
{
print "ftp://" server;
print $3;
for (i=4; i<=NF; i++) printf "%s ", $i;
printf "\n";
}
}