Skip to main content
Tweeted twitter.com/StackUnix/status/966204092413595649
edited tags
Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 265
Source Link
MLu
  • 2.2k
  • 1
  • 18
  • 29

How to delete input field in AWK?

I'm transforming some data with awk (or gawk) and want to delete one of the input fields before printing the output again.

What I want to achieve is this:

~ $ echo 'field1,field2,field3' | awk -F, '{transform($1); delete($2); print $0;}'
new_field1,field3

I can't just assign an empty string to $2 because that leads to new_field1,,field3 (note the two commas).

I could explicitly print only the fields that I want but that's not very elegant because I've got far more field than 3 and also there are optional fields at the end (not shown here). That's why I prefer print $0. Just need to get rid of some fields first.

Any idea?