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 / 问题 / 432957
Accepted
msampaio
msampaio
Asked: 2018-03-23 14:47:33 +0800 CST2018-03-23 14:47:33 +0800 CST 2018-03-23 14:47:33 +0800 CST

如何用 sed 查找和替换特定行的特定部分?

  • 772

我有一个包含以下行的LaTeX文件:foo.tex

\includegraphics[width=0.49\textwidth]{img1}
\includegraphics{img2}
\subfloat{\includegraphics[width=0.3\textwidth]{img3}}
\subfloat{\includegraphics{img4}}

我想替换文件中的这些行以添加文件扩展名,如下所示:

\includegraphics[width=0.49\textwidth]{img1.png}
\includegraphics{img2.png}
\subfloat{\includegraphics[width=0.3\textwidth]{img3.png}}
\subfloat{\includegraphics{img4.png}}

我开始尝试使用sed此命令:

cat foo.tex | sed 's/\(includegraphics.*[a-z,0-9,\-]}\)/\1.png}/g' > bar.tex

但它返回:

\includegraphics[width=0.49\textwidth]{img1}.png
\includegraphics{img2}.png
\subfloat{\includegraphics[width=0.3\textwidth]{img3}.png}
\subfloat{\includegraphics{img4}.png}

如何修复我的sed命令以添加文件扩展名?

编辑

图像文件名不仅是 img1、img2 等。

sed latex
  • 1 1 个回答
  • 63 Views

1 个回答

  • Voted
  1. Best Answer
    DopeGhoti
    2018-03-23T15:03:36+08:002018-03-23T15:03:36+08:00
    $ sed '/\\includegraphics/s/img[0-9]*/&.png/g' input
    \includegraphics[width=0.49\textwidth]{img1.png}
    \includegraphics{img2.png}
    \subfloat{\includegraphics[width=0.3\textwidth]{img3.png}}
    \subfloat{\includegraphics{img4.png}}
    

    替换操作的正则表达式查找img后跟任意数量的数字(包括零位),并附.png加到匹配的末尾。

    一个稍微更健壮的版本,它只会在花括号内执行替换:

    $ sed '/\\includegraphics/s/{\(img[0-9]*\)}/{\1.png}/g' input
    \includegraphics[width=0.49\textwidth]{img1.png}
    \includegraphics{img2.png}
    \subfloat{\includegraphics[width=0.3\textwidth]{img3.png}}
    \subfloat{\includegraphics{img4.png}}
    

    这会在{img3}任何\includegraphics包含.img3.png{img3.png}

    • 3

相关问题

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

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

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

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

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

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