Writing a script to modify text in a file

I need help in writing a script that will modify one word of text in my users .cshrc file.  Here's what the line looks like now:  setenv NNTPSERVER oldserver, and I need to find a way to make it look like setenv NNTPSERVER newserver.  I know I can vi every file to make the necessary changes, but I have over 200 accounts, and that would take a while.  Thanks for any help you can provide.
bpb25Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

soupdragonCommented:
Something like

#!/usr/bin/ksh
for USER in `cat /etc/passwd | awk -F: '{ print $1 }'`
do
# cd to each home directory in turn
cd ~$USER
# Make a backup of the cshrc if it exists - just in case
if [ -f .cshrc ]
  cp -p .cshrc .cshrc.save
  # edit it using stream editor
  sed -e 's/setenv NNTPSERVER oldserver/setenv NNTPSERVER newserver/' .cshrc >.cshrc
fi
done

- Try it out with a couple first before running the script i.e for USER in 'adam joe'

SD
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
liddlerCommented:
For best practise, the sed should write to another file, rather than overwriting the original, as sometimes this can 0 the file. so
sed -e 's/setenv NNTPSERVER oldserver/setenv NNTPSERVER newserver/' .cshrc >.cshrc
should be
sed -e 's/setenv NNTPSERVER oldserver/setenv NNTPSERVER newserver/' .cshrc >.cshrc.tmp
mv .cshrc.tmp .cshrc
0
soupdragonCommented:
True, or even better since you already have a copy of the file
sed -e 's/setenv NNTPSERVER oldserver/setenv NNTPSERVER newserver/' .cshrc.save >.cshrc
fi

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

bpb25Author Commented:
Soupdragon,

When I try running the script, I get "syntax error at line 13 : 'fi' unexpected.  Any idea's on how to fix this?

Thanks,

Brian
0
bpb25Author Commented:
Soupdragon,

When I try running the script, I get "syntax error at line 13 : 'fi' unexpected.  Any idea's on how to fix this?

Thanks,

Brian
0
darren1031Commented:
You could also do it this way:

(echo ":%s/setenv NNTPSERVER oldserver/setenv NNTPSERVER newserver/"; echo ":wq") | vi -s filename

Darren
0
interiotCommented:
find /home/ -name .cshrc -print | xargs -n1 perl -i.bak -pe 's/setenv NNTPSERVER oldserver/setenv NNTPSERVER newserver/'

That will make a .cshrc.bak of the original file in each user's directory.  (which is what the -i option does)
0
jwelterCommented:
Without looking myself, and just remembering some of my own scripting, I'm thinking you need a "; then" after the if test condition, for example:

if [ condition test ]; then
command
command
fi

Otherwise, I think if just executes the next command and expects an fi???

JW
0
jwelterCommented:
hmmm -I read that too fast.  After looking at the fi problem again, actually I would suggest that the "; then" would still work, but it's not a clear description of that problem.  I think because there is no "; then", it executes the copy, then exits the if automatically, so when it hits the fi after the sed, it's like "hey I don't need you, I'm done with my if."  -Also, if it exits the if after the copy, no matter what, it's going to try and sed/edit the file & save it, so if the file didn't exist, the script would still bomb?

Food for thought...

JW
0
soupdragonCommented:
jwelter is right - I forgot my then!
should be

if [[ -f .cshrc ]]; then
0
bpb25Author Commented:
This ended up working great, thanks for all who responded.

Something like

#!/usr/bin/ksh
for USER in `cat /etc/passwd | awk -F: '{ print $1 }'`
do
# cd to each home directory in turn
cd ~$USER
# Make a backup of the cshrc if it exists - just in case
if [[ -f .cshrc ]]; then
 cp -p .cshrc .cshrc.save
 # edit it using stream editor
 sed -e 's/setenv NNTPSERVER oldserver/setenv NNTPSERVER newserver/' .cshrc >.cshrc
fi
done

- Try it out with a couple first before running the script i.e for USER in 'adam joe'

SD
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.