[英]Bash: String manipulation with sed and Regular expression is not working: replace a string by slash
I hope you can help me out: 希望您能帮帮我:
Here is one of my lines that I have to string manipulate: 这是我必须进行字符串操作的行之一:
./period/0.0.1/projectname/path/path/-rw-rw-r--filename.txt 2462
Where the last number is the file size and needed for later calculations. 最后一个数字是文件大小,以后的计算需要此文件。
The sequence -rw-rw-r--
is from a file listing Output where I separated files from directories and skipped all lines starting with "d". 序列
-rw-rw-r--
来自文件列表Output,其中我从目录中分离了文件,并跳过了所有以“ d”开头的行。 Now I need to get rid of the rights sequence in the lines. 现在,我需要摆脱各行中的权限顺序。
Here is my regex, that exactly hits that target: [/][-][-rwx]{9,9}
I checked tat with a regex checker and get exact what I want: the string /- including the following 9 characters. 这是我的正则表达式,正好达到了该目标:
[/][-][-rwx]{9,9}
我用正则表达式检查器检查了tat并得到了我想要的确切内容:字符串/-,包括以下9个字符。
What I want: replace this string " /- including the following 9 characters " by a single slash /. 我想要的是:用单斜杠/替换字符串“ /-包括以下9个字符”。 To avoid escaping I use pipe as separator in sed.
为了避免转义,我在sed中使用管道作为分隔符。 The following sed command is working correct:
以下sed命令运行正常:
sed 's|teststring|/|g' inputfile > outputfile
The problem: 问题:
When I replace "teststring" bei my regex it is not manipulating anything: 当我在我的正则表达式中替换“ teststring”时,它没有进行任何操作:
sed 's|[/][-][-rwx]{9,9}|/|g' inputfile > outputfile
I get no errors at all, but have no stringmanipulations in result outputfile. 我完全没有错误,但是结果输出文件中没有任何字符串操作。
What am I doing wrong here?? 我在这里做错了什么?
Please help! 请帮忙!
You can use this sed
with extended regex: 您可以将此
sed
与扩展正则表达式一起使用:
sed -E 's|/-[-rwx]{9}|/|g' file
./period/0.0.1/projectname/path/path/filename.txt 2462
[/]
and [-]
in your regex [/]
和[-]
-E
for extended regex matching -E
扩展正则表达式匹配 .{9,9}
is same as .{9}
.{9,9}
与.{9}
相同 You may use 您可以使用
sed 's|/-[-rwx]\{9\}|/|g'
Note that in POSIX BRE patterns, in order to specify a limiting quantifier, you need to escape the braces. 请注意,在POSIX BRE模式中,为了指定限制量词,您需要对括号进行转义。
See the Bash demo : 参见Bash演示 :
s='./period/0.0.1/projectname/path/path/-rw-rw-r--filename.txt 2462'
echo $s | sed 's|/-[-rwx]\{9\}|/|g'
Output: 输出:
./period/0.0.1/projectname/path/path/filename.txt 2462
NOTE : It is not a good idea to wrap each individual char with a bracket expression, [/]
= /
and [-]
= -
. 注意 :用括号表达式
[/]
= /
和[-]
= -
来包装每个字符不是一个好主意。
sed
uses the BRE regex flavour by default, where braces should be escaped. sed
默认情况下使用BRE regex样式,其中的括号应转义。
Either escape them : 要么逃脱他们:
sed 's|[/][-][-rwx]\{9,9\}|/|g' inputfile > outputfile
Or switch to ERE : 或切换到ERE:
sed -n 's|[/][-][-rwx]{9,9}|/|g' inputfile > outputfile # for GNU sed
sed -E 's|[/][-][-rwx]{9,9}|/|g' inputfile > outputfile # for BSD sed & modern GNU sed
As a side note, your regex can be simplified to /-[-rwx]{9}
: 另外,您的正则表达式可以简化为
/-[-rwx]{9}
:
sed -E 's|/-[-rwx]{9}|/|g' inputfile > outputfile
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.