Skip to content
Advertisement

how to flip columns in reverse order using shell scripting/python

Dear experts i have a small problem where i just want to reverse the columns.For example i have a data sets arranged in 4 columns i need to put last column first, and so on reversely…how can this work be done…i hope some expert will definitely answer my questions.Thanks

in put data example

      1 2 3 4 5
      6 7 8 9 0
      3 4 5 2 1
      5 6 7 2 3

i need output like as below

         5 4 3 2 1
         0 9 8 7 6
         1 2 5 4 3
         3 2 7 6 5 

Advertisement

Answer

Perl to the rescue!

perl -lane 'print join " ", reverse @F' < input-file
  • -n reads the file line by line, running the code specified after -e for each line
  • -l removes newlines from input and adds them to output
  • -a splits the input line on whitespace populating the @F array
  • reverse reverses the array
  • join turns a list to a string
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement