使用sed在两个斜杠之间提取字符串

use*_*118 3 regex bash shell text-processing sed

我正在尝试使用 sed 从文件中的一行中提取特定字符串。目前我正在使用 while 循环读取文件并搜索特定字符串。当找到该字符串时,我正在提取它,但是我需要使用 sed 来解析输出,以便我只获取两个斜杠之间的字符串(它是一个目录名,所以如果可能的话,我需要同时保留开头和结尾的斜杠)。这是我正在运行以搜索文件的循环:

#!/bin/sh
file=configFile.conf
while read line 
do
    if  echo "$line" | grep -q "directory_root" 
    then DIR_ROOT="$line"
fi
done < "$file"
echo $DIR_ROOT
exit 0
Run Code Online (Sandbox Code Playgroud)

while 循环起作用并回显以下字符串:

directory_root /root/config/data/
Run Code Online (Sandbox Code Playgroud)

然后我需要使用 sed 以获得以下输出,以便将正确的目录名称传递给另一个脚本:

/root/
Run Code Online (Sandbox Code Playgroud)

是否可以使用 sed 和正则表达式从回显输出中仅提取上述内容?

谢谢

Har*_*nry 7

如果你想使用sed,这会起作用:

~/tmp> str="directory_root /root/config/data/"
~/tmp> echo $str | sed 's|^[^/]*\(/[^/]*/\).*$|\1|'
/root/
Run Code Online (Sandbox Code Playgroud)

或单个班轮(假设 directory_root 文字在行中:)

 cat file | sed -e 's|^directory_root \(/[^/]*/\).*$|\1|;tx;d;:x'
Run Code Online (Sandbox Code Playgroud)

第一个例子中正则表达式的解释:

s|: 使用|作为分隔符(在这种情况下更容易阅读)

^ : 匹配行首

[^/]*: 匹配所有非/字符(这是贪婪的,所以它会在遇到第一个/.

\( : 开始录制字符串 1

/ : 匹配文字 /

[^/]*: 匹配所有非/字符

\) : 完成记录字符串 1

.* : 匹配所有其他内容到行尾

| : 分隔符

\1 : 用字符串 1 替换匹配

| : 分隔符

在第二个示例中,我附加了;tx;d;:x哪些不回显不匹配的行,请参见此处。然后你可以在整个文件上运行它,它只会打印它修改的行。

~/tmp> echo "xx" > tmp.txt
~/tmp> echo "directory_root /root/config/data/" >> tmp.txt
~/tmp> echo "xxxx ttt" >> tmp.txt
~/tmp>
~/tmp> cat tmp.txt | sed -e 's|^directory_root \(/[^/]*/\).*$|\1|;tx;d;:x'
/root/
Run Code Online (Sandbox Code Playgroud)