LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   help with sed (https://www.linuxquestions.org/questions/linux-general-1/help-with-sed-662864/)

khill 08-14-2008 05:51 PM

help with sed
 
hey, I need a bit of help using the command sed

what I want to do is include in a script a line which changes the umask inside of a user's .bashrc

from some searching, I think i'm 90% of the way there with:

Code:

grep umask .bashrc | sed 's/$1/umask 027/g' .bashrc
obviously the above doesn't work. So, how do I get sed to recognize the piped output of grep and include it in a replace?

khill 08-14-2008 05:56 PM

well geez, 5 more min of searching and I answered my own question

Code:

grep umask .bashrc | sed 's/{$1}/umask 027/g' .bashrc

colucix 08-14-2008 06:02 PM

Another way is
Code:

sed -i '/umask/s%.*%umask 027%' .bahsrc
or
Code:

sed -i 's%umask ...%umask 027%' .bashrc
these work only if the line containing the umask command is unique in .bashrc, otherwise you can get unwanted results. Also note that the -i option actually changes the content of the file. You may want to do a backup copy of your .bashrc before testing.

Edit: I tested your command and does not work for me...

colucix 08-14-2008 06:13 PM

Just a little add-on: if you want to be sure that user's .bashrc files contain a unique line with the umask command, you can consider something like this:
Code:

if [ $(grep -c umask .bashrc) -eq 1 ]
then
  sed -i '/umask/s%.*%umask 027%' .bahsrc
else
  echo ".bashrc contains more than one line with umask"
  echo "check it manually"
fi


khill 08-15-2008 11:35 AM

Well, my command doesn't work within a script, but it does work directly from the command line for me... odd that it wouldn't for you colucix. I'm using centOS if you can think of any relevant differences. Anyways, because I DO want to use this line in a script I was about to post and ask about another way to do it, but you've answered my question before I asked it colucix, thanks!


All times are GMT -5. The time now is 12:21 AM.