Solved

Bourne shell script question

Posted on 2001-07-18
10
357 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best software for Unix commands in Windows cmd.exe? 18 102
reinstall 1 70
Sed question 2 68
How to clone solaris 10 machine 33 111
In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
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.

914 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

14 Experts available now in Live!

Get 1:1 Help Now