LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Sed - change some thing but not all (https://www.linuxquestions.org/questions/linux-newbie-8/sed-change-some-thing-but-not-all-580962/)

mijohnst 08-30-2007 09:42 AM

Sed - change some thing but not all
 
I love sed...that being said...

I'm trying to figure out a command that will change one line, but not the comment above the line I'm changing... I know it's something simple.

# PASS_MAX_DAYS sets maximum password days

PASS_MAX_DAYS 999999

So I use:

Code:

sed '/#/!s|PASS_MAX_DAYS|PASS_MAX_DAYS    180|g' /etc/login.defs
It's working kind of... but the out put looks like this:

PASS_MAX_DAYS 180 999999

Am I missing a switch somewhere? I guess I want to replace the line and the spaces are throwing me off. Thanks again!

radoulov 08-30-2007 10:32 AM

Code:

sed 's/^PASS_MAX_DAYS *[0-9]*/PASS_MAX_DAYS    180/'

mijohnst 08-30-2007 01:04 PM

Thanks for the reply Radoulov. That command you just sent does the same thing though. It doesn't remove the 99999.

radoulov 08-30-2007 01:15 PM

Quote:

Originally Posted by mijohnst (Post 2875995)
Thanks for the reply Radoulov. That command you just sent does the same thing though. It doesn't remove the 99999.

Could you post the output from the following command?

Code:

echo "# PASS_MAX_DAYS sets maximum password days

PASS_MAX_DAYS 999999"|sed 's/^PASS_MAX_DAYS *[0-9]*/PASS_MAX_DAYS    180/'

This is the output I get:

Code:

zsh 4.3.2% echo "# PASS_MAX_DAYS sets maximum password days

PASS_MAX_DAYS 999999"|sed 's/^PASS_MAX_DAYS *[0-9]*/PASS_MAX_DAYS    180/'
# PASS_MAX_DAYS sets maximum password days

PASS_MAX_DAYS    180
zsh 4.3.2%


mijohnst 09-01-2007 03:36 PM

When I type in your echo command it comes out correct, but when I point what you've done in sed to the file itself it doesn't work and I think it's looking for the 999999 number. I don't think I want to look for the 99999 either because if the system has any other number I still want it replaced. Is there a switch in sed to tell it to overwrite the whole line?

Again thanks for the help!

mijohnst 09-01-2007 04:01 PM

Thanks for the help radoulov.... I figured it out with your help.

Code:

sed '/^PASS_MAX_DAYS/ c\PASS_MAX_DAYS  180' /etc/login.defs
The c\ will allow you to change the line to the end.


All times are GMT -5. The time now is 11:48 PM.