开发者

Using sed to delete a case insensitive matched line

开发者 https://www.devze.com 2022-12-18 05:15 出处:网络
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\"

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开发者_StackOverflow社区 following but it didn't work

sed -e "/pattern/replace/id" filepath > newfilepath


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

sed -e "/pattern/Id" filepath


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"


I produced this one-liner because Ansible cannot handle different lv with the same name. This convert near CSV into perfect JSON. Possibly, you want to change the -F flag to change the field separator.

lvs | perl -ane '
   local %tmp,$i=0;
   while($i<@f){
     $tmp{$f[$i]}=$F[$i] if $F[$i];
     $i++
   };
   if(@f){push @ans,\%tmp}
   else{ @f=@F }; 
   END { print to_json(\@ans,{pretty=>1}) }
' -MJSON
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号