Solved

Bourne shell script question

Posted on 2001-07-18
10
391 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
[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
  • 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 

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
 

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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 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…

691 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