[英]Sed find,match and replace
So I have the following script (just a test) 所以我有以下脚本(只是一个测试)
#!/bin/bash
for file in *
do
echo $file
done
And I'm trying to add parenthesis around file. 我正在尝试在文件周围添加括号。 I'm also pretending I dont know the full name of the variable file.
我还假装我不知道变量文件的全名。
cat sc.sh | sed 's/fi*/(&)/g'
#!/bin/bash
(f)or (fi)le in *
do
echo $(fi)le
done
So basically I'm trying to match words beginning with fi and adding parenthesis around them. 所以基本上我想匹配以fi开头的单词,并在它们周围加上括号。 What am I doing wrong?
我究竟做错了什么? I tried a number of variations to that but it didn't work.
我为此尝试了多种变体,但没有成功。 From what I can see the command matches any f or fi and adds parenthesis around them but not the whole word.
从我可以看到的命令匹配任何f或fi,并在它们周围添加括号,但不是整个单词。 Why?
为什么?
Any help would be greatly appreciated. 任何帮助将不胜感激。
Your regex fi*
is looking for an f followed by 0 or more i's. 您的正则表达式
fi*
正在寻找f,然后是0或更大的i。 You probably want something more like this: 您可能想要更多这样的东西:
cat tmp | sed 's/\bfi[^ ]*/(&)/g'
\\bfi
looks for a word boundary (ie the start of a word) followed by 'fi'. \\bfi
查找单词边界(即单词的开头),后跟“ fi”。 Then [^ ]*
matches the remaining (non-space) characters in the word. 然后
[^ ]*
匹配单词中其余的(非空格)字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.