Bash sed 用文件内容替换文本 [英] Bash sed replace text with file content

查看:52
本文介绍了Bash sed 用文件内容替换文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 file.txt 内容替换字符串.

mtn="约翰"fs=`猫文件.txt`lgtxt=`cat large_text.txt`stxt1=`echo $lgtxt |sed "s/zzzz/$mtn/g"`stxt2=`echo $stxt1 |sed "s/pppp/$fs/g"`

它用'mnt'的值代替'zzzz'但不是'pppp'.文件 file.txt 包含名称列表,例如:汤姆琼斯泰德贝克琳达·埃文斯在单独的行中.我想将它们放在文件 large_text.txt 中的单独行中,就像它们在原始文件中一样,并用逗号分隔.

解决方案

您不想使用变量进行替换(例如,因为它很可能包含换行符).我假设它是 GNU sed,因为它是 .在这种情况下,看看 GNU sed 的 r 命令是否可以帮助你:

<块引用>

`r 文件名'作为 GNU 扩展,此命令接受两个地址.将要读取的 FILENAME 内容排队并插入到当前循环结束时或下一个循环时输出流输入行被读取.请注意,如果 FILENAME 无法读取,则为将其视为空文件,没有任何错误指示.作为 GNU `sed' 扩展,特殊值 `/dev/stdin' 是支持文件名,它读取文件的内容标准输入.

如果 pppp 在它自己的一行上,你可以使用类似的东西

/pppp/{r文件.txtd}

或者,带有 e 修饰符的 s 命令:

<块引用>

`e'此命令允许将来自 shell 命令的输入通过管道传输到图案空间.如果进行了替换,则命令为在模式空间中找到的被执行并替换模式空间与其输出.尾随换行符被抑制;结果是如果要执行的命令包含 NUL 字符,则未定义.这是一个 GNU `sed' 扩展.

这看起来像

s/pppp/cat file.txt/e

如果 pppp 是中线,这就是你所需要的.此外,如果您需要对 file.txt 进行进一步处理,您可以将 cat 替换为您需要的任何内容(尽管您需要小心引用 /\).

最后一个选择是考虑 Perl,它会接受与您的 shell 命令非常相似的东西.

I would like to replace string with file.txt content.

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"`

It replace 'zzzz' with value of 'mnt' but doesn't 'pppp'. File file.txt contain list of names eg: Tom jones Ted Baker Linda Evans in separate lines. I want to place them in file large_text.txt in separate lines like they are in oryginal file and separated by commas.

解决方案

You don't want to use a variable for the substitution (because it may well contain newlines, for example). I'm assuming that it's GNU sed given it's . In which case, see whether GNU sed's r command could help you:

`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.

If pppp is on a line of its own, you could go with something like

/pppp/{
r file.txt
d
}

or, alternatively, the s command with e modifier:

`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.

This would look something like

s/pppp/cat file.txt/e

and is what you'll need if pppp is mid-line. Also, if you need to do further processing on file.txt, you could replace cat with whatever you need (though you need to be careful about quoting / and \).

A final option is to consider Perl, which will accept something very similar to your shell commands.

这篇关于Bash sed 用文件内容替换文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆