-1

are there any usefull command to replace all text that contain number and remove those number .
Ex:

myimages-202x250.jpg
width="202" heignt="250" 


replace to

myimages.jpg
width="217" height="317" 

i've tried with sed but its not working

sed -i 's/*-*x*.jpg/.jpg/g' myfile.sql
  • 1
    sed is used regex (BRE or ERE) not globbing sed 's/-.*x.*\././' – Costas Feb 18 '15 at 13:06
  • i've set a small txt file and use sed 's/-.*x.*\././' a.txt , but it only show echo and the text in the file doesn't change – juicebyah Feb 18 '15 at 13:14
  • You forgot the -i in your last comment. But why talk about sed if you want to do it with MySQL? – wurtel Feb 18 '15 at 13:42
  • while I havenot your test seen I can do any comment. You are free to use pastebin.com to show your example file. – Costas Feb 18 '15 at 13:44
  • 1
    how 202 turns to 217? – Avinash Raj Feb 18 '15 at 15:49
1

As @Costas pointed out, you are using globbing patterns whereas sed uses BRE regular expressions (or ERE if using sed's -r option).

Your command should resemble something like this :

sed -i -r 's/-[0-9]+x[0-9]+\.jpg/.jpg/g' myfile.sql

Explanation :

  • [0-9] means "any number" (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
  • + means "one or more of the previous atom" (The -r switch is needed because + only has a special meaning in EREs)

The pattern -[0-9]+x[0-9]+\.jpg therefore means : "A literal - followed by one or more numbers, followed by a literal x, followed by one or more numbers, followed by a literal . (the backslash is important because . is a special character in REs), followed by a literal jpg.

EDIT:

Upon clarification, this should do what you want :

sed -i -r -e 's/-[0-9]+x[0-9]+\.jpg/.jpg/g' -e 's/width="[0-9]+" *height="[0-9]+"/width="217" height="317"/' myfile.sql

The added pattern width="[0-9]+" *height="[0-9]+" builds on the same principles as said above and replaces it with the hard-coded string width="217" height="317".

  • THANKS!! this should work , i've tested and it worked. any idea how to change width="202" heignt="250" where the numbers is random this with width="217" height="317" – juicebyah Feb 19 '15 at 1:58
  • @juicebyah You want to correct the word heignt by the word height or you want to replace the width and height by the numbers 217 and 317? – user43791 Feb 19 '15 at 15:53
  • i want to replace the size of width and height into 217 and 317 – juicebyah Feb 19 '15 at 16:59
  • @juicebyah Ok, see the edit section of my answer for a complete answer. Please accept the answer if this suits your need. – user43791 Feb 19 '15 at 19:08
  • that code works with a small line of txt files , but when i tried to use in mysql files it didnt work. in mysql its shows like this : width=\"233\" height=\"350\" – juicebyah Feb 20 '15 at 2:26

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.