LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-09-2012, 02:18 AM   #1
sharp859
LQ Newbie
 
Registered: May 2008
Posts: 28

Rep: Reputation: 0
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?

Last edited by sharp859; 05-09-2012 at 02:33 AM.
 
Old 05-09-2012, 05:48 AM   #2
bradvan
Member
 
Registered: Mar 2009
Posts: 367

Rep: Reputation: 61
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}
 
Old 05-09-2012, 11:37 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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

Last edited by David the H.; 05-09-2012 at 11:39 AM. Reason: updated code
 
Old 05-09-2012, 09:18 PM   #4
sharp859
LQ Newbie
 
Registered: May 2008
Posts: 28

Original Poster
Rep: Reputation: 0
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
 
Old 05-10-2012, 05:42 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
 
  


Reply

Tags
grep, perl, sed bash, shell, unix


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script to edit a file jacky29 Programming 4 04-01-2011 08:44 AM
How can I use Shell script to edit a data at a particular location in a txt file? leena_d Programming 30 02-08-2010 12:43 AM
How can I use Shell script to edit row 23 column 5-8 in a txt file? leena_d Linux - Newbie 4 12-14-2009 03:43 AM
bash shell script find and edit fields in a file hchoonbeng Programming 9 10-29-2008 02:13 AM
Shell script to edit a file kaash_m31 Linux - Newbie 2 06-02-2008 05:43 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration