Results 1 to 9 of 9

Thread: sed - find&replace - in multiple files

  1. #1
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    181

    Default sed - find&replace - in multiple files

    Hello
    I have 60+ *.xhtml files

    I have to find particular phrase (e.g. <h3 id="top">) which includes spaces, <, ", etc.
    and replace it with another phrase
    (e.g. <a class"top" href="../file/file.xhtml>), also with spaces and other 'characters'.

    I tried to use sed:
    Code:
    find . -name "*.xhtml" | xargs sed -i 's/<h3 id="top">/<h3 id="top"><a class="tyt" href="http://forums.opensuse.org/english/get-technical-help-here/Text/text.xhtml">/g'
    and have error:
    Code:
    sed: -e expression #1, char 54: unknown option to `s'
    or
    Code:
    find . -name "*.xhtml" | xargs sed -i 's/'<h3 id="top">'/'<h3 id="top"><a class="tyt" href="http://forums.opensuse.org/english/get-technical-help-here/Text/SpisTresci.xhtml">'/g'
    with this error
    Code:
    syntax error near unexpected token `<'
    or
    Code:
    find . -name "*.xhtml" | xargs sed -i 's/"<h3 id="top">"/"<h3 id="top"><a class="tyt" href="http://forums.opensuse.org/english/get-technical-help-here/Text/SpisTresci.xhtml">"/g'
    with error
    Code:
    sed: -e expression #1, char 57: unknown option to `s'
    What shall I do?
    Any proper way of entering this command?
    Any help?
    Or how to do that?

    greetings
    Linux 4.12.14
    openSUSE Leap 15.0 - 64 bit
    KDE Plasma 5.12.15
    KDE Framework 5.44.0
    Qt - 5.9.4

  2. #2
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    181

    Default Re: sed - find&replace - in multiple files

    I discovered that problem is "backslash /" in the expression.
    Linux 4.12.14
    openSUSE Leap 15.0 - 64 bit
    KDE Plasma 5.12.15
    KDE Framework 5.44.0
    Qt - 5.9.4

  3. #3
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    24,864

    Default Re: sed - find&replace - in multiple files

    Quote Originally Posted by GazetaCypr View Post
    I discovered that problem is "backslash /" in the expression.
    Yes and no.

    It certainly is the / (it is a "slash" not a "backslash"). And you can avoid that by using another character instead of all three / that are relevant to the s command. One that does not appear anywhere in both strings. The % for example. Remember that the fisrt character after the s defines what you should use to delimiter both strings.
    Henk van Velden

  4. #4
    Join Date
    Jun 2008
    Location
    Groningen, Netherlands
    Posts
    19,651
    Blog Entries
    14

    Default Re: sed - find&replace - in multiple files

    Quote Originally Posted by hcvv View Post
    Yes and no.

    It certainly is the / (it is a "slash" not a "backslash"). And you can avoid that by using another character instead of all three / that are relevant to the s command. One that does not appear anywhere in both strings. The % for example. Remember that the fisrt character after the s defines what you should use to delimiter both strings.
    I think he meant
    Code:
    \/
    but, like me, saw that that looks like a capital v.
    ° Appreciate my reply? Click the star and let me know why.

    ° Perfection is not gonna happen. No way.

    https://en.opensuse.org/openSUSE:Board#Members
    http://en.opensuse.org/User:Knurpht
    http://nl.opensuse.org/Gebruiker:Knurpht

  5. #5
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    24,864

    Default Re: sed - find&replace - in multiple files

    Quote Originally Posted by Knurpht View Post
    I think he meant
    Code:
    \/
    but, like me, saw that that looks like a capital v.
    I can not find the sequence \/ anywhere.

    His first example is wrong because / is used as the string delimiter for the sed s command. and there are /s all over the place.

    I did not try to evaluate the second example because it is even weirder
    Henk van Velden

  6. #6
    Join Date
    Jun 2008
    Location
    Groningen, Netherlands
    Posts
    19,651
    Blog Entries
    14

    Default Re: sed - find&replace - in multiple files

    Quote Originally Posted by hcvv View Post
    I can not find the sequence \/ anywhere.

    His first example is wrong because / is used as the string delimiter for the sed s command. and there are /s all over the place.
    Like in escaping a space char in a file name on a bash shell: file\ name.
    ° Appreciate my reply? Click the star and let me know why.

    ° Perfection is not gonna happen. No way.

    https://en.opensuse.org/openSUSE:Board#Members
    http://en.opensuse.org/User:Knurpht
    http://nl.opensuse.org/Gebruiker:Knurpht

  7. #7
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    24,864

    Default Re: sed - find&replace - in multiple files

    Quote Originally Posted by Knurpht View Post
    Like in escaping a space char in a file name on a bash shell: file\ name.
    I agree that using a \ before characters in the REGEXP and/or the REPLACEMENT that are the same as the delimiter character, should help also (but I still think using a different delimiter is easier to type and read), but why does he then say:
    I discovered that problem is "backslash /" in the expression.
    without btw showing he problem.
    Henk van Velden

  8. #8
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    181

    Default Re: sed - find&replace - in multiple files

    I solved the problem using , (comma) as a delimiter. Then I could f&r whatever I wanted (fortunately there were no commas in my expression )

    Some explanations:
    I had problem with / (slash) because it was used as a delimiter and character in my expression - that's why it caused problems.
    I didn't know (prior) that first letter after 's is delimiter. Most examples are with / as a delimiter. Thanks to hcvv and his explanation on delimiter, plus few examples on the net, I tried to use comma and it worked!

    So for me for now the problem is solved.
    Thanks everyone.
    Linux 4.12.14
    openSUSE Leap 15.0 - 64 bit
    KDE Plasma 5.12.15
    KDE Framework 5.44.0
    Qt - 5.9.4

  9. #9
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    24,864

    Default Re: sed - find&replace - in multiple files

    That is fine. I often use % because that is even less used in many cases then , .

    And yes, the / is allmost allways ussed (and is more or less automaticly used by my fingers when I am using vi), but one should llways use the documentation (hehe).
    Henk van Velden

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •