LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Edit a dat file using shell script (http://www.linuxquestions.org/questions/linux-newbie-8/edit-a-dat-file-using-shell-script-944010/)

sharp859 05-09-2012 02:18 AM

Edit a dat file using shell script
 
Hi All,

I have a text file inject.dat, it has 5 entries as below
PKGNAME=Project-debug
OLD_VERSION=1.0.0
NEW_VERSION=1.0.1
PRODUCT NUMBER=1
RELASE DATE=11/11/2011

How we could write shell script so I can read each line replace everything after "="sign, with new value, example run
./inject.sh $PKGNAME $OLD_VERSION $NEW_VERSION _PRODUCT_NUMBER $RELEASE_DATE , so it should update the inject.dat with newer value what ever I give as parameter? Any idea?

bradvan 05-09-2012 05:48 AM

This will do it
 
Code:

#!/bin/bash
if [ ${#} -ne 5 ]; then
  echo "You must supply five parameters"
  exit 1
fi
FIL=inject.dat
/bin/sed -i "s/^\(PKGNAME=\).*/\1${1}/" ${FIL}
/bin/sed -i "s/^\(OLD_VERSION=\).*/\1${2}/" ${FIL}
/bin/sed -i "s/^\(NEW_VERSION=\).*/\1${3}/" ${FIL}
/bin/sed -i "s/^\(PRODUCT_NUMBER=\).*/\1${4}/" ${FIL}
/bin/sed -i "s/^\(RELEASE DATE=\).*/\1${5}/" ${FIL}


David the H. 05-09-2012 11:37 AM

Do you really need to edit the original? If there's nothing in the file that needs to be preserved, why not simply overwrite it?

Code:

#!/bin/bash

{
echo "PKGNAME=$1"
echo "OLD_VERSION=$2"
echo "NEW_VERSION=$3"
echo "PRODUCT NUMBER=$4"
echo "RELASE DATE=$5"
} >inject.dat


sharp859 05-09-2012 09:18 PM

Always there will be original, I may replace, or no, but for sure I will replace couple of them every time I run. If we do not put some of them in script parameter would it run right keeping as it is.? Or is that mandatory to enter value, I will be testing this soon and vote and let people know.!, I

David the H. 05-10-2012 05:42 PM

If the file only contains those five lines, then another option is to read it into your script, alter the contents directly, then re-export it.

One other question I have though is how you wish to decide which script parameters change which variable setting. One very simple option could be to just insert blank entries on the command line for the ones you don't want to change.

Code:

#!/bin/bash

file=inject.dat

# confirm that the file is readable first.

if [[ ! -r $file ]]; then
        echo "$file is not readable. Exiting."
        exit 1
fi

# import each line and split it at the "=" sign into two temporary variables.
# then test the first part and save the second part into the appropriate variable.

while IFS='=' read -r name value ; do

        case $name in
                        PKGNAME)  pkgname=$value  ;;
                    OLD_VERSION)  oldvers=$value  ;;
                    NEW_VERSION)  newvers=$value  ;;
                "PRODUCT NUMBER")  prodnum=$value  ;;
                  "RELEASE DATE")  reldate=$value  ;;
        esac


# export the values back into the file.  If a new entry parameter exists,
# for that value then use it, otherwise default to the original.

{
        echo "PKGNAME=${1:-$pkgname}"
        echo "OLD_VERSION=${2:-$oldvers}"
        echo "NEW_VERSION=${3:-$newvers}"
        echo "PRODUCT NUMBER=${4:-$prodnum}"
        echo "RELEASE DATE=${5:-$reldate}"
} >"$file"

exit 0

Then, to use the script, do something like this:

Code:

./updatescript '' '' 1.0.2 '' 05/10/2012
This will update the new version number and release date, an keep the package name, old version number, and product number the same.

Of course this means you have to remember the exact order to put them in. You may want to create a more complex and robust system. Perhaps something with getopts, or even an interactive version using read.


BTW, if you can ensure that the lines always match proper variable form (var=value, with no spaces), then the whole thing can be made even easier. Just source the file and use the entries themselves as the variables.

Code:


#!/bin/bash

file=inject.dat

# if the file is readable, source it into the script.

if [[ ! -r $file ]]; then
        . "$file
else
        echo "$file is not readable. Exiting."
        exit 1
fi

# export the values back into the file.  If a new entry exists as a parameter,
# use it, otherwise default to the original.

{
        echo "PKGNAME=${1:-$PKGNAME}"
        echo "OLD_VERSION=${2:-$OLD_VERSION}"
        echo "NEW_VERSION=${3:-$NEW_VERSION}"
        echo "PRODUCT_NUMBER=${4:-$PRODUCT_NUMBER}"
        echo "RELEASE_DATE=${5:-$RELEASE_DATE}"
} >"$file"

exit 0



All times are GMT -5. The time now is 05:33 PM.