Case insensitive search matching with sed?

依然范特西╮ 提交于 2019-12-23 07:05:00

问题


I'm trying to use SED to extract text from two words, such as "Account" and "Recognized", and I'd like that the searching be case insensitive. So I tried to use the I parameter, but receive this error message:

cat Security.txt | sed -n "/Account/,/Recognized/pI" | sed -e '1d' -e '$d' sed: -e expression #1, char 24: extra characters after command


回答1:


Use:

sed -n "/Account/,/Recognized/Ip"

i.e. change the order to: Ip instead of pI




回答2:


Avoid useless use of cat

/pattern/I is how to specify case-insensitive matching in sed

sed -n "/Account/I,/Recognized/Ip" Security.txt | sed -e '1d' -e '$d'

You can use single sed command to achieve the same:

sed -n '/account/I,/recognized/I{/account/I!{/recognized/I!p}}' Security.txt

Or awk

awk 'BEGIN{IGNORECASE=1} /account/{f=1; next} /recognized/{f=0} f' Security.txt

Reference:

  • How to select lines between two patterns?



回答3:


You have useless use of cat where you should've fed the file directly to sed. Below could be a way of doing it.

$ cat file.txt 
Some stuff Account sllslsljjs Security.
Another stuff account name and ffss security.
$ sed -nE 's/^.*account[[:blank:]]*(.*)[[:blank:]]*security.*$/\1/pI' file.txt
sllslsljjs 
name and ffss 

The [[:blank:]]* is greedy and will strip the spaces before and after the required text. The -E option enables the use of extended regular expressions.



来源:https://stackoverflow.com/questions/39178188/case-insensitive-search-matching-with-sed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!