0

There are two bash variables in the script which I am building

num1=site_2734
num2=796

It needs to read an svg file containing the string:

http://xxx-xxx.xxx.com/var/site_2734/storage/issues/796/svg/img/page-002-img1.jpg?q=80

I want to replace the first part with 'nothing' and remove the '?q=10' so it becomes

page-002-img1.jpg

So I've tried something like this with sed which isn't working

sed -i -e 's/\(http:\/\/xxx-xxx.xxx.com\/var\/$num1\/storage\/issues\/$num2\/svg\/img/\).*\(\)/\1\2/'

Any help would be much appreciated

  • The ' prevent the shell from expanding $num1/$num2, try " instead. – nohillside May 4 '18 at 21:33
  • It might be helpful to learn how not to build fences when working with sed commands. Which is easier to read: s/\/this\/or\/that/theother/ or s_this/or/that_theother_? (: – DopeGhoti May 4 '18 at 21:42
  • Why don't we use awk instead of sed... – msp9011 May 5 '18 at 7:32
2

Simple enough so long as the filename itself is known to never contain slashes or question marks:

sed --in-place 's_http.*/img/\(.*\)?q=[0-9][0-9]*_\1_' inputfile

If you really do need the site and issue numbers, you can assemble your sed command, and then implement it as a variable:

sedcmd='s_http.*'$num1'.*'$num2'.*/img/\(.*\)?q=[0-9][0-9]*_\1_'
sed --in-place "$sedcmd" inputfile
  • The url in the first comment above points to the whole svg file, I want to replace the string as detailed in the original question, so where it says link I want to change it to "page-002-img1.jpg?q=80" - but two variables need to be passed in to make sure the string matches exactly as above. This is to make the svg render the background image locally and not fetch it from the website above. – jquerynewbie May 4 '18 at 22:07
  • I have modified the expression to not expect the URL to be the entire input line as the question initially implied. – DopeGhoti May 4 '18 at 22:13
  • Thanks, I apologise if it wasn't that clear. – jquerynewbie May 4 '18 at 22:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.