views:

205

answers:

5

I have 12 columns separated by a tab. How can I join them side-by-side?

[Added] You can also tell me other methods as AWK: the faster the better.

+3  A: 

If you are just using awk to concatenate the columns I would use 'tr' and delete tab

cat file1 | tr -d '\011'>  file2
Martin Beckett
Every file has many columns.
Masi
The above doesn't car it just deletes all tabs and leaves all other chars the same
Martin Beckett
Thanks for all the downvotes - I know the OP asked about awk. But if someone asks how to knock in a screw with a hammer it is often worth pointing out the invention of the screwrdiver.
Martin Beckett
A: 

Try this:

{
    print $1$2$3$4$5$6$7$8$9$(10)$(11)$(12)
}

I'm not an awk genius so I don't know if there's some sort of looping construct you can use.

strager
+4  A: 

Since you asked specifically about awk (there are tools better suited to the job), the following is a first-cut solution:

awk '{print $1$2$3$4$5$6$7$8$9$10$11$12}'

A more complicated and configurable solution, where you could change the number of columns used for output, would be:

awk -v lim=12 '{for(x=1;x<lim;x++){printf "%s",$x};print ""}'

Other possibilities, if you're not restricted to awk, are:

tr -d '\011'                     # to combine ALL columns on the line.
cut --output-delimiter='' -f1-12 # more general (1-12 or 3-7 or 1-6,9).

Based on your edit and comments, I suggest cut is the best tool for the job. Use "man cut", "info cut" or "cut --help" for more details (this depends on your platform).

paxdiablo
A: 

Well, it depends on your editor/command of choice. But generally, it boild down to replacing the character with nothing. For example, in vim: ":%s/\t//g"

ldigas
A: 

You did not mention what tool you would like to use but any text editor would be able to replace the tab to an empty character, I guess that would work, that's what I usually do.

Gustavo Rubio