bash - 提取字符串的一部分

时间:2016-07-18 00:55:39

标签: regex bash sed

我有一个像这样的字符串

xsd:import schemaLocation="AppointmentManagementService.xsd6.xsd" namespace=

我想从中提取以下内容:

AppointmentManagementService.xsd6.xsd

我尝试过使用正则表达式,bash和sed但没有成功。有人可以帮我解决这个问题吗?

我使用的正则表达式是:

/AppointmentManagementService.xsd\d{1,2}.xsd/g

6 个答案:

答案 0 :(得分:2)

你的字符串是:

nampt@nampt-desktop:$ cat 1
xsd:import schemaLocation="AppointmentManagementService.xsd6.xsd" namespace=

尝试使用awk:

cat 1 | awk -F "\"" '{print $2}'

输出:

AppointmentManagementService.xsd6.xsd

答案 1 :(得分:1)

id | sid | product --------------------- 1 | ABC | 1 2 | ABC | 2 3 | ABC | 3 6 | XYZ | 2 7 | XYZ | 3 无法识别sed,请改为使用\d[0-9]

[[:digit:]]

答案 2 :(得分:1)

您可以使用bash本机正则表达式匹配:

$ in='xsd:import schemaLocation="AppointmentManagementService.xsd6.xsd" namespace='

$ if [[ $in =~ \"(.+)\" ]]; then echo "${BASH_REMATCH[1]}"; fi

输出:

  

AppointmentManagementService.xsd6.xsd

根据您的示例,如果您要在.xsd...组件中至少授予1个或最多2个数字,则可以使用以下方法对正则表达式进行微调:

$ if [[ $in =~ \"(AppointmentManagementService.xsd[0-9]{1,2}.xsd)\" ]]; then echo "${BASH_REMATCH[1]}"; fi

答案 3 :(得分:1)

GNU中使用PCRE $stmt = $dbh->query($sqlx); if($stmt!=false){ $result = $stmt->fetchAll(PDO::FETCH_ASSOC); }

grep

这将输出匹配grep -oP 'schemaLocation="\K.*?(?=")' 和下次出现的schemaLocation="

之间的模式

参考:

https://unix.stackexchange.com/a/13472/109046

答案 4 :(得分:1)

我们也可以使用' cut'为此目的命令,

local&web

答案 5 :(得分:0)

s='xsd:import schemaLocation="AppointmentManagementService.xsd6.xsd" namespace='
echo $s | sed 's/.*schemaLocation="\(.*\)" namespace=.*/\1/'