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 / 问题 / 788606
Accepted
learninglinux12345
learninglinux12345
Asked: 2024-12-24 10:59:14 +0800 CST2024-12-24 10:59:14 +0800 CST 2024-12-24 10:59:14 +0800 CST

使用 sed 在文件中的字符串后添加一行

  • 772

我想在文件中查找一个字符串:

例子

https://google.com/first/second/45683

我想重写它并保留原来的行:

https://google.com/first/second/45683
https://yahoo.com/third/fourth/45683

我尝试过几次

sed -r 's@https://google.com/first/second/([a-Z0-9]+)@yahoo.com/third/fourth/\1@' 文件.txt

输出:

yahoo.com/third/fourth/45683

它进行了重写,但没有保留原始行。

另一次尝试

sed -r 'a\s@https://google.com/first/second/([a-Z0-9]+)@yahoo.com/third/fourth/\1@' 文件.txt

输出:

https://google.com/first/second/45683
https://google.com/first/second/([a-Z0-9]+)@yahoo.com/third/fourth/1@

它附加两行但不会重写第二行。

sed
  • 5 5 个回答
  • 160 Views

5 个回答

  • Voted
  1. larsks
    2024-12-24T12:01:54+08:002024-12-24T12:01:54+08:00

    sed在使用命令修改匹配行之前,您需要先打印出匹配的行s//。如下所示:

    sed -E '
      \@https://google.com/first/second/[a-zA-Z0-9]+@ {
        p
        s@https://google.com/first/second/([a-zA-Z0-9]+)@https://yahoo.com/third/fourth/\1@
      }
    ' < data.txt
    

    或者更短一点,

    sed -E '
      \@https://google.com/first/second/([a-zA-Z0-9]+)@ {
        p
        s@@https://yahoo.com/third/fourth/\1@
      }
    ' < data.txt
    

    这表示“对于与表达式匹配的行https://google.com/first/second/[a-zA-Z0-9]+,首先打印出该行,然后使用给定的s//表达式进行修改”。修改后的行会被打印出来,因为这是 的默认行为sed。

    • 4
  2. Gilles Quénot
    2024-12-24T13:56:53+08:002024-12-24T13:56:53+08:00

    保持简单(愚蠢):

    $ sed -r 's@https://google.com/first/second/([a-Z0-9]+)@&\nyahoo.com/third/fourth/\1@' file
    https://google.com/first/second/45683
    yahoo.com/third/fourth/45683
    

    快捷方式&是整个左匹配部分。

    • 2
  3. Best Answer
    hastmu
    2024-12-24T19:36:19+08:002024-12-24T19:36:19+08:00

    看起来像这样:

    echo "https://google.com/first/second/45683" \
    | sed 's@https://\(.*\)/\(.*\)/\(.*\)@https://\1/\2/\3\nhttps://yahoo.com/third/fourth/\3@g'
    

    我得到:

    https://google.com/first/second/45683
    https://yahoo.com/third/fourth/45683
    

    享受。

    • 0
  4. Ed Morton
    2024-12-26T03:53:50+08:002024-12-26T03:53:50+08:00

    使用 GNU awk 将第三个参数设置为match():

    $ awk '
        { print }
        match($0, "^(https://)google[.]com/first/second/([[:alnum:]]+)", a) {
            print a[1] "yahoo.com/third/fourth/" a[2]
        }
    ' file
    https://google.com/first/second/45683
    https://yahoo.com/third/fourth/45683
    
    • 0
  5. Rafael Mori
    2024-12-26T07:49:51+08:002024-12-26T07:49:51+08:00

    你尝试过这个吗?

    sed -i '/https:\/\/google\.com\/first\/second\/45683/a https://yahoo.com/third/fourth/45683' $YOUR_FILE_PATH
    

    通过转义正斜杠来提高清晰度:

    sed -i '/https:\/\/google\.com\/first\/second\/45683/a https:\/\/yahoo\.com\/third\/fourth\/45683' $YOUR_FILE_PATH
    

    希望这有帮助!

    • 0

相关问题

  • Linux grep文件1中的内容在文件2中[重复]

  • 如何在第三个逗号后用条件grep行

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

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

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

Sidebar

Stats

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

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

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

    • 4 个回答
  • Marko Smith

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

    • 5 个回答
  • Marko Smith

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

    • 3 个回答
  • 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
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +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

热门标签

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