sed 删除匹配行 ? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
station
V2EX    Linux

sed 删除匹配行 ?

  •  
  •   station Jan 8, 2021 3907 views
    This topic created in 1937 days ago, the information mentioned may be changed or developed.
    自定义 openwrt 默认设置

    如何删除 dnsmasq 段落中的某一行

    sed 试了试,我搞不定

    config dnsmasq
    option domainneeded '1'
    option localise_queries '1'
    option rebind_protetion '1'
    option rebind_localhost '1'
    option local '/lan/'
    option expandhosts '1'
    option authoritative '1'
    option readethers '1'
    option leasefile '/tmp/dhcp.leases'
    option resolvfile '/tmp/resolv.conf.d/resolv.conf.auto'
    option nonwildcard '1'
    option localservice '1'
    option filter_aaaa '1'
    option domain 'lan'

    config dhcp 'lan'
    option interface 'lan'
    option start '100'
    option limit '150'
    option leasetime '12h'

    config dhcp 'wan'
    option interface 'wan'
    option ignore '1'

    config srvhost
    option srv '_vlmcs._tcp'
    option target 'OpenWrt'
    option port '1688'
    option class '0'
    option weight '100'
    17 replies    2021-01-08 19:26:24 +08:00
    qakito
        1
    qakito  
       Jan 8, 2021
    uci 配置文件有提供统一的读写接口:
    Usage: uci [<options>] <command> [<arguments>]

    Commands:
    batch
    export [<config>]
    import [<config>]
    changes [<config>]
    commit [<config>]
    add <config> <section-type>
    add_list <config>.<section>.<option>=<string>
    del_list <config>.<section>.<option>=<string>
    show [<config>[.<section>[.<option>]]]
    get <config>.<section>[.<option>]
    set <config>.<section>[.<option>]=<value>
    delete <config>[.<section>[[.<option>][=<id>]]]
    rename <config>.<section>[.<option>]=<name>
    revert <config>[.<section>[.<option>]]
    reorder <config>.<section>=<position>

    Options:
    -c <path> set the search path for config files (default: /etc/config)
    -d <str> set the delimiter for list values in uci show
    -f <file> use <file> as input instead of stdin
    -m when importing, merge data into an existing package
    -n name unnamed sections on export (default)
    -N don't name unnamed sections
    -p <path> add a search path for config change files
    -P <path> add a search path for config change files and use as default
    -q quiet mode (don't print error messages)
    -s force strict mode (stop on parser errors, default)
    -S disable strict mode
    -X do not use extended syntax on 'show'
    Raos
        2
    Raos  
       Jan 8, 2021
    sed '/local/d'
    lihuoqingfly
        3
    lihuoqingfly  
       Jan 8, 2021
    参考手册: https://quickref.me/sed
    FurN1
        4
    FurN1  
       Jan 8, 2021 via iPhone
    你还可以试试 awk 啊
    station
        5
    station  
    OP
       Jan 8, 2021
    @Raos sed '/option domain 'lan'/d' dhcp 不行哦~
    diaryevil
        6
    diaryevil  
       Jan 8, 2021
    @station 四个单引号上转义符啊
    zent00
        7
    zent00  
       Jan 8, 2021
    比如删除 option rebind_localhost '1' 这一行:

    sed -i "/option rebind_localhost '1'/d" dnsmasq.conf
    FurN1
        8
    FurN1  
       Jan 8, 2021 via iPhone   1
    看了楼主在 5 楼的代码,我只能说

    君 regex 本当上手

    另外根据你系统不一样,shell 不一样,有时候要用 sed -e,有时候对于双引号的严格性也不同。这些信息也不写在问题里……
    FurN1
        9
    FurN1  
       Jan 8, 2021 via iPhone
    纠正,是-E,不是小写
    6IbA2bj5ip3tK49j
        10
    6IbA2bj5ip3tK49j  
       Jan 8, 2021 via iPhone
    目测一楼才是正解
    Raos
        11
    Raos  
       Jan 8, 2021
    @station sed "/option domain 'lan'/d"
    @zent00 正解
    Raos
        12
    Raos  
       Jan 8, 2021
    @lihuoqingfly 收藏了
    tachikomchann
        13
    tachikomachann  
       Jan 8, 2021 via Android
    sed 不知道,但是 vim 的话,用 g 命令可以
    :g/要匹配的关键字 /d
    aibangjuxin
        14
    aibangjuxin  
       Jan 8, 2021
    考虑效率?
    cat a|sed -e '/./{H;$!d;}' -e 'x;/dnsmasq/!d;'|sed '/domainneeded/d'
    domainneeded 为要删除的行
    doumeki
        15
    doumeki  
       Jan 8, 2021
    说个题外话,如果是编译之前自定义,还是写成 patch 吧。
    运行时的话使用一楼说的 uci.

    PS: 我自己写了改了几十个编译自定义文件,全是用的 patch 方式。
    fo0o7hU2tr6v6TCe
        16
    fo0o7hU2tr6v6TCe  
       Jan 8, 2021
    cat xx | sed "/option leasefile '\/tmp\/dhcp.leases'/d"
    byaiu
        17
    byaiu  
       Jan 8, 2021 via iPhone
    忘了-i 了吧
    About     Help     Advertise     Blog     API     FAQ     Solana     3251 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 54ms UTC 12:22 PVG 20:22 LAX 05:22 JFK 08:22
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86