With awk
:
awk 'NF{NF-=1};1' <in >out
or:
awk 'NF{NF--};1' <in >out
or:
awk 'NF{--NF};1' <in >out
Although this looks like voodoo, it works.
There are three parts to each of these awk commands.
The first is NF
, which is a precondition for the second part. NF
is a variable containing the number of fields in a line. In AWK, things are true if they're not 0 or empty string ""
. Hence, the second part (where NF
is decremented) only happens if NF
is not 0.
The second part (either NF-=1
NF--
or --NF
) is just subtracting one from the NF
variable. This prevent the last field from being printed, because when you change a field (removing the last field in this case), awk
re-construct $0
, concatenate all fields separated by space by default. $0
didn't contain the last field anymore.
The final part is 1
. It's not magical, it's just used as a expression that means true
. If an awk
expression evaluates to true without any associated action, awk
default action is print $0
.
cut
sounds like the tool for the job.