我想用file.txt内容替换字符串.
mtn="John"
fs=`cat file.txt`
lgtxt=`cat large_text.txt`
stxt1=`echo $lgtxt | sed "s/zzzz/$mtn/g"`
stxt2=`echo $stxt1 | sed "s/pppp/$fs/g"`
Run Code Online (Sandbox Code Playgroud)
它将'zzzz'替换为'mnt'的值,但不是'pppp'.文件file.txt包含名称列表,例如:Tom jones Ted Baker Linda Evans在不同的行中.我想将它们放在文件large_text.txt中的单独行中,就像它们在oryginal文件中并用逗号分隔.
您不希望使用变量进行替换(例如,因为它可能包含换行符).我假设它是GNU sed,因为它是linux.在这种情况下,看看GNU sed的r命令是否可以帮助你:
Run Code Online (Sandbox Code Playgroud)`r FILENAME' As a GNU extension, this command accepts two addresses. Queue the contents of FILENAME to be read and inserted into the output stream at the end of the current cycle, or when the next input line is read. Note that if FILENAME cannot be read, it is treated as if it were an empty file, without any error indication. As a GNU `sed' extension, the special value `/dev/stdin' is supported for the file name, which reads the contents of the standard input.
如果pppp它在自己的线上,你可以使用类似的东西
/pppp/{
r file.txt
d
}
Run Code Online (Sandbox Code Playgroud)
或者,s带e修饰符的命令:
Run Code Online (Sandbox Code Playgroud)`e' This command allows one to pipe input from a shell command into pattern space. If a substitution was made, the command that is found in pattern space is executed and pattern space is replaced with its output. A trailing newline is suppressed; results are undefined if the command to be executed contains a NUL character. This is a GNU `sed' extension.
这看起来像
s/pppp/cat file.txt/e
Run Code Online (Sandbox Code Playgroud)
如果pppp是中线你就是你需要的.此外,如果您需要进行进一步处理file.txt,您可以替换cat为您需要的任何内容(尽管您需要小心引用/和\).
最后一个选择是考虑Perl,它将接受与shell命令非常相似的东西.
| 归档时间: |
|
| 查看次数: |
1784 次 |
| 最近记录: |