Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use sed with ignore case while adding text before some pattern

Tags:

linux

unix

sed

sed -i '/first/i This line to be added' 

In this case,how to ignore case while searching for pattern =first

like image 329
MaNn Avatar asked Jun 04 '13 12:06

MaNn


People also ask

How do I ignore a case in sed?

Ignore case while replacing sed by default is case sensitive. To ignore the case -i flag can be used with sed command.

Which I command is used for case insensitivity in sed command?

GNU sed and other version does support a case-insensitive search using I flag after /regex/.

How do you make an awk case-insensitive?

You can make the awk to do case insensitive and match even for the words like “Iphone” or “IPHONE” etc. The IGNORECASE is a built in variable which can be used in awk command to make it either case sensitive or case insensitive. If the IGNORECASE value is 0, then the awk does a case sensitive match.

How do you make a case-insensitive in Unix?

The key to that case-insensitive search is the use of the -iname option, which is only one character different from the -name option. The -iname option is what makes the search case-insensitive.


1 Answers

You can use the following:

sed 's/[Ff][Ii][Rr][Ss][Tt]/last/g' file

Otherwise, you have the /I and n/i flags:

sed 's/first/last/Ig' file

From man sed:

I

i

The I modifier to regular-expression matching is a GNU extension which makes sed match regexp in a case-insensitive manner.

Test

$ cat file
first
FiRst
FIRST
fir3st
$ sed 's/[Ff][Ii][Rr][Ss][Tt]/last/g' file
last
last
last
fir3st
$ sed 's/first/last/Ig' file
last
last
last
fir3st
like image 171
fedorqui 'SO stop harming' Avatar answered Sep 29 '22 17:09

fedorqui 'SO stop harming'