print line backwards - horizontally by field?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users print line backwards - horizontally by field?
# 1  
Old 03-05-2011
print line backwards - horizontally by field?

let's say you have
Code:
dog, cat, 1, 2, 3
reverse, 7, 9, i, tell, you

you want to print

Code:
3, 2, 1, cat, dog
you, tell, i, 9, 7, reverse

The delimiter is a comma. The number of commas in each line though is undetermined.

How would you do this using either regular UNIX or awk? I know the answer can't be that complex, but am having trouble thinking INSIDE the box.

Last edited by radoulov; 03-05-2011 at 03:02 PM.. Reason: Code tags, please!
# 2  
Old 03-05-2011
Code:
echo dog, cat, 1, 2, 3 |awk '{for(i=NF;i>1;i--) printf $i OFS;print $1}' FS=, OFS=,
 3, 2, 1, cat,dog

This User Gave Thanks to yinyuemi For This Post:
# 3  
Old 03-05-2011
---------- Post updated at 04:14 AM ---------- Previous update was at 04:09 AM ----------

never mind my previous comment (which I edited). this does appear to work!
# 4  
Old 03-05-2011
Code:
$ ruby -F"," -ane '$F[-1].chomp!; $F.reverse!; print "#{$F.join(",")}\n"' file
 3, 2, 1, cat,dog
 you, tell, i, 9, 7,reverse

# 5  
Old 03-05-2011
Perl & Ruby:

Code:
perl -F, -lane'
  print join ",", reverse @F
  ' infile

Code:
ruby -F, -lane'
  print  $F.reverse.join ","
  ' infile


Last edited by radoulov; 03-05-2011 at 03:03 PM..
# 6  
Old 03-05-2011
@radoulov, thanks for reminding. forgot about the -l switch.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

2. Shell Programming and Scripting

Use two field separator in the same line and print them

Hi Guys, I have the file --- HOST_NAME,data_coleta,data_carga,CPU_util,CPU_idle,run_queue,memory,MEMORY_SYSTEM,MEMORY_TOTAL,MEMORY_SWAPIN,MEMORY_SWAPOUT,DISK_READ,DISK_WRITE,DISK_IO,NET_IN_PACKET, NET_OUT_PACKET... (4 Replies)
Discussion started by: antoniorajr
4 Replies

3. Shell Programming and Scripting

print whole line if the 1st field contains...

i want to print lines in a file that the 1st field of each line has a Date shape such: yy/mm/dd or on the other hand contains slash "/" . (1 Reply)
Discussion started by: oreka18
1 Replies

4. Shell Programming and Scripting

Print a field from the previous line

plz help me!! I have this file , 3408 5600 3796 6035 4200 6285 4676 0 40 1554 200 1998 652 2451 864 2728 1200 0 I want it like if $2==0,replace it with field from the previous line+500 say here the o/p would be like 3408 5600 3796 6035 4200 6285... (16 Replies)
Discussion started by: Indra2011
16 Replies

5. Shell Programming and Scripting

only print line if 3rd field is 01

Similar question... I have a space delimited text file and I want to only print the lines where the 3rd word/field/column is equal to "01" awk '{if $3 = "01" print $0}' something like this. I meant to say: only print line IF 3rd field is 01 (2 Replies)
Discussion started by: ajp7701
2 Replies

6. Shell Programming and Scripting

How to print line if field matches?

Hi all, I got several lines line this a b c d e 1 e a 1 c d e 3 f a b c 1 e 8 h a b c d e 1 w a 1 c d e 2 w a b c d e 1 t a b c d e 7 4 How can I print the line if 1 is the field one before the last field? Basicly this 2 field ? a b c d e 1 e a b c d e 1 t The file I got is... (7 Replies)
Discussion started by: stinkefisch
7 Replies

7. Shell Programming and Scripting

Print the entire line if second field has value P

Friends, I have .txt file with 3 millions of rows. File1.txt ABC1|A|ABCD1|XYZ1 ABC2|P|ABCD2|XYZ2 ABC3|A|ABCD3|XYZ3 ABC4|P|ABCD4|XYZ4 If second field has value P then print the entire line. Thanks in advance for your help, Prashant (4 Replies)
Discussion started by: ppat7046
4 Replies

8. Shell Programming and Scripting

how to print field n of line m

Hi everyone, I have a basic csh/awk question. How do I print a given field from a given line in a given file? Thanks in advance! (11 Replies)
Discussion started by: Deanne
11 Replies

9. Shell Programming and Scripting

Print line if first Field matches a pattern

Hi All, I would like my code to be able to print out the whole line if 1st field has a dot in the number. Sample input and expected output given below. My AWK code is below but it can;t work, can any expert help me ? Thanks in advance. {if ($1 ~ /*\.*/) { print $0 }} Input: ... (2 Replies)
Discussion started by: Raynon
2 Replies

10. Shell Programming and Scripting

How to print empty line when the a field is changed

Hi all, I have this input file .. BSS01 107 Swafieh 11/06/2008 12:06:57 BSS01 111 Ramada_Hotel 12/06/2008 11:37:20 BSS01 147 Kalha_Rep 11/06/2008 19:13:39 BSS01 147 Kalha_Rep ... (9 Replies)
Discussion started by: yahyaaa
9 Replies
Login or Register to Ask a Question