Bash: replacing a substring in pipe stdin

假装没事ソ 提交于 2019-12-10 03:52:27

问题


I try to replace a certain substring from the stdin with a new substring. I have to get the stdin from the pipe, after reading several files by cat. Then I want to push the changed string forward to the pipe.

Here is what I try to do:

cat file1 file2 | echo ${#(cat)/@path_to_file/'path//to//file'} | ... | ... | ... | ...

I try to replace the substring @path_to_file with the replacement substring 'path/to/file' (the surrounding quotes are part of the replacement substring).

However bash gives me an error message: bad substitution

Any ideas how I could accomplish this?


回答1:


You can use the command sed.

cat file1 file2 | sed -e 's/@path_to_file/path/to/file/' ...



回答2:


With Parameter Expansion:

cat file1 file2 | while read -r line; do echo "${line/@path_to_file/path\/to\/file}"; done


来源:https://stackoverflow.com/questions/26517168/bash-replacing-a-substring-in-pipe-stdin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!