简体   繁体   English

Perl正则表达式从bash中查找并替换为变量

[英]perl regex find and replace with variable from bash

I have this regex: 'src=\\d' which match all src attributes who start with a number in a file. 我有这个正则表达式: 'src=\\d' ,它匹配以文件中的数字开头的所有src属性。 I need to store it within a variable, cut src= out and write from there a new $string with \\d concatenated to it: $string . $d 我需要将其存储在变量中,切出src=并从那里写一个新的$string并将其\\d连接到它: $string . $d $string . $d . $string . $d Is it possible to store only \\d in a variable with a single command line? 是否可以通过单个命令行仅将\\d存储在变量中? How to use cut and variable in a command line with perl? 如何在Perl的命令行中使用cut和variable? Is it possible? 可能吗?

perl -pi -w -e 's/src="\d+/src="http:\/\/website.com\/\d+/g’ file.tsv 

I'm not exactly sure what you mean, but I think you want something like this, where the () brackets store the number and the $1 replaces it back. 我不确定您的意思,但我想您需要这样的内容,其中()括号用于存储数字,而$1会将数字替换回去。

perl -pi -w -e 's/src="(\d+)/src="http:\/\/website.com\/$1/g’ file.tsv

And you can avoid the so-called 'leaning toothpick syndrome by selecting a different delimiter for the s/// operation like s{}{} 您可以通过为s///操作选择不同的分隔符(例如s{}{}来避免所谓的“倾斜牙签综合症”

perl -pi -w -e 's{src="(\d+)}{src="http://website.com/$1}g’ file.tsv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM