-1

I would like to delete specific column from textual table I've got as an output in bash. Here is sample data:

Column_1  Column_2  Column_3
15        20        40
10        30        50
...       ...       ...
5         10        20

Lets say I would like to delete whole second column to have something like this:

Column_1  Column_3
15        40
10        50
...       ...
5         20

I tried to use AWK to do this, by specifying that I want to print columns $1 and $3:

awk '{print $1 $3}'

but it doesn't work in general case when number of columns is just N.

How to do this in bash?

  • 1
    Dear Wojciech, welcome to Stack Overflow. While I mark your question as a duplicate, it does not imply that you asked a bad question. You question was very well written, concise, contained example data and example code of what you tried. Unfortunately, this is a very common question. Hence, the mark as duplicate. – kvantour Oct 11 '19 at 20:56
  • 2
    Related: [this](https://unix.stackexchange.com/questions/222121/), [this](https://stackoverflow.com/questions/15361632/), [this](https://stackoverflow.com/questions/12716392/), [this](https://askubuntu.com/questions/1127670/), [this](https://www.commandlinefu.com/commands/view/6872/exclude-a-column-with-awk), [this](http://tuxgraphics.org/~guido/scripts/awk-one-liner.html), ... – kvantour Oct 11 '19 at 20:57
  • If columns are tab separated, maybe `cut -f1,3 foo.tsv` – Shawn Oct 11 '19 at 22:05
  • @Shawn, easier as cut -f2 --complement foo.tsv, especially if there are a lot of columns. – user2138595 Oct 12 '19 at 08:41
  • @user2138595 I actually put that up first and changed it because afaik `--complement` isn't posix. – Shawn Oct 12 '19 at 17:46

1 Answers1

1

Just clear out the column you don't want.

awk '{$2 = ""; print}'
Barmar
  • 741,623
  • 53
  • 500
  • 612