Linux sed:只是想删除一个子字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8356958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sed: just trying to remove a substring
提问by user1077220
I have a file with this content:
我有一个包含此内容的文件:
foo: bar1
foo: bar2
foo: bar3
foo: bar4
foo: bar5
I want to remove the chains "foo: " so the file remains:
我想删除链“foo:”所以文件仍然存在:
bar1
bar2
bar3
...
I'm trying it with:
我正在尝试:
$ sed 's/foo: /' file.txt
but it says:
但它说:
sed: -e expression #1, char 15: unterminated `s' command
sed: -e 表达式 #1, char 15: 未终止的 `s' 命令
Any help?
有什么帮助吗?
Javi
哈维
采纳答案by aefxx
You need to:
你需要:
$ sed -r 's/^foo: //' file.txt
回答by Zsolt Botykai
For this simple task you can use cut
:
对于这个简单的任务,您可以使用cut
:
cut -b 11- INPUTFILE
HTH
HTH
回答by Kent
awk -F';' '{print }' file
or
或者
grep -Po '(?<= ).*$' file
回答by jaypal singh
Your file
您的 file
[jaypal:~/Temp] cat file
foo: bar1
foo: bar2
foo: bar3
foo: bar4
foo: bar5
Using awk
:
使用awk
:
[jaypal:~/Temp] awk -F";" '{print $NF}' file
bar1
bar2
bar3
bar4
bar5
Using sed
使用 sed
[jaypal:~/Temp] sed 's/.*;//' file
bar1
bar2
bar3
bar4
bar5
Using cut
使用 cut
[jaypal:~/Temp] cut -d";" -f2 file
bar1
bar2
bar3
bar4
bar5
回答by zzapper
Practical example using sed to strip off a substring and then copy to paste buffer (linux)
Scanning an Autohotkey config file on linux (sadly no autohotkey for linux) . '::' is used as a delimiter in AHK
使用 sed 剥离子字符串然后复制到粘贴缓冲区 (linux)
在 linux 上扫描 Autohotkey 配置文件的实际示例(遗憾的是没有用于 linux 的 autohotkey)。'::' 在 AHK 中用作分隔符
grep -Eie "xxx::" AutoHotkey.ahk | sed 's/^.*:://' | xclip