2

I have this regex: 'src=\d' which match all src attributes who start with a number in a file. 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. Is it possible to store only \d in a variable with a single command line? How to use cut and variable in a command line with perl? Is it possible?

perl -pi -w -e 's/src="\d+/src="http:\/\/website.com\/\d+/g’ file.tsv 
1
  • 2
    Am I correct to assume that src=d is a typo src=\d? Commented May 31, 2015 at 0:03

1 Answer 1

4

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.

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{}{}

perl -pi -w -e 's{src="(\d+)}{src="http://website.com/$1}g’ file.tsv
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, it is! I had tried this but without () brackets.. It only needs to be correct in the regex that is \d or \d+
@lizardhr - just change the d+ to \d+

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.