Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何编辑shell或PowerScript中XML样式标记内的文本?_Bash_Shell_Powershell_Unix_Sh - Fatal编程技术网

Bash 如何编辑shell或PowerScript中XML样式标记内的文本?

Bash 如何编辑shell或PowerScript中XML样式标记内的文本?,bash,shell,powershell,unix,sh,Bash,Shell,Powershell,Unix,Sh,我的input.txt文件中有这些行 your group image is <gpimage>image1</gpimage> some text some text some text <gpimage>pic2</gpimage> some text some text some text <gpimage>image3</gpimage> <gpimage>img4</gpimage> &l

我的
input.txt文件中有这些行

your group image is <gpimage>image1</gpimage> some text some text some text <gpimage>pic2</gpimage> some text some text some text <gpimage>image3</gpimage> 
<gpimage>img4</gpimage> <gpimage>photo5</gpimage> some text some text some some text some text some some text some text some <gpimage>image6</gpimage>.
您的组图像是图像1一些文本一些文本一些文本pic2一些文本一些文本一些文本图像3
img4照片5一些文字一些文字一些文字一些文字一些文字一些文字一些文字一些文字一些文字一些图片6。
我想将字符串添加到“.png”
image1
,使内容看起来像这样
image1.png


PS:我的输出文件应该在
元素后附加所有带有“.png”的元素。如何做到这一点?

我在OSX上测试了这个功能,但在Linux上也应该可以(当然不是在PowerShell中)


cat input.txt | sed-E's!([^看起来您已经得到了一个shell脚本方式的有效答案。如果您打算在Powershell中尝试此方法,您可以使用此方法获得相同的结果

(Get-Content -Raw input.txt).Replace('<gpimage>image1</gpimage>', '<gpimage>image1.png</gpimage>')
(获取内容-Raw input.txt)。替换('image1','image1.png')
我在这里硬编码了image1的值,因为您说这是您唯一想要更改的值,但是如果您想要更改标签之间的任何内容,您可以使用以下内容:

(Get-Content -Raw input.txt) -Replace '<gpimage>([^<]*)</gpimage>','<gpimage>$1.png</gpimage>'

(Get Content-Raw input.txt)-Replace'([^Shell脚本解决方案,我认为在使用
sed
命令将
替换为
.png
时也可以实现这一点,如前面的答案所述。我在
Mac 2.7.1版(388)上测试了这一点
它正在工作。这可能也适用于其他类似unix的终端

cat input.txt | sed 's/<\/gpimage>/.png<\/gpimage>/g'
cat input.txt | sed's//.png/g'

StackOverflow不是脚本编写服务。请说明您尝试了什么,预期的输出是什么,以及尝试的结果是什么。请明确说明您的要求;如果您希望指向其他信息,请明确说明您正在寻找的信息。@JeffZeitlin Hi我很久以前就使用过shell脚本,但我知道这可以很容易地在Shell脚本中完成,实际上我一直在使用notepad++手动进行替换editor@JeffZeitlin请安博迪帮个忙,我被这件事深深打动了如果你的内容真的是这样的话,XML编辑应该用实际的XML感知工具来完成。假设它不是,但是,它是相关的。不,它不会像在Powe中那样工作rShell,但PowerShell确实具有所需的功能,几乎与bash/sed解决方案一样简单。@Elisiano Petrini您的解决方案仅适用于image1,不适用于其他类似的解决方案image2@user5561286您是否将最后的
g
修饰符添加到sed?缩短
sed-E/([^@JeffZeitlin内部的数据并不总是相同的,我只需要将.png附加到像image1这样的元素中,而不是附加到您使用的任何其他元素(),然后您可以删除-raw;)感谢@Esperento57在我发布之前使用measure命令进行了一些非常快速的测试,我注意到通过保留原始字符串而不是让replace遍历对象,性能略有提高。在每次运行十几次之后,两者之间的差异仅为几毫秒,但对于较大的文件,这可能会产生差异o我想我会保留原始数据:)我的测试也是在Linux机器上完成的,尽管在Windows上结果可能不同
cat input.txt | sed 's/<\/gpimage>/.png<\/gpimage>/g'