1837193 Members
2366 Online
110114 Solutions
New Discussion

Scrpit help

 
SOLVED
Go to solution

Scrpit help

Hello Admins

I am writing a script that does a replace within a series of text files. The problem that i am having is with the sed portion of it.

The line that I am replacing will always be line 4. It starts with "startprogram_gui: " followed by other text. I need to replace the other text with "otczsvmsg1"

The problem is that I could use a series of sed commands to do it, but I was hoping that there is a more elegant method.

Any quick ideas?

Good points for all
When one door closes, God opens anather one. But we stare at the closed one so long that we miss the open door
7 REPLIES 7
Dave La Mar
Honored Contributor

Re: Scrpit help

George -
sed 's/gui.*$/guisomething/' dummy >dummy2

Regards,

dl
"I'm not dumb. I just have a command of thoroughly useless information."
john korterman
Honored Contributor

Re: Scrpit help

Hi,
try this simple script, using your input file as $1:
#!/usr/bin/sh
while read word rest
do
if [ "$word" = "startprogram_gui:" ]
then
rest="otczsvmsg1"
fi
echo "$word $rest"
done <$1

regards,
John K.
it would be nice if you always got a second chance
curt larson_1
Honored Contributor
Solution

Re: Scrpit help

how about just

ex -s -c "4 s/:.*/: otczsvmsg1/|wq" $yourfile
H.Merijn Brand (procura
Honored Contributor

Re: Scrpit help

# perl -pi -e 's/gui.*$/guisomething/' file1 file2 file3

will IN PLACE make the substitutions in all files passed

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Elmar P. Kolkman
Honored Contributor

Re: Scrpit help

You can do it quite simple, just like your description of the problem:
sed '4s/^startprogram_gui:.*$/startprogram_gui: otczsvmsg1/'

(On line 4, replace the line if it starts with .... to ...)

Every problem has at least one solution. Only some solutions are harder to find.
rmueller58
Valued Contributor

Re: Scrpit help

I created a process to insert a piece of text into a the .profile.. to lock people off our system based on subdirectories in the /tmp fs..



# ========================================================================
# This script is used for global modifications of all profiles..
# This is was written to disseminate a test condition to lock accounts
# prior to backup/export
# Written by Rex Mueller - Unix Systems Engineer 6/2/2003
# FILE NAME /pei/peitools/bin/profchange
# ========================================================================
#
# ========================================================================
# DEFINE GLOBAL VARIABLES
# ========================================================================
export SDE=`date +%m%d%Y`
#echo $SDE
export STI=`date +%T`
#echo $STI

A=`cat /etc/passwd|wc -l`
export A
B=15
export B
C=`expr $A - $B`
export C
# ECHO VARIABLES FOR TESTING ONLY!!!

#echo $A
#echo $B
#echo $C

tail -n +15 /etc/passwd > /tmp/passwd.tmp
#tail -n +$B /etc/passwd |more

export prof1=/.profile

# =========================================================================
# PROGRAM EXECUTION
# =========================================================================
#find / -name .profile -print > /tmp/proflist
for prof in `awk -F":" '{print $6}' /tmp/passwd.tmp`
do
# =========================================================================
# DEFINE LOCAL VARIABLES
# =========================================================================
export PROF2=$prof
echo $PROF2 > /tmp/dirtemp
#awk -F":" '{print substr( $6, 7, 3)}' /etc/passwd
awk -F":" '{print substr($1, 7, 3)}' /tmp/dirtemp > /tmp/disttemp
export dist=`cat /tmp/disttemp`
#echo $PROF2
#echo $PROF2$prof1
#echo $dist
# =========================================================================
# PROFILE MODIFICATION PROCESS USING LOCAL VARIABLES
# =========================================================================
cp profchange.template prochange.tmp
perl -p -i -e s/LOCKDIR/$dist"LOCK"/g prochange.tmp

cat $PROF2$prof1 > $PROF2/.profile.$SDE
cat $PROF2$prof1 >> prochange.tmp
cat prochange.tmp >> $PROF2$prof1
done

# =========================================================================
# ESU#3 DATA AND NETWORK PASSWORD EXCEPTION LIST
# Reverses Profile changes for Administrative and Helpdesk users
# dsrath, slforslu, dwmilan, glfergus, phillips, rmueller
# =========================================================================
cp -p /home/esu/dsrath/.profile.$SDE /home/esu/dsrath/.profile
cp -p /home/esu/slforslu/.profile.$SDE /home/esu/slforslu/.profile
cp -p /home/esu/dwmilan/.profile.$SDE /home/esu/dwmilan/.profile
cp -p /home/esu/glfergus/.profile.$SDE /home/esu/glfergus/.profile
cp -p /home/esu/phillips/.profile.$SDE /home/esu/phillips/.profile
cp -p /home/esu/rmueller/.profile.$SDE /home/esu/rmueller/.profile

# =========================================================================
# PROGRAM DONE!!!
# =========================================================================

# profile.template

if [ -d "/tmp/LOCKDIR" ]
then
cat /etc/motd
echo "press any key to continue\n"
read
exit
fi

rmueller58
Valued Contributor

Re: Scrpit help

Elmer's sed script is so much cleaner!!!