Sample Header Ad - 728x90

Remove the first field (and leading spaces) with a single AWK

4 votes
6 answers
638 views
Consider this input and output:
foo bar baz
bar baz
How do you achieve with a single AWK? Please explain your approach too. These are a couple tries:
$ awk '{ $1 = ""; print(substr($0, 2)) }' <<<'foo bar baz'
bar baz
Since I know the $1 = "" deletes the first field, that anything in $0 will start at the second character; but it seems kinda obtuse. This is another way:
$ awk '{ $1 = ""; print }' <<<'foo bar baz' | awk '{ $1 = $1; print }'
bar baz
Since the second awk "recompiles $0"; but what I'd really like to do is "recompile $0; but in first awk call. This approach doesn't work:
$ awk '{ $1 = ""; $0 = $0; print }' <<<'foo bar baz'
 bar baz
notice the leading spaces. I was hoping the $0 = $0 would recompile $0; but it didn't work.
Asked by mbigras (3472 rep)
Apr 9, 2025, 06:44 AM
Last activity: Jun 26, 2025, 09:32 AM