简体   繁体   English

bash脚本中的sed regexp

[英]sed regexp in a bash script

I want to extract a certain part of a string, if it exists. 我想提取字符串的某个部分(如果存在)。 I'm interested in the xml filename, ie i want whats between an "_" and ".xml". 我对xml文件名感兴趣,即我想要“ _”和“ .xml”之间的内容。

This is ok, it prints "555" 可以,它打印“ 555”

MYSTRING=`echo "/sdd/ee/publ/xmlfile_555.xml" | sed 's/^.*_\([0-9]*\).xml/\1/'`
echo "STRING = $MYSTRING"

This is not ok because it returns the whole string. 这不行,因为它返回整个字符串。 In this case I don't want any result. 在这种情况下,我不希望有任何结果。 It prints "/sdd/ee/publ/xmlfile.xml" 它显示“ /sdd/ee/publ/xmlfile.xml”

MYSTRING=`echo "/sdd/ee/publ/xmlfile.xml" | sed 's/^.*_\([0-9]*\).xml/\1/'`
echo "STRING = $MYSTRING"

Any ideas how to get an "empty" result in the second case. 在第二种情况下,如何获得“空”的任何想法都会产生。

thanks! 谢谢!

You just need to tell sed to keep its mouth shut if it doesn't find a match. 如果找不到匹配项,您只需要告诉sed闭嘴即可。 The -n option is used for that. -n选项用于此目的。

MYSTRING=`echo "/sdd/ee/publ/xmlfile_555.xml" | sed -n 's/^.*_\([0-9]*\)\.xml/\1/p'`

I only made two changes to what you had: the aforementioned -n option to sed , and the p flag that comes after the s/// command, which tells sed to print the output only if the substitution was successfully done. 我仅对您的内容进行了两项更改:前面提到的sed -n选项,以及在s///命令之后的p标志,它告诉sed仅在成功完成替换后才输出输出。

EDIT : I've also escaped the final . 编辑 :我也逃脱了决赛. as suggested in the comments. 正如评论中所建议的那样。

Try this? 尝试这个?

basename /sdd/ee/publ/xmlfile_555.xml | awk -F_ '{print $2}'

The output is 555.xml 输出为555.xml

With the other one. 与另一个。

basename /sdd/ee/publ/xmlfile.xml | awk -F_ '{print $2}'

The output is an empty string. 输出为空字符串。

$ path=/sdd/ee/publ/xmlfile_555.xml
$ echo ${path##*/}
xmlfile_555.xml
$ path=${path##*/}
$ echo ${path%.xml}
xmlfile_555
$ path=${path%.xml}
$ echo ${path##*_}
555

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

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