78

I've got data in a large file (280 columns wide, 7 million lines long!) and I need to swap the first two columns. I think I could do this with some kind of awk for loop, to print $2, $1, then a range to the end of the file - but I don't know how to do the range part, and I can't print $2, $1, $3...$280! Most of the column swap answers I've seen here are specific to small files with a manageable number of columns, so I need something that doesn't depend on specifying every column number.

The file is tab delimited:

Affy-id chr 0 pos NA06984 NA06985 NA06986 NA06989

10 Answers 10

129

You can do this by swapping values of the first two fields:

awk ' { t = $1; $1 = $2; $2 = t; print; } ' input_file
12
  • 5
    This answer is problematic with different sizes of columns and their separators. More extensible answer here unix.stackexchange.com/a/31596/16920 Commented Jul 1, 2015 at 14:35
  • 4
    Using -F '\t' tabs are eaten away in the final output. Is there a way to preserve them?
    – Atcold
    Commented Nov 2, 2015 at 15:49
  • 3
    OK, one has to specify OFS=$'\t' as pointed out by the answer below. @perreal, perhaps it's worth it updating the answer with the additional parameter?
    – Atcold
    Commented Nov 2, 2015 at 15:51
  • 14
    if you use: awk '{ print $2, $1}' is the same :D
    – A.Villegas
    Commented Feb 4, 2019 at 17:34
  • 2
    @cornuz, no problem, Note that your suggestion only prints 2 columns. OP wants to print all columns not just the first two.
    – perreal
    Commented Mar 4, 2021 at 21:27
27

I tried the answer of perreal with cygwin on a windows system with a tab separated file. It didn't work, because the standard separator is space.

If you encounter the same problem, try this instead:

awk -F $'\t' ' { t = $1; $1 = $2; $2 = t; print; } ' OFS=$'\t' input_file

Incoming separator is defined by -F $'\t' and the seperator for output by OFS=$'\t'.

awk -F $'\t' ' { t = $1; $1 = $2; $2 = t; print; } ' OFS=$'\t' input_file > output_file
2
  • 2
    Super! I was missing the OFS=$'\t' parameter!
    – Atcold
    Commented Nov 2, 2015 at 15:52
  • 3
    This approach can end up with tabs at the start of the line. Might not be the intended outcome. Commented Jun 26, 2017 at 13:37
15

Try this more relevant to your question :

awk '{printf("%s\t%s\n", $2, $1)}' inputfile
1
  • 10
    This only prints the first two columns. Slightly more compact is awk '{print $2 "\t" $1}' inputfile.
    – Fuujuhi
    Commented Jun 27, 2018 at 6:33
6

This might work for you (GNU sed):

sed -i 's/^\([^\t]*\t\)\([^\t]*\t\)/\2\1/' file
1
  • Perfect solution for us vim users.
    – awm
    Commented Nov 29, 2021 at 12:36
3

Have you tried using the cut command? E.g.

cat myhugefile | cut -c10-20,c1-9,c21- > myrearrangedhugefile
5
  • I haven't, but I'll remember that for future use! Commented Aug 15, 2012 at 10:44
  • 3
    -c=characters ... so this does not exchange columns.
    – blehman
    Commented Dec 27, 2013 at 19:44
  • It will swap columns in the output file - try it for yourself
    – Robbie Dee
    Commented Jan 20, 2014 at 22:03
  • 5
    how can we do it without knowing the character count ? cat myhugefile | cut -f2,1 gives the same output as cat myhugefile | cut -f1,2 Commented Feb 2, 2014 at 0:38
  • 5
    You can output each column to an intermediate file. Something like: cut -f2 myhugefile > piece1 ; cut -f1 myhugefile > piece2 | paste piece1 piece2 > myrearrangedhugefile ; rm piece1 ; rm piece2
    – Robbie Dee
    Commented Feb 3, 2014 at 17:17
3

This is also easy in perl:

perl -pe 's/^(\S+)\t(\S+)/$2\t$1/;' file > outputfile
2

You could do this in Perl:

perl -F\\t -nlae 'print join("\t", @F[1,0,2..$#F])' inputfile

The -F specifies the delimiter. In most shells you need to precede a backslash with another to escape it. On some platforms -F automatically implies -n and -a so they can be dropped.

For your problem you wouldn't need to use -l because the last columns appears last in the output. But if in a different situation, if the last column needs to appear between other columns, the newline character must be removed. The -l switch takes care of this.

The "\t" in join can be changed to anything else to produce a different delimiter in the output.

2..$#F specifies a range from 2 until the last column. As you might have guessed, inside the square brackets, you can put any single column or range of columns in the desired order.

2

No need to call anything else but your shell:

bash> while read col1 col2 rest; do 
        echo $col2 $col1 $rest
      done <input_file

Test:

bash> echo "first second a c d e f g" | 
      while read col1 col2 rest; do 
        echo $col2 $col1 $rest
      done
second first a b c d e f g
0

Maybe even with "inlined" Python - as in a Python script within a shell script - but only if you want to do some more scripting with Bash beforehand or afterwards... Otherwise it is unnecessarily complex.

Content of script file process.sh:

#!/bin/bash

# inline Python script
read -r -d '' PYSCR << EOSCR
from __future__ import print_function
import codecs
import sys

encoding = "utf-8"
fn_in = sys.argv[1]
fn_out = sys.argv[2]

# print("Input:", fn_in)
# print("Output:", fn_out)

with codecs.open(fn_in, "r", encoding) as fp_in, \
        codecs.open(fn_out, "w", encoding) as fp_out:
    for line in fp_in:
        # split into two columns and rest
        col1, col2, rest = line.split("\t", 2)
        # swap columns in output
        fp_out.write("{}\t{}\t{}".format(col2, col1, rest))
EOSCR

# ---------------------
# do setup work?
# e. g. list files for processing

# call python script with params
python3 -c "$PYSCR" "$inputfile" "$outputfile"

# do some more processing
# e. g. rename outputfile to inputfile, ...

If you only need to swap the columns for a single file, then you can also just create a single Python script and statically define the filenames. Or just use an answer above.

0

awk swapping sans temp-variable :

echo '777777744444444464449: 317 647 14423 262927714037  :   0x2A29D5A1BAA7A95541' | 
mawk '1; ($1 = $2 substr(_, ($2 = $1)^_))^_' FS=':' OFS=':' 
777777744444444464449: 317 647 14423 262927714037  :   0x2A29D5A1BAA7A95541

 317 647 14423 262927714037  :777777744444444464449:   0x2A29D5A1BAA7A95541

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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