linux - 如何在文本文件中包含带有模式的字符串的行的末尾添加文本?

标签 linux bash shell unix terminal

例如:

hello1.jpg
hello2.jpg
hello3.jpg

我想把它编辑成这样:

hello1.jpg 0
hello2.jpg 0
hello3.jpg 0

我试过:

sed -i '/(hello*)/ s/$/ 0/' hello.txt
perl -ipe 's/$/ 0/ if /hello/' hello.txt
sed -i '/^hello*/ s/$/ 0/' hello.txt

最佳答案

你的第一种方法几乎是正确的,除了括号(不需要)是按字面解释的:

$ sed '/hello/ s/$/ 0/' file
hello1.jpg 0
hello2.jpg 0
hello3.jpg 0

关于linux - 如何在文本文件中包含带有模式的字符串的行的末尾添加文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38147911/

相关文章:

linux - Sublime - 仅将选择范围扩展到匹配项

python - 从 Mac 到 Linux 的虚拟环境

c++ - 使用 boost::process 调用外部程序会导致调用者挂起 (Linux)

php - 从 file1 file2 获取公共(public)数据的 comm 命令

php - windows环境下如何通过PHP连接远程Linux box

bash - 包含变量的表达式的grep

node.js - 从 node --inspect 命令打开 stdout 给出的 url

bash - exec-maven-plugin 无法执行 .sh 脚本 : permission denied

linux - 在 Bash 中解析带有开始和结束定界符的字符串

linux - 自动化 Enter 按键不适用于 Linux Mint 15