Assign values to variables of a file

Hi,

I have a file like the following...

CUST=
DIR=
NULIST=
name=philps_123

How can i add values to each of these unassigned variables using a shell script?

say for eg: i have values for CUST as onida, dir as /dir/onida, NULIST as /tmp/onida_files. How can i add these values to the corresponding fileds using a shell script? output should look like..

CUST=onida
DIR=/dir/onida
NULIST=/tmp/onida_files
name=philps_123

pass the value as input parameter in shell script.

$ ./a.sh onida /dir/onida /tmp/onida_files

#!/bin/sh

CUST=$1
DIR=$2
NULIST=$3
name=philps_123

echo "$CUST"
echo $DIR
echo $NULIST
echo $name

I dont test it an iam sure that there are better possibilities...but one way to handle it is this way:

sed '/CUST/s/=/=onida/g' filename > filename

It substitude all "=" with "=onida" in all lines contained the word "CUST" in your file with the name "filename"

Thnaks for the quick reply, but for each customer i need to create different files. So this method won't work.

No this works. You need to restate your question. It looks like what you really want is not what you orginally asked for.

ok i will restate the qn. i think i was not clear before.

i have a template file like below..

CUST=
DIR=
NUFILE=
XXXXXXX
XXXXXXx
XXXXXXX

For each user i need to create a file with these entries filled.

so for a user named onida i should have a file onida.txt with these fields added.

CUST=onida
DIR=/dir/onida
NULIST=/tmp/onida_files
name=philps_123
XXXXXXX
XXXXXXx
XXXXXXX

No tell me one thing... from where u will get list of user? Is it in a file or you need to pass dynamically each time...

For each use, do u need to create seperate file like above. If yes, what will be the output file name conventions?

Try awk

# cat name.list
Joe
Max
Alice
# awk '{_=$1;printf "CUST=%s\nDIR=/dir/%s\nNULIST=/tmp/%s_files\nname=phils_123\n",_,_,_>(_".txt")}' name.list
# ls
Alice.txt       Joe.txt         Max.txt         name.list
# cat Alice.txt
CUST=Alice
DIR=/dir/Alice
NULIST=/tmp/Alice_files
name=phils_123

Note: Look like a homework :wink:

I have an interactive script at work I made a couple of years ago which does exactly what you are looking for. Its a bit messy, but does the trick.

It basically asks you a series of questions;
Customer's name?
Directories etc?

and then echo's them into a new file;

echo "#Config file" >> filename
echo "CUST=$cust_name" >> filename
echo "DIR=$dir1" >> filename
echo "NULIST=$nulist" >> filename
echo "name=philps_123" >> filename

etc etc

I'll dig it out when I'm back at work tomorrow and post it if it would be of interest?

pass dynamically each time another script runs. yeah for each use i need to create separate file like above and the output file name will be something like username.txt.

awk 'BEGIN{_=ARGV[1];printf "CUST=%s\nDIR=/dir/%s\nNULIST=/tmp/%s_files\nname=phils_123\n",_,_,_>(_".txt")}' user

So give sample of two users at least, and tell us where you got "name=philps_123"

For above 3 lines, maybe you can use below code:

$ cat template.txt
onida
xnidb
ynidc

$ awk '{print "CUST=" $0 > ( fn= $0 ".txt")
      print "DIR=/dir/" $0 > fn
      print "NULIST=/tmp/" $0 "_files" > fn
     } ' template.txt

$ cat onida.txt
CUST=onida
DIR=/dir/onida
NULIST=/tmp/onida_files