awk: Removing a column
cut
is a great tool for the job of selecting one or more columns from some input lines but awk
will also do it, albeit with two tricks:
- When you reassign
$0
,awk
splits it into the numbered fields,NF
,$1
and$2
etc. onFS
- When you reassign
$1
(or any other numbered field),awk
rebuilds$0
withFS
replaced withOFS
{
$2 = "" # Empty the second field
$0 = $0 # Assign the line -> perform field splitting based on FS, reassigning NF, $1, $2, etc.
$1 = $1 # Assign to a field -> rebuilds $0, with FS replaced with OF
}
From StackOverflow answer
Published on: 04 Mar 2024