How to Move First Column to Last Column in Unix

How to move first column to last column in unix?

Perl is handy for text manipulation:

perl -lane 'push @F, shift @F; print "@F"' file

How do I swap the last column in a csv file to be the first column? + awk

For an arbitrary number of columns, you can do:

awk 'BEGIN {OFS=FS=",";} {temp=$NF; $NF = ""; sub(",$", ""); print temp, $0}' filename
  • OFS=FS="," sets the output and input field separators to comma.
  • temp = $NF saves the original contents of the last column in a variable.
  • $NF = '' empties the last column in the input.
  • sub(",$", "") gets rid of the extra comma left by that
  • print temp, $0 prints the saved last column followed by the rest of the input.

If you know how many columns there are, it's probably easier to do this using cut. If there are 9 columns, you can do:

cut -d, -f9,1-8 infile > outfile

How to move last column to nth column?

A short awk command to move the last column to the third:

awk '{$3=$NF OFS $3;$NF=""}1' file

copy the last column in the first position awk

What about this? It is pretty similar to yours.

$ awk '{score=$NF; $NF="#"$NF; print score, $0}' file
1233425 word1 word2 #1233425
49586 word1 word2 word3 #49586

Note that in your case you are emptying $1, which is not necessary. Just store score as you did and then add # to the beginning of $NF.

How to print third column to last column?

...or a simpler solution: cut -f 3- INPUTFILE just add the correct delimiter (-d) and you got the same effect.

How to cut first n and last n columns?

Cut can take several ranges in -f:

Columns up to 4 and from 7 onwards:

cut -f -4,7-

or for fields 1,2,5,6 and from 10 onwards:

cut -f 1,2,5,6,10-

etc

rearrange columns using awk or cut command

try this awk one-liner:

awk '{$3=$NF OFS $3;$NF=""}7' file

this is moving the last col to the 3rd col. if you have 1000, then it does it with 1000th col.

EDIT

if the file is tab-delimited, you could try:

awk -F'\t' -v OFS="\t" '{$3=$NF OFS $3;$NF=""}7' file

EDIT2

add an example:

kent$  seq 20|paste -s -d'\t'                              
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

kent$ seq 20|paste -s -d'\t'|awk -F'\t' -v OFS="\t" '{$3=$NF OFS $3;$NF=""}7'
1 2 20 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

EDIT3

You didn't give any input example. so assume you don't have empty columns in original file. (no continuous multi-tabs):

kent$  seq 20|paste -s -d'\t'|awk -F'\t'  -v OFS="\t" '{$3=$10 FS $11 FS $3;$10=$11="";gsub(/\t+/,"\t")}7'
1 2 10 11 3 4 5 6 7 8 9 12 13 14 15 16 17 18 19 20

After all we could print those fields in a loop.

Rearrange columns using cut

For the cut(1) man page:

Use one, and only one of -b, -c or -f. Each LIST is made up of
one
range, or many ranges separated by commas. Selected input is written
in the same order that it is read, and is written exactly once.

It reaches field 1 first, so that is printed, followed by field 2.

Use awk instead:

awk '{ print $2 " " $1}' file.txt


Related Topics



Leave a reply



Submit