Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am new to linux I have a CSV file as

input.csv
1,2,3,10
4,5,6
7,8,9,12,28,30

I want to reverse the columns in this file which means

output.csv
10,3,2,1
6,5,4
30,28,12,9,8,7

I know for a fixed column count but If the column count varieswhat should I do? Any help is appreciated

share|improve this question
up vote 7 down vote accepted

With perl, assuming the fields in your CSV don't have embedded commas, newlines, etc.:

perl -lpe '$_ = join ",", reverse split /,/' input.csv
share|improve this answer
    
Thanks a lot.It works perfectly . – RKR 15 hours ago
    
@RKR If this answer solved your issue, please take a moment and accept it by clicking on the check mark to the left. That will mark the question as answered and is the way thanks are expressed on the Stack Exchange sites. – terdon 15 hours ago
    
@terdon Sure I will accept it. – RKR 14 hours ago
    
@RKR no hurry. I just saw you're a new user and wanted to make sure you knew how the site worked. Welcome aboard! – terdon 14 hours ago
    
The safe way would use Text::CSV, for if the assumption fails. – OrangeDog 7 hours ago

You could use awk:

awk  'BEGIN{
     FS=OFS=","               # set the field delimiter
   }
   NF{                        # execute only on non empty line
      for(i=NF;i>1;i--)       # loop through all element except the first one
         printf "%s", $i OFS; # print the parameter together with the comma 
      printf "%s", $1 "\n"    # print last element
   }' input.csv
share|improve this answer

CSV can't always be processed by simply splitting by commas, line by line, since fields may sometimes have commas themselves, or newlines. To be able to include those characters, fields must be quoted. You need quotes to include quote characters too.

This is a simple solution you can call from the shell that uses a proper csv parser:

ruby -e 'require "csv"; CSV.filter(&:reverse!)' < input.csv > output.csv

If you don't have ruby, this works with both python 2 and 3.

python -c $'import csv; import sys\nfor r in csv.reader(sys.stdin): r.reverse(); csv.writer(sys.stdout).writerow(r)' < input.csv > output.csv

Here it is in multiple lines:

python < input.csv > output.csv -c '
import csv
import sys

for r in csv.reader(sys.stdin):
  r.reverse()
  csv.writer(sys.stdout).writerow(r)
'

Here are some examples where this will work where solutions with simple comma splitting won't work:

input.csv

1,"2,3"

output.csv

"2,3",1

input.csv

1,"
2"

output.csv

"
2",1
share|improve this answer
perl -l -a -F, -n -e 'print join ("," , reverse  @F)'  input.csv
share|improve this answer
awk -F, '{for(i=NF;i>=1;i--)printf("%s,",$i);printf "\n"}' input.txt | sed "s/$,//"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.