简体   繁体   English

Shell脚本,用于替换文件夹中所有文件中的令牌

[英]shell scripting for token replacement in all files in a folder

HI

I am not very good with linux shell scripting.I am trying following shell script to replace 我对Linux Shell脚本不是很好,我正在尝试按照Shell脚本来替换

revision number token $rev -<rev number> in all html files under specified directory 指定目录下所有html文件中的修订号令牌$rev -<rev number>

cd /home/myapp/test
set repUpRev = "`svnversion`"
echo $repUpRev
grep -lr -e '\$rev -'.$repUpRev.'\$' *.html | xargs sed -i 's/'\$rev -'.$repUpRev.'\$'/'\$rev -.*$'/g'

This seems not working, what is wrong with the above code ? 这似乎不起作用,上述代码有什么问题?

rev=$(svnversion)
sed -i.bak "s/$rev/some other string/g" *.html

What is $rev in the regexp string? regexp字符串中的$ rev是什么? Is it another variable? 它是另一个变量吗? Or you're looking for a string '$rev'. 或者,您正在寻找字符串'$ rev'。 If latter - I would suggest adding '\\' before $ otherwise it's treated as a special regexp character... 如果是后者-我建议在$之前添加“ \\”,否则将被视为特殊的正则表达式字符...

This is how you show the last line: 这是显示最后一行的方式:

grep -lr -e '\$rev -'.$repUpRev.'\$' *.html | xargs sed -i 's/'\$rev -'.$repUpRev.'\$'/'\$rev -.*$'/g'

It would help if you showed some input data. 如果您显示一些输入数据,将会有所帮助。

The -r option makes the grep recursive. -r选项使grep递归。 That means it will operate on files in the directory and its subdirectories. 这意味着它会在目录及其子目录中的文件进行操作。 Is that what you intend? 那是你打算的吗?

The dots in your grep and sed stand for any character. grepsed的点代表任何字符。 If you want literal dots, you'll need to escape them. 如果需要文字点,则需要对其进行转义。

The final escaped dollar sign in the grep and sed commands will be seen as a literal dollar sign. grepsed命令中最后一个转义的美元符号将被视为文字美元符号。 If you want to anchor to the end of the line you should remove the escape. 如果要锚定到行尾,则应删除转义符。

The .* works only as a literal string on the right hand side of a sed s command. .*仅作为sed s命令右侧的文字字符串。 If you want to include what was matched on the left side, you need to use capture groups. 如果要包括左侧匹配的内容,则需要使用捕获组。 The g modifier on the s command is only needed if the pattern appears more than once in a line. 仅当模式在一行中出现多次时,才需要s命令上的g修饰符。

Using quote, unquote, quote, unquote is hard to read. 使用引号,取消引用,引用,取消引用很难阅读。 Use double quotes to permit variable expansion. 使用双引号允许变量扩展。

Try your grep command by itself without the xargs and sed to see if it's producing a list of files. 单独尝试不带xargssedgrep命令,以查看它是否正在生成文件列表。

This may be closer to what you want: 这可能更接近您想要的:

grep -lr -e "\$rev -.$repUpRev.$" *.html | xargs sed -i "s/\$rev -.$repUpRev.$/\$rev -REPLACEMENT_TEXT/g"

but you'll still need to determine if the g modifier, the dots, the final dollar signs, etc., are what you intend. 但您仍然需要确定g修饰符,点,最后的美元符号等是否符合您的要求。

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

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