Multiple varibles in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple varibles in file
# 1  
Old 05-23-2010
Multiple varibles in file

Working on a way to speed up my script output. I have a multiline file that I pull each line as a variable and post it on a webpage. It needs some help as it works but slow. I think I have too many cat, grep and sed going on but don't know what would work better.
Code:
#!/bin/sh
FILE=/export/htdocs/request/$REQUEST
DATE=`cat $FILE | grep "DATE=" | sed -n 's/.*=//p'`
NAME=`cat $FILE | grep "NAME=" | sed -n 's/.*=//p'`
EMAIL=`cat $FILE | grep "EMAIL=" | sed -n 's/.*=//p'`
PHONE=`cat $FILE | grep "PHONE=" | sed -n 's/.*=//p'`
UID=`cat $FILE | grep "UID=" | sed -n 's/.*=//p'`
COMPANY=`cat $FILE | grep "COMPANY=" | sed -n 's/.*=//p'`
OC=`cat $FILE | grep "OC=" | sed -n 's/.*=//p'`
PRIORITY=`cat $FILE | grep "PRIORITY=" | sed -n 's/.*=//p'`
EXPLAIN=`cat $FILE | grep "EXPLAIN=" | sed -n 's/.*=//p'`
JUSTIFICATION=`cat $FILE | grep "JUSTIFICATION=" | sed -n 's/.*=//p'`
COMMENTS=`cat $FILE | grep "COMMENTS=" | sed -n 's/.*=//p'`
REGION=`cat $FILE | grep "REGION=" | sed -n 's/.*=//p'`
ADD_DELETE=`cat $FILE | grep "ADD_DELETE=" | sed -n 's/.*=//p'`
FILTER=`cat $FILE | grep "FILTER=" | sed -n 's/.*=//p'`
TYPE=`cat $FILE | grep "TYPE=" | sed -n 's/.*=//p'`
SOURCE_IP=`cat $FILE | grep "SOURCE_IP=" | sed -n 's/.*=//p'`
PORT=`cat $FILE | grep "PORT=" | sed -n 's/.*=//p'`
STATUS=`cat $FILE | grep "STATUS=" | sed -n 's/.*=//p'`

Here is a sample file it is pulling from
Code:
NAME=John Smith
EMAIL=jsmith@testmail.com
PHONE=212-555-1212
UID=gg1234
COMPANY=OTHER
OC=ABC Company
REGION=All
ADD_DELETE=Add
FILTER=Mail
SERVICE_TYPE=TCP
SOURCE_IP=10.1.1.1
PORT=25
REGION=All
ADD_DELETE=Delete
FILTER=Router Access
SERVICE_TYPE=TCP
SOURCE_IP=192.168.1.0/24
PORT=23
PRIORITY=Normal
EXPLAIN=This is an example explanation
JUSTIFICATION=Business justification description
COMMENTS=No comments
DATE=05/23/2010
STATUS=Pending

# 2  
Old 05-24-2010
Hi
Try this:

Code:
awk -F"=" '/NAME/{print $2}' $FILE

Guru.
# 3  
Old 05-24-2010
Code:
#!/bin/sh

while IFS== read -r name value; do
    case $name in
        FILE|DATE|NAME|EMAIL|PHONE|UID|COMPANY|\
        OC|PRIORITY|EXPLAIN|JUSTIFICATION|\
        COMMENTS|REGION|ADD_DELETE|FILTER|\
        TYPE|SOURCE_IP|PORT|STATUS)
            eval curval=\$$name
            if [ "$curval" ]; then
                value=$(printf '%s\n%s' "$curval" "$value")
            fi
            eval $name=\$value
    esac
done < "$1"

Regards,
Alister
# 4  
Old 05-24-2010
Alister, your solution works perfectly. Thank you very much!
# 5  
Old 05-24-2010
Could make the input file executable, then run it within the current shell.

Code:
. ./filename

# 6  
Old 05-24-2010
Hey, methyl:

Whitespace in the values precludes simple sourcing (and if quotes are allowed to appear in values, properly escaping whitespace would be a pain).

Multiple assignment to the same name also stand in the way of simple sourcing. PORT, SOURCE_IP, and a couple of others occur more than once, and the original code accumulates the values, instead of overwritting.

Cheers,
Alister

Last edited by alister; 05-24-2010 at 04:10 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep from multiple patterns multiple file multiple output

Hi, I want to grep multiple patterns from multiple files and save to multiple outputs. As of now its outputting all to the same file when I use this command. Input : 108 files to check for 390 patterns to check for. output I need to 108 files with the searched patterns. Xargs -I {} grep... (3 Replies)
Discussion started by: Diya123
3 Replies

2. Shell Programming and Scripting

Grep and replace multiple strings in a file with multiple filenames in a file

Hi, I have a file containing list of strings like i: Pink Yellow Green and I have file having list of file names in a directory j : a b c d Where j contains of a ,b,c,d are as follows a: Pink (3 Replies)
Discussion started by: madabhg
3 Replies

3. UNIX for Dummies Questions & Answers

Need Help Setting Path and Environment Varibles

Hello all, I have a Mac OS X (10.7), and I need to set environment variables and paths for some programs I will be running. I have followed instructions and searched the Web for where to do this, but I can't seem to find an answer. I have tried using the VIM editor to write them into my .login,... (2 Replies)
Discussion started by: Tyler_92
2 Replies

4. Shell Programming and Scripting

Passing varibles as a string argument ?

For example test.sh: test="teststring" cmd=$1 $cmd For some reason I'm NOT seeing "teststring" when I type: ./test.sh "echo $test" Any ideas on how to get around this? I've tried commands like: ./test.sh "echo $($test)" ./test.sh "echo '$test'" And many variations to no... (6 Replies)
Discussion started by: secops
6 Replies

5. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

6. Shell Programming and Scripting

split varibles and store fields into shell varible array

I need to split a long varible which is a whole line read from a file into fields and store them in an array, the fields are delimited by pipe and a field may contain white spaces. I tried the following concept test and it has problem with field 5 which contain a space, appearently so because... (3 Replies)
Discussion started by: gratus
3 Replies

7. UNIX for Dummies Questions & Answers

How to create ENV varibles

Hi, I want create env variable for "/ramakrsihna/scripts" help me on this. (1 Reply)
Discussion started by: koti_rama
1 Replies

8. UNIX for Dummies Questions & Answers

different way to echo varibles...help

Given the following loop: foreach id (DB4 GH4 CD4) and the previously defined variables: DB4sf DB4sfk DB4pp GH4sf GH4sfk GH4pp CD4sf CD4sfk CD4pp how do i echo all of these variables using a one line command in the for loop. If it was a script, and assuming the previously mentioned... (8 Replies)
Discussion started by: wxornot
8 Replies

9. Shell Programming and Scripting

Dynamic Varibles problem

All, Any chance someone could help me with this.... The script is reading from a file and for every line of input to the loop I am trying to assign a new varible. When i run the script I get the below errors. I have come to a bit of a dead end so any pointers/help would be very much... (1 Reply)
Discussion started by: oconnon2
1 Replies

10. UNIX for Dummies Questions & Answers

environmental varibles

flavor -- AIX 4.2.1 I am putting together some HTML pages, some of which contain forms. The problem pops up when I attempt to pass variables (from the forms) from one HTML page to a cgi-like page created using ksh. I have used the $1 - $9 vars, but they do not work with the passing. With... (1 Reply)
Discussion started by: Xiix
1 Replies
Login or Register to Ask a Question