?
Solved

Writing a script to modify text in a file

Posted on 2003-03-18
11
Medium Priority
?
416 Views
Last Modified: 2013-12-27
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.
0
Comment
Question by:bpb25
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 2

Accepted Solution

by:
soupdragon earned 120 total points
ID: 8158841
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
 
LVL 18

Expert Comment

by:liddler
ID: 8159132
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
 
LVL 2

Expert Comment

by:soupdragon
ID: 8159168
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bpb25
ID: 8160214
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
 

Author Comment

by:bpb25
ID: 8160362
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
 

Expert Comment

by:darren1031
ID: 8161490
You could also do it this way:

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

Darren
0
 
LVL 3

Expert Comment

by:interiot
ID: 8161886
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
 
LVL 3

Expert Comment

by:jwelter
ID: 8162270
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
 
LVL 3

Expert Comment

by:jwelter
ID: 8162321
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
 
LVL 2

Expert Comment

by:soupdragon
ID: 8162371
jwelter is right - I forgot my then!
should be

if [[ -f .cshrc ]]; then
0
 

Author Comment

by:bpb25
ID: 8167655
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

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses
Course of the Month11 days, 13 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question