regex - bash - 如何从 bash 脚本中将换行符传递给 perl?

标签 regex linux bash perl linefeed

<分区>

我正在尝试替换所有数字序列,后跟换行符和字母 a。这些数字序列位于名为 test.txt 的文件中。 bash 脚本 command.sh 用于执行任务(见下文)。

测试.txt

00
a1
b2
a

command.sh

#!/bin/bash

MY_VAR="\d+
a"

grep -P "^.+$" test.txt | perl -pe "s/$MY_VAR/D/";

当我调用 command.sh 文件时,这是我看到的:

$ ./command
00
a1
b2
a

但是,我期待这样的输出:

$ ./command
D1
bD

我错过了什么?

最佳答案

你甚至不需要 grep 因为它只是匹配 .+,只需使用 perl-0777 选项(slurp 模式)以跨行匹配:

#!/bin/bash

MY_VAR="\d+
a"

perl -0777pe "s/$MY_VAR/D/g" test.txt

输出:

D1
bD

关于regex - bash - 如何从 bash 脚本中将换行符传递给 perl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38642827/

相关文章:

python - 从pycharm中重组的文本文件中删除面包屑

linux - bash 脚本中带有空格的基本名称?

linux - 临时 : ssh: connect to host failed: Connection refused

linux - 在网络命名空间上打开 gnome-terminal,然后运行命令

java 文件是从命令行执行的,而不是从浏览器(apache)执行的?

python - python 中的正则表达式跳过行的前 2 个字符

python - 使用正则表达式替换字符串部分之间的数字

python - Pandas 通过正则表达式读取带有字符串定界符的 CSV

linux - 使专有的 ELF 二进制文件在 Linux 上可移植

linux - cURL 文件上传 - "No File Provided"