Delete the last word of a line in shell Delete the last word of a line in shell shell shell

Delete the last word of a line in shell


Try this :

sed -i '1{s/[^ ]\+\s*$//}' file test.db


If you want to delete the last word only off of the last line, then you can use:

sed -i '$s/\w*$//' test.db

If you want to delete the last word from the line for key3:

sed -i '/key3/s/\w*$//' test.db


Here is an awk to remove last field:

echo "one two three" | awk '{$NF="";sub(/[ \t]+$/,"")}1'one two