0

I want to print the fields in specific format , Input :

col1|col2|col3|col4

I used cat file | cut -d '|' -f 3,1,4 output :

col1|col3|col4

But my expected output is:

col3|col1|col4

Can anyone help me with this?

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
Pooja
  • 165
  • 4
  • 14
  • possible duplicate of [Forcing the order of output fields from cut command](http://stackoverflow.com/questions/1037171/forcing-the-order-of-output-fields-from-cut-command) – dogbane Mar 04 '13 at 08:56

2 Answers2

2

From man cut:

Selected input is written in the same order that it is read, and is written exactly once

You should do:

$ awk -F'|' -vOFS='|' '{print $3,$1,$4}'  <<< "col1|col2|col3|col4"
col3|col1|col4
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
0

even though awk is good,here is a perl solution:

perl -F"\|" -ane 'print join "|",@F[2,0,3]'

tested:

> echo "col1|col2|col3|col4" | perl -F"\|" -ane 'print join "|",@F[2,0,3]'
col3|col1|col4
Vijay
  • 65,327
  • 90
  • 227
  • 319