AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 476352
Accepted
YardenST
YardenST
Asked: 2018-10-19 07:12:37 +0800 CST2018-10-19 07:12:37 +0800 CST 2018-10-19 07:12:37 +0800 CST

sed on OS X - 提取方括号之间的所有文本

  • 772

鉴于此流:

[foo] 123 [bar]
[gar] dsa [har] 345
[uf] 88 [gc] 43 [br]

我想用 sed (或其他任何东西)处理这个,所以输出将是:

foo bar
gar har
uf gc br

我试过了cat myfile | sed -e 's/^.*\[//;s/\].*$//'

但它只给了我最后一个例子。

我真正的输入是这样的:

53f42d4 [the contacts are duplicated] Adding support in picking email verified users [https://trello.com/c/663]
3c454b0 [the contacts are duplicated] splitting contact by phone numbers and emails and changing contact model to contain only 1 email [https://trello.com/c/663]
0e63e5b [we should not let a user confirm his email if we have a user with this confirmed email already] better doc [https://trello.com/c/643]
02671b7 [we should not let a user confirm his email if we have a user with this confirmed email already] preventing updating email if already in used by other user [https://trello.com/c/643]

所以我想获得第一行:

the contacts are duplicated https://trello.com/c/663
sed osx
  • 5 5 个回答
  • 2366 Views

5 个回答

  • Voted
  1. user232326
    2018-10-19T08:21:40+08:002018-10-19T08:21:40+08:00

    这会将第一个(开始)方括号内的任何内容与随后的第一个(结束)方括号匹配多次。

    $ sed 's/[^[]*\[\([^]]*\)\][^[]*/\1 /g' file
    foo bar
    gar har
    uf gc br
    

    描述:

    sed '                      # start a sed script
            s/                 # start a substitute command
            [^[]*              # match all leading characters (except [)
            \[                 # match an explicit [
            \([^]]*\)          # capture text inside brackets.
            \]                 # match the closing ]
            [^[]*              # match trailing text (if any).
            /\1 /              # replace everything matched by the captured text.
            g                  # repeat for all the line.
           ' file              # close script. Apply to file.
    

    这会为每场比赛添加一个尾随空格。如果必须删除,请在末尾添加删除:

    sed -e 's/[^[]*\[\([^]]*\)\][^[]*/\1 /g' -e 's/ $//' file
    

    如果您有 GNU grep,这可能会有所帮助(每次捕获一行)。

    grep -Po '\[\K[^]]*(?=])'
    

    而且,如果上述方法不起作用,awk 也可以这样做:

    awk '{print gensub(/\[([^]]*)\][^[]*/,"\\1 ","g")}' file
    
    • 4
  2. Best Answer
    glenn jackman
    2018-10-19T08:23:16+08:002018-10-19T08:23:16+08:00

    awk 也适用于此:使用[ or ]作为字段分隔符,打印每个偶数字段:

    awk -F '[][]' '{for (i=2; i<=NF; i+=2) {printf "%s ", $i}; print ""}' file
    

    使用 sed,我会写

    sed -E 's/(^|\])[^[]*($|\[)/ /g' file
    
    • 4
  3. wwerner
    2018-10-19T08:26:57+08:002018-10-19T08:26:57+08:00

    一种惯用的方法是使用环视断言,请参阅例如https://www.regular-expressions.info/lookaround.html,但这些在 sed 中不受支持,仅在符合 PCRE 的正则表达式处理器中支持。

    由于 Perl 应该默认在 macOS 上可用,也许这是一个可行的替代方案。

    使用 Perl,你可以说

    perl -pe 's/.+?(?<=\[)(.+?)(?=\]).+?/$1 /g'
    

    (请注意,这会在行尾添加一个空格)

    有关该模式的说明,请参阅https://regexr.com/41gi5。

    • 3
  4. DopeGhoti
    2018-10-19T07:23:50+08:002018-10-19T07:23:50+08:00

    这似乎有效:

    $ sed -E 's/ [^[][a-zA-Z0-9][^]]/ /g;s/ +/ /g' input | tr -d '[]'
    foo bar
    gar har
    uf gc br
    
    • 2
  5. Juergen
    2018-10-19T08:04:43+08:002018-10-19T08:04:43+08:00

    利用:

    sed -n '/\[/ { s-[^[]*--; s-\[\([^]]*\)\][^[]*- \1-g; s- --p }'
    

    算法是:

    • 忽略不包含括号的行。
    • 删除第一个括号之前的文本。
    • 用空格替换成对的括号和可选的尾随文本,将文本留在括号内。
    • 删除初始空格,只留下中间的空格。
    • 1

相关问题

  • 如何使用 `at` 安排脚本在 macOS 上执行?

  • csplit 无法识别提供的正则表达式

  • 根据第一个逗号之前的匹配删除重复行数

  • 如何改进这个字符转换脚本?

  • 如何删除两行之间的单行

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    ssh 无法协商:“找不到匹配的密码”,正在拒绝 cbc

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    如何卸载内核模块“nvidia-drm”?

    • 13 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Wong Jia Hau ssh-add 返回:“连接代理时出错:没有这样的文件或目录” 2018-08-24 23:28:13 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST
  • Martin Hope
    Bagas Sanjaya 为什么 Linux 使用 LF 作为换行符? 2017-12-20 05:48:21 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve