Find the answer to your Linux question:
Results 1 to 7 of 7
what do you do when sed works, but only half the time? i am writing a script that will replace some text in some config files but i am noticing ...
  1. #1
    Just Joined!
    Join Date
    Sep 2007
    Posts
    15

    Question sed errors, is there another way?

    what do you do when sed works, but only half the time?

    i am writing a script that will replace some text in some config files but i am noticing the sed sucks! it only works about half the time and the other half i get obscure errors. i'm tired of trying to track down the problem with sed.

    is there another way of replacing a specific string in a config file without using the sed command?

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Sure. Learn Perl. It's at least as bug-free as sed is.

    Learn Perl, and you win. Show us with a concrete example how sed is misbehaving, and we'll show you how you're wrong, and everybody wins.

    Your choice.

  3. #3
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by dwygant View Post
    is there another way of replacing a specific string in a config file without using the sed command?
    show your sed code, as well as the input file.
    a variety of tools such as Python, awk, even the bash shell has in built string replacement, lastly but not least, Perl.
    in awk,
    Code:
    awk '{ gsub("old,"new")}{print}' file
    however, it also very depends on how your input file looks like.

  4. #4
    Just Joined!
    Join Date
    Sep 2007
    Posts
    15
    it is easy to get frustrated when a deadline approaches and you are stuck in the mud.

    i may try and use awk, but i really need to learn perl too.

    i am trying to use sed in a script to replace some configuration settings in four different conf files.

    it seems to work, sort of. The four files are /etc/network/interfaces, /etc/postfix/main.cf, /etc/MailScanner/spam.assassin.prefs.conf, and /etc/MailScanner/MailScanner.conf

    it is the MailScanner.conf file that has been giving me trouble.

    to make matters worse, i'm doing this all within a virtual machine. the errors don't appear in the original version but show up after i copy the machine. from the original machine my code seems to do exactly what i want.

    here is the error that I get when i run it in the copied version of the virtual machine.

    sed: -e expression #1, char 24: unterminated `s' command
    sed: -e expression #1, char 28: unterminated `s' command


    of course when i cat the file that was supposed to be changed it is empty.

    my code looks thus...

    cat /etc/MailScanner/MailScanner.conf | sed s/ORGNAME/$OName/ > MailScanner.1.conf.tmp
    cp MailScanner.1.conf.tmp /etc/MailScanner/MailScanner.conf

    rm MailScanner.1.conf.tmp

    cat /etc/MailScanner/MailScanner.conf | sed s/ORGLONGNAME/$LName/ > MailScanner.2.conf.tmp
    cp MailScanner.2.conf.tmp /etc/MailScanner/MailScanner.conf

    rm MailScanner.2.conf.tmp

    cat /etc/MailScanner/MailScanner.conf | sed s/SITENAME/$OSite/ > MailScanner.3.conf.tmp
    cp MailScanner.3.conf.tmp /etc/MailScanner/MailScanner.conf

    rm MailScanner.3.conf.tmp




    i've tried doing this using only one temp file as i did with the other files but that did not seem to work. this code actually does work, sort of...

    i sure do appreciate any help you can give...

  5. #5
    Just Joined!
    Join Date
    Sep 2007
    Posts
    15
    oh you know what... i just noticed something. only in my MailScanner.conf file do i ask for an input that could have a space in it. when the ORGLONGNAME is queried the response may contain spaces, such as foo bar llc. the spaces may be my whole problem.

    when i have tested the script i just entered in a string of text with out spaces in order to save time. but after i copy the machine i have attempted to put in genuine values that contain a space or two.

    do you think i'm on the right track here?

    more importantly, is there a way to deal with this?

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Code:
    sed s/ORGLONGNAME/$LName/      # bad
    sed "s/ORGLONGNAME/$LName/"    # good
    Hope this helps.

  7. #7
    Just Joined!
    Join Date
    Sep 2007
    Posts
    15
    Quote Originally Posted by wje_lf View Post
    Code:
    sed s/ORGLONGNAME/$LName/      # bad
    sed "s/ORGLONGNAME/$LName/"    # good
    Hope this helps.
    Yes!

    me bad, you good!

    i sort of figured quotes would be involved here somehow.

    thank you very much for your time.

Posting Permissions

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