庆典 - 如何通过一个换行符从bash脚本内perl的? [英] bash - How to pass a line feed to perl from within a bash script?

查看:185
本文介绍了庆典 - 如何通过一个换行符从bash脚本内perl的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图取代所有的数字序列,后跟一个换行符和信 A 。这些数字序列位于一个名为的test.txt 文件。在bash脚本 command.sh 是用来执行的任务(见下文)。

I am trying to replace all digits sequences followed by a line feed and the letter a. Those digits sequences are located in a file called test.txt. The bash script command.sh is used to perform the task (see below).

的test.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 文件,这里是我所看到的:


When I call the command.sh file, here is what I see:

$ ./command
00
a1
b2
a

不过,我期待这样的输出:

However, I'm expecting this output:

$ ./command
D1
bD

我是什么失踪?

推荐答案

您甚至不需要的grep ,因为它仅仅是匹配 + ,只需要使用 perl的 -0777 选项(啜食模式)跨线匹配

You don't even need grep since it is just matching .+, just use perl with -0777 option (slurp mode) to match across the lines:

#!/bin/bash

MY_VAR="\d+
a"

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

输出:

D1
bD

这篇关于庆典 - 如何通过一个换行符从bash脚本内perl的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆