5

I have a tsv file like

1   2   3   4   5   ...
a   b   c   d   e   ...
x   y   z   j   k   ...

How can I merge two contiguous columns, say the 2nd and the 3rd, to get

1   2-3   4   5   ...
a   b-c   d   e   ...
x   y-z   j   k   ...

I need the code to work with text files with different numbers of columns, so I can't use something like awk 'BEGIN{FS="\t"} {print $1"\t"$2"-"$3"\t"$4"\t"$5}' file

awk is the first tool I thought about for the task and one I'm trying to learn, so I'm very interested in answers using it, but any solution with any other tool would be greatly appreciated.

Arch Stanton
  • 382
  • 5
  • 14

3 Answers3

6

With simple sed command for tsv file:

sed 's/\t/-/2' file

The output:

1   2-3 4   5   ...
a   b-c d   e   ...
x   y-z j   k   ...
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
3

With awk:

awk -v OFS='\t' -v col=2 '{
    $(col)=$(col)"-"$(col+1);              # merge col and col+1
    for (i=col+1;i<NF;i++) $(i)=$(i+1);    # shift columns right of col+1 by one to the left
    NF--;                                  # remove the last field
}1' file                                   # print the record

Output:

1   2-3   4   5   ...
a   b-c   d   e   ...
x   y-z   j   k   ...
randomir
  • 17,989
  • 1
  • 40
  • 55
  • I hope you don't mind me marking RomanPerekhrest's answer as the best one, I think people stumbling upon this question will be better served by it. I wanted to see how it could be done with `awk` and you showed me. Thank you :-) – Arch Stanton Jan 22 '18 at 18:43
  • Of course I don't mind :) I'm glad my answer helped you too. – randomir Jan 22 '18 at 18:45
3

Following awk may help you in same, in case you are not worried about little space which will be created when 3rd field will be nullified.

awk '{$2=$2"-"$3;$3=""} 1'  Input_file
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93