LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-14-2008, 05:51 PM   #1
khill
LQ Newbie
 
Registered: Aug 2008
Posts: 3

Rep: Reputation: 0
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?
 
Old 08-14-2008, 05:56 PM   #2
khill
LQ Newbie
 
Registered: Aug 2008
Posts: 3

Original Poster
Rep: Reputation: 0
well geez, 5 more min of searching and I answered my own question

Code:
grep umask .bashrc | sed 's/{$1}/umask 027/g' .bashrc
 
Old 08-14-2008, 06:02 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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...

Last edited by colucix; 08-14-2008 at 06:06 PM.
 
Old 08-14-2008, 06:13 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
 
Old 08-15-2008, 11:35 AM   #5
khill
LQ Newbie
 
Registered: Aug 2008
Posts: 3

Original Poster
Rep: Reputation: 0
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!
 
  


Reply

Tags
replace, search, sed



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
sed ShaqDiesel Programming 8 07-24-2005 05:57 PM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration