删除最后一个字在bash中的每一行 [英] Delete last word in each line in bash
问题描述
我试图去通过文件,并删除每行的最后一个字。目前,我使用命令
I am trying to go through a file, and delete the last word in each line. Currently, I am using the command
sed 's/^*\n//' old.txt > new.txt
,但它是未来出old.txt相同new.txt。感谢您的帮助,让我知道如果我能澄清问题。此外,为了界定字我只是用空间。
but it is coming out that old.txt is the same as new.txt. Thanks for any help, and let me know if I can clarify the question. Also, in order to define 'word' I am just using spaces.
推荐答案
请尝试以下方法。 \\ W *
的最后一个字相匹配的文件和 $
内部锚搜索到的行的末尾。
Try the following. \w*
matches the last word inside of the file and $
anchors the search to the end of the line.
sed s/'\w*$'// old.txt > new.txt
之所以说是old.txt出来作为new.txt可能是因为你的常规的前pression ^ * \\ n
不匹配任何旧线.TXT。
The reason that old.txt is coming out as new.txt is likely because your regular expression ^*\n
is not matching any lines in old.txt.
这篇关于删除最后一个字在bash中的每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!