Find the answer to your Linux question:
Results 1 to 9 of 9
I am having a lot of problems trying to change one string by another using sed: the sentence is like this: sed -i 's/KERNEL=="tty[A-Z]*", NAME="%k", GROUP="uucp", MODE="0660"/KERNEL=="tty[A-Z]*", NAME="%k", GROUP="uucp", MODE="0666"/g' ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2010
    Posts
    5

    [SOLVED] Using sed to change strings


    I am having a lot of problems trying to change one string by another using sed:
    the sentence is like this:
    sed -i 's/KERNEL=="tty[A-Z]*", NAME="%k", GROUP="uucp", MODE="0660"/KERNEL=="tty[A-Z]*", NAME="%k", GROUP="uucp", MODE="0666"/g' 50-udev.rules

    it is just to fing the line with:
    KERNEL=="tty[A-Z]*", NAME="%k", GROUP="uucp", MODE="0660"
    and replace it with:
    KERNEL=="tty[A-Z]*", NAME="%k", GROUP="uucp", MODE="0666"
    I would like to identify the whole line because other lines have the same structure.
    I beliebe that between KERNEL=="tty[A-Z]*", and NAME="%k" there are 12 spaces (it is the file 50-udev.rules for red hat)
    I will appreciate your help!

  2. #2
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    Code:
    sed -i 's/KERNEL=="tty\[A-Z\]\*", NAME="%k", GROUP="uucp", MODE="0660"/KERNEL=="tty\[A-Z\]\*", NAME="%k", GROUP="uucp", MODE="0666"/g' 50-udev.rules
    You just have to prevent the character substitution [A-Z] and the * from being interpreted by sed, and impose them as string litterals.
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

  3. #3
    Just Joined!
    Join Date
    Sep 2010
    Posts
    5
    Thank you very much for your asnwer. Unfortunately, I still could not achieve to make it work. Could you help me with a working example, please?
    Thank you very much again!

  4. $spacer_open
    $spacer_close
  5. #4
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    ... It seems to be quite straight forward ! You should perhaps feed in the full path to 50-udev.rules. Are you on Linux ? or some other 'NIX ? Not all commands are portable from one 'NIX to the other.
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

  6. #5
    Just Joined!
    Join Date
    Sep 2010
    Posts
    5
    I am in Linux (Red Hat). I would like to create a script instead of writting it manualy. I do not achieve to impose [A-Z] and * as string literals.

  7. #6
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Try this:

    Code:
    awk -v var='KERNEL=="tty[A-Z]*", NAME="%k", GROUP="uucp", MODE="0660"' '
    $0 == var{sub("MODE=\"0660\"","MODE=\"0666\"")}1' file > newfile
    mv newfile file

  8. #7
    Just Joined!
    Join Date
    Sep 2010
    Posts
    5
    Thank you for yor quick answer.
    with this awk example, it did not work. I am trying with smaller strings to check it but it does not make it neither. Could you help me to find out how to solve the problem?
    Thanks!

  9. #8
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Works fine for me, this is the output I get with this example, note that only the first line matches the pattern:

    Code:
    $ cat file
    KERNEL=="tty[A-Z]*", NAME="%k", GROUP="uucp", MODE="0660"
    KERNEL=="tty[A-Z]*", NAME="%s", GROUP="uucp", MODE="0660"
    KERNEL=="tty[A-Y]*", NAME="%k", GROUP="uucp", MODE="0660"
    KERNEL=="tty[A-Z]*", NAME="%n", GROUP="uucp", MODE="0660"
    $
    $ awk -v var='KERNEL=="tty[A-Z]*", NAME="%k", GROUP="uucp", MODE="0660"' '
    $0 == var{sub("MODE=\"0660\"","MODE=\"0666\"")}1' file
    KERNEL=="tty[A-Z]*", NAME="%k", GROUP="uucp", MODE="0666"
    KERNEL=="tty[A-Z]*", NAME="%s", GROUP="uucp", MODE="0660"
    KERNEL=="tty[A-Y]*", NAME="%k", GROUP="uucp", MODE="0660"
    KERNEL=="tty[A-Z]*", NAME="%n", GROUP="uucp", MODE="0660"
    $

  10. #9
    Just Joined!
    Join Date
    Sep 2010
    Posts
    5
    Ok! I used the two line commands as an onli one line:
    awk -v var='KERNEL=="tty[A-Z]*", NAME="%k", GROUP="uucp", MODE="0660"' '$0 == var{sub("MODE=\"0660\"","MODE=\"0666\"")}1' file

    instead of
    awk -v var='KERNEL=="tty[A-Z]*", NAME="%k", GROUP="uucp", MODE="0660"' '
    $0 == var{sub("MODE=\"0660\"","MODE=\"0666\"")}1' file

    By the way, I just got it as well using:
    sed -i 's/KERNEL=="tty\[A-Z\]\*"\,[ ][ ]*NAME="%k", GROUP="uucp", MODE="0660"/KERNEL=="tty\[A-Z\]\*"\,\t\tNAME="%k", GROUP="uucp", MODE="0666"/g' 50-udev.rules
    Thank you very much for your help!!! It was really useful

Posting Permissions

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