Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using sed to delete a case insensitive matched line

How do I match a case insensitive regex and delete it at the same time

I read that to get case insensitive matches, use the flag "i"

sed -e "/pattern/replace/i" filepath 

and to delete use d

sed -e "/pattern/d" filepath 

I've also read that I could combine multiple flags like 2iw

I'd like to know if sed could combine both i and d I've tried the following but it didn't work

sed -e "/pattern/replace/id" filepath > newfilepath 
like image 936
eruina Avatar asked Jan 28 '10 19:01

eruina


People also ask

How do I delete a sed matching line?

To begin with, if you want to delete a line containing the keyword, you would run sed as shown below. Similarly, you could run the sed command with option -n and negated p , (! p) command. To delete lines containing multiple keywords, for example to delete lines with the keyword green or lines with keyword violet.

How do I make sed not case sensitive?

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 grep a case insensitive?

Case Insensitive Search By default, grep is case sensitive. This means that the uppercase and lowercase characters are treated as distinct. To ignore case when searching, invoke grep with the -i option (or --ignore-case ).


2 Answers

For case-insensitive use /I instead of /i.

sed -e "/pattern/Id" filepath 
like image 195
Mark Byers Avatar answered Sep 22 '22 10:09

Mark Byers


you can use (g)awk as well.

# print case insensitive awk 'BEGIN{IGNORECASE=1}/pattern/{print}' file  # replace with case insensitive awk 'BEGIN{IGNORECASE=1}/pattern/{gsub(/pattern/,"replacement")}1' file 

OR just with the shell(bash)

#!/bin/bash shopt -s nocasematch while read -r line do     case "$line" in         *pattern* ) echo $line;     esac done <"file" 
like image 36
ghostdog74 Avatar answered Sep 18 '22 10:09

ghostdog74