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,awksplits it into the numbered fields,NF,$1and$2etc. onFS - When you reassign
$1(or any other numbered field),awkrebuilds$0withFSreplaced 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