Hi,
I could not find this anywhere and I am wondering if someone knows a quick way of doing this.
So heres the problem... I have a row that looks like this (an example):
5 4 3 2 1
What I want to do is reverse it so it looks like this:
1 2 3 4 5
Does anyone know the simple unix command for this?
thanks
One way to do it:
$
$ cat input.txt
5 4 3 2 1
$
$ perl -ne 'chomp; print scalar reverse, "\n"' input.txt
1 2 3 4 5
$
tyler_durden
# echo "5 4 3 2 1" | awk 'BEGIN{FS=""}{for(i=NF;i>0;i--){printf $i}}'
1 2 3 4 5
clx
4
echo "5 4 3 2 1" | rev
1 2 3 4 5
Not a good approach but works
$
echo "5 4 3 2 1" | sed 's/\([^ ]*\) \([^ *]\) \([^ ]*\) \([^ ]*\) \([^ ]*\)/\
5 \4 \3 \2 \1/g'
1 2 3 4 5
-Devaraj Takhellambam
info sed, look for examples
ghostdog74,
I don't get the right output with your solution, am I missing something?
Another one:
awk '{while (NF){printf("%s%s", $(NF--),!NF?"":FS)}}'
my bad. just remove the BEGIN{} portion.
# echo "10 9 8 7 6 5 5 4 3 2 1 0" | awk '{for(i=NF;i>0;i--){printf "%s%s",$i,FS}}'
0 1 2 3 4 5 5 6 7 8 9 10
of course, your printf can be used as well..