Using sed to delete a case insensitive matched line

三世轮回 提交于 2019-11-29 10:41:30

问题


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

回答1:


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

sed -e "/pattern/Id" filepath



回答2:


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"


来源:https://stackoverflow.com/questions/2157288/using-sed-to-delete-a-case-insensitive-matched-line

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