-   Linux - Newbie (
-   -   Edit a dat file using shell script (

sharp859 05-09-2012 03:18 AM

Edit a dat file using shell script
Hi All,

I have a text file inject.dat, it has 5 entries as below
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
./ $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 06:48 AM

This will do it

if [ ${#} -ne 5 ]; then
  echo "You must supply five parameters"
  exit 1
/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 12:37 PM

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?



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

sharp859 05-09-2012 10: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 06: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.




# confirm that the file is readable first.

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

# 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  ;;

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


./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.




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

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

# 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 "RELEASE_DATE=${5:-$RELEASE_DATE}"
} >"$file"

exit 0

All times are GMT -5. The time now is 11:02 PM.