Delete last word on every line in bash

I am trying to view a file and delete the last word on each line. I am currently using the command

sed 's/^*\n//' old.txt > new.txt

but it turns out that old.txt is the same as new.txt. Thanks for any help, and let me know if I can clarify the issue. Also, to define a "word," I just use spaces.

+3
source share
2 answers

Try the following. \w*matches the last word inside the file, and $binds the search to the end of the line.

sed s/'\w*$'// old.txt > new.txt

The reason old.txt is output as new.txt is probably because your regular expression ^*\ndoes not match the lines in old.txt.

+8
source

. , FILE.

sed -i 's/[[:alnum:]]*$//' /yourfile

OS X,

sed -i '' 's/[[:alnum:]]*$//' /yourfile

, .

sed 's/[[:alnum:]]*$//' /yourfile
0

All Articles