bash - How to pass a line feed to perl from within a bash script? [duplicate]

流过昼夜 提交于 2019-12-11 18:36:22

问题


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/";

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

What am I missing?


回答1:


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

Output:

D1
bD


来源:https://stackoverflow.com/questions/38642827/bash-how-to-pass-a-line-feed-to-perl-from-within-a-bash-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!