Why doesn't this simple RegEx work with sed?
来源:互联网 发布:seo的优化 编辑:程序博客网 时间:2024/05/01 09:29
Why doesn't this simple RegEx work with sed?
This is a really simple RegEx that isn't working, and I can't figure out why. According tothis, it should work.
I'm on a Mac (OS X 10.8.2).
script.sh
#!/bin/bashZIP="software-1.3-licensetypeone.zip"VERSION=$(sed 's/software-//g;s/-(licensetypeone|licensetypetwo).zip//g' <<< $ZIP)echo $VERSION
terminal
$ sh script.sh1.3-licensetypeone.zip
4 Answers
Looking at the regex documentation for OS X 10.7.4 (but should apply to OP's 10.8.2), it is mentioned in the last paragraph that
Obsolete (basic) regular expressions differ in several respects.
|
is an ordinary character and there is no equivalent for its functionality...... The parentheses for nested subexpressions are
\(' and
)'...
sed
, without any options, uses basic regular expression (BRE).
To use |
in OS X or BSD's sed
, you need to enable extended regular expression (ERE) via-E
option, i.e.
sed -E 's/software-//g;s/-(licensetypeone|licensetypetwo).zip//g'
p/s: \|
in BRE is a GNU extension.
Alternative ways to extract version number
chop-chop (parameter expansion)
VERSION=${ZIP#software-}VERSION=${VERSION%-license*.zip}
sed
VERSION=$(sed 's/software-\(.*\)-license.*/\1/' <<< "$ZIP")
You don't necessarily have to match strings word-by-word with shell patterns or regex.
sed
works with simple regular expressions. You have to backslash parentheses and a vertical bar to make it work.
sed 's/software-//g;s/-\(licensetypeone\|licensetypetwo\)\.zip//g'
Note that I backslashed the dot, too. Otherwise, it would have matched any character.
sed
support ;
to separate commands? In some versions, multiple-e
are needed: sed -e 's///' -e 's///'
.– chorobaNov 12 '12 at 13:14;
. I'm using that elsewhere.– curtisblackwellNov 12 '12 at 13:16<<< "$ZIP"
.– chorobaNov 12 '12 at 13:18You can do this in the shell, don't need sed, parameter expansion suffices:
shopt -s extglobZIP="software-1.3-licensetypeone.zip"tmp=${ZIP#software-}VERSION=${tmp%-licensetype@(one|two).zip}
With a recent version of bash (may not ship with OSX) you can use regular expressions
if [[ $ZIP =~ software-([0-9.]+)-licensetype(one|two).zip ]]; then VERSION=${BASH_REMATCH[1]}fi
or, if you just want the 2nd word in a hyphen-separated string
VERSION=$(IFS=-; set -- $ZIP; echo $2)
man sed
and (perhaps) man ed
to learn about regexes implemented by sed.– glenn jackmanNov 13 '12 at 1:03$ man sed | grep "regexp-extended" -A2 -r, --regexp-extended use extended regular expressions in the script.
Not the answer you're looking for? Browse other questions taggedregex sed or ask your own question.
- Why doesn't this simple RegEx work with sed?
- Why dynamic_cast doesn't work ?
- ROOT: Why TStyle::SetStatFontSize() doesn't work?
- Makefile: ifeq directive doesn't work with $*
- Why doesn't `sudo cd /var/named` work?
- MSTest doesn’t work
- set_terminate doesn't work
- vi doesn't work
- cscope doesn't work with symbolic link file
- Spring boot @Qualifier doesn't work with datasources
- File@CompanionFile doesn't work
- LESSON: Why Eclipse doesn't show SVN updates information with Subversive Plugin
- SequenceFile doesn't work with GzipCodec without native-hadoop code的解决办法
- Hadoop | SequenceFile doesn't work with GzipCodec without native-hadoop code 异常解决
- Why doesn't Linux need defragmenting?
- Why doesn't Linux need defragmenting?
- Why doesn't my new Favicon display?
- Why doesn't C++ provide heterogenous containers?
- STL系列之三 queue 单向队列
- ASP.NET偷懒之C#代码生成器大全
- 配置开发环境和Android Tools
- QT Model/View
- 学习C语言基本思路与参考书籍
- Why doesn't this simple RegEx work with sed?
- hdu 3833 YY's new problem(hash表)
- STL系列之四 heap 堆
- matlab小波分析工具箱常用函数
- RMAN恢复目录(Recovery Catalog)
- Specular Reflection Models
- Vm虚拟机中Linux扩展磁盘空间
- STL系列之五 priority_queue 优先级队列
- 广播接收器—Broadcast Receiver(一)