Bourne shell script question

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
lubna132Asked:
Who is Participating?
 
yuzhConnect With a Mentor Commented:
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
 
ozoCommented:
perl -i -pe 's/(val3=)(\d+)/$1.($2+10)/e' file
0
 
yuzhCommented:
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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
yuzhCommented:
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
 
lubna132Author Commented:
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
 
lubna132Author Commented:
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
 
lubna132Author Commented:
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
 
tdaoudCommented:

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
 
tdaoudCommented:

lubna132,

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

Tarik
0
 
lubna132Author Commented:
Thanks,I found using Perl was much easier
0
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.

All Courses

From novice to tech pro — start learning today.