Distributions
› Ubuntu
› Fedora
› CentOS
中文资源站
› 网易开源镜像站
hailongs
V2EX  ›  Linux

请教 shell 的朋友帮忙处理个字符串

  •  
  •   hailongs · Nov 2, 2016 · 3329 views
    This topic created in 3655 days ago, the information mentioned may be changed or developed.

    ......&key=xxxxx&name=..........

    有很多这样的行,我想用 shell 取到 key=xxxxx ,其他的多余字段不要。怎么操作。

    xxxxx 是变量,每行都不同。谢谢!

    6 replies  •  2016-11-03 09:37:25 +08:00
    giskard
    giskard
    Nov 2, 2016
    1
    cut -d '&' -f2 xxx
    xxx 是你的文件名,-d '&' 表示以 '&' 作为字段分隔符,-f2 表示取第二个字段
    rrfeng
    rrfeng
    Nov 2, 2016
    2
    grep -Po 'key=[^&]*'
    hailongs
    hailongs
    OP
    Nov 2, 2016
    3
    @rrfeng 感谢。
    SoloCompany
    SoloCompany
    Nov 2, 2016
    4
    -P 不是所有 grep 都支持的吧,比如 macOS 就不支持
    用 grep -o '&key=[^&]*' | cut -c5- 就好了
    yuedingwangji
    yuedingwangji
    Nov 3, 2016
    5
    grep -oP '(?<=&)key=.*(?=&)'
    uuspider
    uuspider
    Nov 3, 2016 via iPhone
    6
    tr '&' '\n' | grep 'key='
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2621 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 14:08 · PVG 22:08 · LAX 06:08 · JFK 09:08
    ♥ Do have faith in what you're doing.