Solved

Bourne shell script question

Posted on 2001-07-18
10
344 Views
Last Modified: 2010-04-21
I would like to write a script which would update one value in a file.
  The file is like this:
   ValueA={
    val1=10
    val2=10
    val3=10}
    ValueB={
    val1=10
    val2=10
    val3=10}
    ValueC={
    val1=10
    val2=10
    val3=10} etc...
 Now i when i run the script only val3 should be incremented by 10
 I think i should grep for val3 and then add 10 to the value, however i am not able to do it .
Thanks in advance
0
Comment
Question by:lubna132
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 6296622
perl -i -pe 's/(val3=)(\d+)/$1.($2+10)/e' file
0
 
LVL 38

Expert Comment

by:yuzh
ID: 6296647
Hi  lubna132,

     Here's a sh script for your question, you can modify it to use a
filename as command line argument:

     
#!/bin/sh
cat /dev/null > /tmp/mytmp.$USER
cat MYINPUTFILE | while read LINE

    do
    KEY=`echo $LINE | cut -f1 -d=`
    echo "KEY = $KEY \n"
    VAL=`echo $LINE | cut -f2 -d= | tr -d "}"`
    echo "VAL = $VAL\n"

    if [ "${KEY}" = "val3" ] ; then
        VAL=`expr 3 + ${VAL} `
        LINE=`echo "${KEY}=${VAL}}"`
    fi
    echo "$LINE"

    echo "$LINE" >> /tmp/mytmp.$USER
done
    mv /tmp/mytmp.$USER MYINPUTFILE

exit

0
 
LVL 38

Expert Comment

by:yuzh
ID: 6296889
Hi lubna132,,

    Sorry, I type it wrong in the above script, you want to increase
val3 by 10 NOT 3, just change the script to:

#!/bin/sh
cat /dev/null > /tmp/mytmp.$USER

cat MYINPUTFILE | while read LINE
      do
          KEY=`echo $LINE | cut -f1 -d=`
          VAL=`echo $LINE | cut -f2 -d= | tr -d "}"`
          if [ "${KEY}" = "val3" ] ; then
                  VAL=`expr 10 + ${VAL} `
                  LINE=`echo "${KEY}=${VAL}}"`
          fi
                       
          echo "$LINE" >> /tmp/mytmp.$USER
      done
           mv /tmp/mytmp.$USER MYINPUTFILE

      exit

   
Regards
      yuzh
     
                             
             
0
 

Author Comment

by:lubna132
ID: 6298238
Thank you all for your comments.
 I wnat to know how to increment val3 by 10 even if it is not the last element, ie
  ValA={
    val1=10
    val2=10
    val3=10
    val4=10
   }
  etc..ie vla3 may not always be last, it could be followed by other values
Thank you
0
 

Author Comment

by:lubna132
ID: 6298389
Actually the file is like this:
  VAL A= { val1=10 \
           val2=10 \
           val3=10 \
           val4=10 \
         }
when i used
  cat $1 |
  while read line
  echo $line
 I expected each line to be like:
  VAL A= {val1=10 \ -->1
  val2=10 \   line 2
 etc..
howerver line1 is:
        VAL A= { val1=10 \
           val2=10 \
           val3=10 \
           val4=10 \
         }
line 2 is:
VAL B= { val1=10 \
           val2=10 \
           val3=10 \
           val4=10 \
         }
Should i be doing something differently?
Thank you
 
 
   
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:lubna132
ID: 6298516
Is there anyway by which i can figure out the lines at which "val3" occurs and just update the right hand value by 10?
0
 
LVL 38

Accepted Solution

by:
yuzh earned 50 total points
ID: 6300762
Hi  lubna132 ,

     Here's a  newer version of the script for your question. we read the
file line by line, check if it is val3, if not just  save it to a tmp file, otherwise, check if the record is at the end of a record, if it is then remember to put a } back to the line, if not put a \ back.

#!/bin/sh
cat /dev/null > /tmp/mytmp.$USER
cat MYINPUTFILE | while read LINE

    do
    KEY=`echo $LINE | cut -f1 -d=`

    if [ "${KEY}" = "val3" ] ; then
        TMPVAL=`echo $LINE | cut -f2 -d=`
        LineCnt=`echo ${TMPVAL} | grep } | grep -v grep | wc -l`
        if [ ${LineCnt} -eq 1 ] ; then
           # this one at the end of record, put } back to the file
           VAL=`echo ${TMPVAL} | tr -d "{"`
           VAL=`expr 10 + ${VAL} `
           LINE=`echo "${KEY}=${VAL}}"`
        else
           VAL=`echo ${TMPVAL} | tr -d "\"`
           VAL=`expr 10 + ${VAL} `
           LINE=`echo "${KEY}=${VAL}\"`
        fi

    fi
    echo "$LINE"

    echo "$LINE" >> /tmp/mytmp.$USER
done

    mv /tmp/mytmp.$USER MYINPUTFILE

exit 0

#=========End of the Script ==========================

Cheers!

yuzh
0
 
LVL 3

Expert Comment

by:tdaoud
ID: 6300793

You can create a script file and write in it the following script

#
# Begins here
#
while read line
do
echo $line
newline=""
x=0
for i in $line
do
        x=`expr $x + 1`
        f=$i
        f1=`echo $i | cut -d'=' -f1`
        f2=`echo $i | cut -d'=' -f2`
        if [ $f1 = "val3" ]
        then
                f2=`expr $f2 + 10`
                f="$f1=$f2"
        fi
        if [ $x -eq 1 ]
        then
                newline=$f
        else
                newline="$newline $f"
        fi
done
echo $newline
done < $1
#
# Ends here
#

and run it by giving it the name of the input file, it will print out each line with the value of val3 increased by 10.

Good Luck,

Tarik
0
 
LVL 3

Expert Comment

by:tdaoud
ID: 6320635

lubna132,

Any luck with your script....did you find any of our answers helpful?  Do you need any further help?

Tarik
0
 

Author Comment

by:lubna132
ID: 6458501
Thanks,I found using Perl was much easier
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now