print in reverse order


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print in reverse order
# 1  
Old 05-23-2012
print in reverse order

Hi,

I want to print the item in reverse order such that the output would look like

Code:
00 50 50 23 40 22 02 96

Below is the input:
Code:
00 05 05 32 04 22 20 69

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by radoulov; 05-23-2012 at 06:10 AM..
# 2  
Old 05-23-2012
Code:
echo "00 50 50 23 40 22 02 96" | perl -ane '@F = map { scalar reverse } @F; print "@F"'

# 3  
Old 05-23-2012
Thanks man, but i don't know perl

found a way using shellscript

Code:
#Check for the number or words
w_cnt=`cat <file> | wc -w`

#Initialized cnt and proceed with while loop
cnt=1
while [ $cnt -lt $w_cnt ]
do
   rev_order=`cut -d' ' -f${cnt} <file> | rev`
   echo $rev_order
   cnt=$((cnt+1))
done > final_order

awk '{printf("%s",$0)}' final_order

this will give you an output of
00505023402202

Regards,

Last edited by radoulov; 05-23-2012 at 06:11 AM..
# 4  
Old 05-23-2012
Hi

Using awk:

Code:
$ echo 00 50 50 23 40 22 02 96 | awk '{for(i=1;i<=NF;i++){split($i,a,"");x=x a[2]""a[1] FS}}END{print x}'

# 5  
Old 05-23-2012
With modern bash:
Code:
for x in 00 50 50 23 40 22 02 96; do echo -e "${x#[0-9]}${x%[0-9]} \c"; done

With rev (much slower):
Code:
for x in 00 50 50 23 40 22 02 96; do echo -e "$(echo $x | rev) \c"; done

# 6  
Old 05-23-2012
can you explain me how echo -e "${x#[0-9]}${x%[0-9]} \c" works?

Thanks,
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Issues with sorting in reverse order

I have a unix script that outputs a summary file to the mac desktop. The file is called summary.txt I am trying to configure such so that the summary.txt file lists the content contained within such in reverse sort order. I have used sort -r but it does not seem to work. I would be... (8 Replies)
Discussion started by: Braveheart
8 Replies

2. Shell Programming and Scripting

Delete records in reverse order

Hi all, i have dynamic file 'xyz.txt', records always look likes below format ... 0000021 RET 31-MAR-1984 FAP 0000021 DTA 14-JAN-2003 CNV 0000021 DTA 25-MAR-2012 DTA 0000021 DTA 26-MAR-2012 DTA ################################################# 0000021 DTA ... (4 Replies)
Discussion started by: krupasindhu18
4 Replies

3. Shell Programming and Scripting

Sorting strings in reverse order

Hello, I have a large database of words and would like them sorted in reverse order i.e. from the end up. An example will make this clear: I have tried to write a program in Perl which basically takes the string from the end and tries to sort from that end but it does not seem... (5 Replies)
Discussion started by: gimley
5 Replies

4. UNIX for Dummies Questions & Answers

printing fields in reverse order

command/script(apart from awk) to print the fields in reverse order that is last field has to come first and so on and first field has to go last Input store-id date sale ............. ............. ... (3 Replies)
Discussion started by: tsurendra
3 Replies

5. UNIX for Dummies Questions & Answers

How to print arguments in reverse order?

Hey all, How do I make a script print its arguments in reverse order? Thanks (5 Replies)
Discussion started by: unclepickle1
5 Replies

6. Shell Programming and Scripting

How to get fields in reverse order?

i am having lines like below seperated by "|" (pipe) abc|xyz 123|567 i have to get the above in reverse order xyz|abc 567|123 Pls help (5 Replies)
Discussion started by: suryanarayana
5 Replies

7. Shell Programming and Scripting

Print rows in reverse order if values decrease along the column

Hi, Guys. Please help me to find solution to this problem using shell scripting. I have an INPUT file with 4 columns separated by tab. Each block of records is separated by ----- ----- Sample1 5402 6680 Pattern01 Sample2 2216 2368 Pattern02... (6 Replies)
Discussion started by: sam_2921
6 Replies

8. UNIX for Dummies Questions & Answers

sort -reverse order

I need to sort the particular column only in reverse order how i can give it.. if i give the -r option the whole file is getting sorted in reverse order. 1st 2nd col 3rd C col 4th col 5th col ------------------------------------------- C... (7 Replies)
Discussion started by: sivakumar.rj
7 Replies

9. Shell Programming and Scripting

sort a file in reverse order

I a file with log entries... I want to sort it so that the last line in the file is first and the first line is last.. eg. Sample file 1 h a f 8 6 After sort should look like 6 8 f a h 1 (11 Replies)
Discussion started by: frustrated1
11 Replies
Login or Register to Ask a Question