How to delete the last column of a file in Linux

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.


Using grep with PCRE:

$ grep -Po '.*(?=\s+[^\s]+$)' file.txt 
1223 1234 1323 ... 2222
1233 1234 1233 ... 3444
0000 5553 3455 ... 2334

Using GNU sed:

$ sed -r 's/(.*)\s+[^\s]+$/\1/' file.txt 
1223 1234 1323 ... 2222
1233 1234 1233 ... 3444
0000 5553 3455 ... 2334

Using Perl:

perl -lane '$,=" ";pop(@F);print(@F)' in

Using rev + cut:

rev in | cut -d ' ' -f 2- | rev