user2965031
user2965031

Reputation: 71

Case insensitive search matching with sed?

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

Upvotes: 2

Views: 4672

Answers (3)

Мона_Сах
Мона_Сах

Reputation: 322

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.

Upvotes: -1

Sundeep
Sundeep

Reputation: 23697

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:

Upvotes: 6

dbosky
dbosky

Reputation: 1641

Use:

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

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

Upvotes: 1

Related Questions