简体   繁体   English

使用SED在脚本中难以替换的字符串

[英]difficult string to replace in script using SED

I know, many people have had questions on how to use sed to replace a string, but I have a difficult one here. 我知道,许多人对如何使用sed替换字符串有疑问,但是我在这里遇到了一个难题。

I have a file I need to replace a string of text that prompts the user to enter content. 我有一个文件,需要替换一个提示用户输入内容的文本字符串。 I want to automate this so the user does not interact. 我想使它自动化,以便用户不进行交互。 By replacing this string with a static file path. 通过用静态文件路径替换此字符串。 but the text is a bash script and has ' and " within the string I want to replace. It does not work. Either because I have syntax errors in my formatting, or it simply is not possible to do this action with sed. Please advice! 但是文本是bash脚本,并且要替换的字符串中包含“和”。它不起作用。要么是因为我的格式出现语法错误,要么根本无法使用sed执行此操作。请咨询!

Here is what I am attempting to do: I want to replace this long string 这是我要尝试做的事情:我想替换这个长字符串

read -e -p 'Enter path for Boot Partition : '  BOOTUSERFILEPATH

with a string that looks like this: 带有如下所示的字符串:

BOOTUSERFILEPATH=../board-support/prebuilt-images

My attempt: 我的尝试:

sed -i "/read -e -p 'Enter path for Boot Partition : '  BOOTUSERFILEPATH/BOOTUSERFILEPATH="../board-support/prebuilt-images"" file_to_search.sh

Update: I fixed the syntax error, but file still is not updated with the new path information... :( 更新:我已修复语法错误,但文件仍未使用新的路径信息进行更新... :(

found the problem. 发现了问题。 the search was not finding the strings because of an extra space in my search command. 由于我的搜索命令中有多余的空格,搜索未找到字符串。 It works now! 现在可以使用!

There are forward slashes in your string so one needs to use a different delmiter. 字符串中有正斜杠,因此需要使用其他的斜杠。 Here I use '|' 在这里我用'|' as the delimiter. 作为分隔符。

sed "s|read -e -p \'Enter path for Boot Partition : \'  BOOTUSERFILEPATH|BOOTUSERFILEPATH=../board-support/prebuilt-images|g" oldfile > newfile

You may note that the -i option to sed which allows files to be edited in place is not a POSIX supported option. 您可能会注意到sed的-i选项(允许在适当位置编辑文件)不是POSIX支持的选项。

However if you wish to use it: 但是,如果您想使用它:

sed -i "s|read -e -p \'Enter path for Boot Partition : \'  BOOTUSERFILEPATH|BOOTUSERFILEPATH=../board-support/prebuilt-images|g" oldfile

You may find it easier to use a pattern with sed which matches part of this string and then replaces its entirety: 您可能会发现,使用带有sed的模式更容易,该模式与该字符串的一部分匹配,然后替换其全部内容:

sed 's|read -e -p .* BOOTUSERFILEPATH|BOOTUSERFILEPATH=../board-support/prebuilt-images|g' filename > newfilename

From the POSIX specification page for sed : sed的POSIX规范页面:

s/BRE/replacement/flags
Substitute the replacement string for instances of the BRE in the pattern space. 将替换字符串替换为模式空间中BRE的实例。 Any character other than <backslash> or <newline> can be used instead of a slash to delimit the BRE and the replacement. 可以使用<backslash><newline>以外的任何字符代替斜杠来分隔BRE和替换。

You need to use an actual substitute command, and you need to avoid the slashes in the replacement text from confusing sed . 您需要使用实际的替代命令,并且需要避免替换文本中的斜杠使sed混淆。 Personally, I'd probably use: 就个人而言,我可能会使用:

sed -i.bak "s%^read .* BOOTUSERFILEPATH$%BOOTUSERFILEPATH=../board-support/prebuilt-images%" file_to_search.sh

or even more likely: 甚至更有可能:

BOOTUSERFILEPATH="../board-support/prebuild-images"
sed -i.bak "s%^read .* BOOTUSERFILEPATH$%BOOTUSERFILEPATH=$BOOTUSERFILEPATH%" file_to_search.sh

The s%%% uses % instead of / to delimit the parts of the command. s%%%使用%代替/来分隔命令的各个部分。 I cheated on the match pattern, working on the assumption that you don't have many similar lines in the file. 我欺骗了匹配模式,并假设文件中没有很多相似的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM