IgnorantGuru's Blog

Linux software, news, and tips

About

This blog is where you can check on the latest updates to my Linux software, news and tips, and leave your comments and questions.
TERMS OF USE

2 Comments

  1. Hello, I am trying to write a program, and I have been studying some of your scripts trying to decern how I can achieve what I want to happen. But its not going too well.

    I am having a problem using $VARIABLES inside the SED -I command. And I have not found one person that has achieved this whithout PIPING variables to the SED command.

    I just wanna do something like…

    sudo sed -i “s/^\${oldn}.*/\#\1/” /etc/fstab

    Like your scripts, I am trying to write ONE script which can do all the necessary changes to Ubuntu after a fresh install.

    Of course I have it asking step by step if the user wants to perform a certain section, and if not go on to the next section. And I’m trying to make it have some intelligence.

    For example I prefer to make a Linux partition but not a swap partition, so that I can have a 2gb swap file instead. But one of my friends might have installed theirs with a swap partition. And if so, I can have it detect it, and then UNREGISTER that swap from the system, so that they can then make a swap file of whatever size they say.

    I would expect with the various methods…as in…

    sudo sed -i ‘s/^\${oldn}.*/\#\1/’ /etc/fstab
    sudo sed -i “s/^\${oldn}.*/\#\1/” /etc/fstab
    sudo sed -i `s/^\${oldn}.*/\#\1/` /etc/fstab
    sudo sed -i ‘s/^\”${oldn}”.*/\#\1/’ /etc/fstab
    sudo sed -i (s/^\${oldn}.*/\#\1/) /etc/fstab
    And so on, and so on…I have tried everything, I have tried using $oldn, \$oldn (for the \ to negate the $) and so on.

    I dont wanna pipe variables through…it is so thick and prehistoric.

    I just want to set variable1 with what to look for and variable 2 with what to replace it with, and even variable3 with the file and path for the query to be acted on…like…
    sudo sed -i ‘s/^$var1.*/$var2/’ $var3

    Could you please point me in the right direction. I have tried reading some of these manuals online, and they dont know what they are doing…

    One was showing ECHO “WHATEVER BLAH BLAH” 2>SOMEFILE, and sometimes the 2 would be a 1 or 3…but this tutorial did not break down the command explaining each component. So I never found out what 2 does. Or whether I need to use it, or when.

    Alot of tutorials are that way…one page covering redirection, but they throw in something INTO that redirection and do not even acknowledge its existence.

    I am also trying to OUTPUT a variable’s contents and appending it to a file like…

    var1=”10″
    echo “vm.swappiness=$var1″ >>/etc/sysctl.conf

    I have GREP’d and found that doesn’t exist in the file, so then I want to add it to the file. And I have found that you can’t even SUDO ECHO output using redirection, as the ROOT user is not passed on in the redirection, so even SUDO ECHO results in PERMISSION DENIED.

    Is there a way to ADD a line using SED?

    I hope you can help, tutorials out there dont even know how to show examples!

    Thanks much!

    Comment by Lee Howard | April 17, 2010

    • > I just wanna do something like…
      > sudo sed -i “s/^\${oldn}.*/\#\1/” /etc/fstab

      Variables will work as long as the sed script is double-quoted, and you DON’T escape the $. For example:

      sudo sed -i "s/^${oldn}.*/\#\1/" /etc/fstab

      Bash expands the variable before passing it to sed (keep this in mind if $oldn contains characters that need to be escaped). Also note that you didn’t include parenthesis in your sed script, unless they’re in $oldn, so \1 is empty. You probably want this:

      sudo sed -i "s/^\(${oldn}.*\)/\#\1/" /etc/fstab

      Note that if you use single quotes (‘) around the sed script, bash will not expand the variables. If you need the single quotes, you can use eval, which expands escaped characters BEFORE passing the line to sed, so you may need to double-escape some:

      eval sudo sed -i \'s/^\\\(${oldn}.*\\\)/\\\#\\1/\' /etc/fstab

      To get an idea of what that looks like to sed, insert an echo:

      oldn="test"
      eval echo sudo sed -i \'s/^\\\(${oldn}.*\\\)/\\\#\\1/\' /etc/fstab

      Note that the echo will swallow the single quotes, but otherwise you should see what sed sees.

      My kscrubber script on this site uses variables in a sed command if you want to see a working example.

      > One was showing ECHO “WHATEVER BLAH BLAH” 2>SOMEFILE, and sometimes the 2 would be a 1 or 3…but this tutorial did not break down the command explaining each component. So I never found out what 2 does.

      “2>” redirects stderr instead of stdout. For example:
      ls dfbfdbfdbdfbhtrbg
      will produce an error message, or you can redirect the error message:

      ls dfbfdbfdbdfbhtrbg 2> /dev/null

      See http://www.tldp.org/LDP/abs/html/io-redirection.html

      > And I have found that you can’t even SUDO ECHO output using redirection, as the ROOT user is not passed on in the redirection, so even SUDO ECHO results in PERMISSION DENIED.

      To redirect as root:

      su -c 'echo "yyy" >> testfile'

      Or with a variable:

      eval su -c \'echo "$oldn" \>\> testfile\'

      Also if you prefer to use sudo instead of su for root redirection, try

      sudo bash -c "echo test >> testfile"

      Or with a variable:

      eval sudo bash -c \'echo \"$oldn\" \>\> testfile\'

      > Is there a way to ADD a line using SED?

      You can include a linefeed in the replacement string to add a line. For example:

      echo "xxx" | sed 's/xxx/xxx\x0Ayyy/'  # add yyy line

      Or you can also use sed’s buffer to do multi-line edits, such as:

      sed '1h;1!H;${;g;s/oldtext\x0Aoldtext/newtext\x0Anewtext/;p;}'

      See http://ilfilosofo.com/blog/2008/04/26/sed-multi-line-search-and-replace/

      Hope that helps.

      Comment by igurublog | April 17, 2010


Follow

Get every new post delivered to your Inbox.

Join 130 other followers