Reading from a File and Using as an Input variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading from a File and Using as an Input variable
# 1  
Old 09-24-2010
Reading from a File and Using as an Input variable

I need to know how the the string constant from Input File should be read and provide as input data for the script .

Code:
INPUT FILE

CONST        VARIABLE
myname=/root/dir/syslog/myname1
myname=/root/dir/syslog/myname2
myname=/root/dir/syslog/myname3 

urname=/root/dir/syslog/urname1
urname=/root/dir/syslog/urname2
urname=/root/dir/syslog/urname3

........
.........
;;;;;;;

nthname =/root/dir/syslog/ntname1

I would like to read some constant values from the file and feed that as an input to the script, something like this.

Code:
./myscript urname

From above syntax, Im just using the Constant String from the input text, but script should perform actions on all the instances of variable string mapped to the constant from input text. Can some one provide me the method how to call this . Do i need to write any functions for this?
# 2  
Old 09-24-2010
something like:

Code:
while IFS="=" read $CONST $VARIABLE
do
 ## do whatever with $CONST and $VARIABLE
done < INPUT_FILE

# 3  
Old 09-24-2010
I would add an if statement to operate only on lines containing given parameter:
bash code:
  1. while IFS="=" read $CONST $VARIABLE
  2. do
  3.    if &#91; "$CONST" = "$1" ]
  4.    then
  5.         ## do whatever with $CONST and $VARIABLE
  6.    fi
  7. done < INPUT_FILE
# 4  
Old 09-24-2010
This may not in work in the instance while if offer script to search in those method.

Assume we are talking in term of pointer.

Code:
urname ---> /root/dir/syslog/urname1
                  |---> /root/dir/syslog/urname2
                  |---> /root/dir/syslog/urname3

Assume if the above Constant is Pattern of a File, & Variable is the instances of Files, where we want our script to search for the message. So how will this work, when.
The above syntax should read only the constant and redirect the script to search message in all the 3 instances of File. Just like give below.

Code:
./myscript message urname

But not like

Code:
./myscript message /root/dir/syslog/urname1 or /root/dir/syslog/urname2 or /root/dir/syslog/urname3

# 5  
Old 09-24-2010
So you are not having fixed format? sorry but I am not clear about the requirement.
# 6  
Old 09-24-2010
something like this,

parse_file.pl
Code:
#!/usr/bin/perl

while (<>) {
chomp;
if ($i==1) { if (/^\s+\|---\>\s+(\/.*)/){ print "Do whatever with variable",$1,"\n";} else {$i=0;}}
if(/(urname)([^\w]+)(\/.*)/) {
$a=$3;
                if ($2 =~ /---/) { $i=1;print "Do whatever with variable $a","\n"} else { print "Do whatever with variable $3\n"; }
}
}

invocation
Code:
perl parese_file.pl inputfile

# 7  
Old 09-24-2010
sorry, perl is not available in my environment to test this. Can you break that code in shell script. For your review, here is an samplescript uploaded, if you can capture the week points and correc the same. I feel there are many number of loop holes , but still unable to sort it out.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

User input while reading from a file

I am not able to capture the user input in this script(bash).There is prompt for user input.Could some one help me capture user input while reading afile? while read line do echo "$i" path1=$line path2=`echo $line|sed s/new_dir/old_dir/` echo "Do you want to replace?";... (4 Replies)
Discussion started by: parijat guh
4 Replies

2. Shell Programming and Scripting

XML variable for input in same input file

Dear All , i stuck in one problem executing xml .. i have input xml as <COMMAND name="ARRANGEMENT.WRITE" timestamp="0" so="initial"> <SVLOBJECT> <LONG name="CSP_PMNT_ID" val="-1"/> <MONEY name="CSP_CEILING" amount="0.0" currency="AUD"/> ... (6 Replies)
Discussion started by: arvindng
6 Replies

3. UNIX for Dummies Questions & Answers

Help in reading the date from the input file name

Hi, I need to read the date from the input file. The format of the input file is as follows: a_b_c_yyyymmdd.txt I need to read the date(yyyymmdd) part from the name of the input file. Would really appreciate if someone can help me in this regard Thanks a lot. (1 Reply)
Discussion started by: Sunny_teotia
1 Replies

4. Shell Programming and Scripting

awk- reading input file twice

Hello, I've been trying to come up with a solution for the following problem; I have an input file with two columns and I want to print as an output the first column without any changes but for the second column, I want to divide it by its last value. Example input: 1 9 2 10 3 11 4 12 5... (14 Replies)
Discussion started by: acsg
14 Replies

5. Shell Programming and Scripting

Reading specific contents from 1 input files and appending it to another input file

Hi guys, I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help. I have 2 input files (example given below) Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it... (5 Replies)
Discussion started by: sksahu
5 Replies

6. Shell Programming and Scripting

reading input from a file

I am trying to read input for a C program (that expects input from the user) from a file using the shell command: progname < filename but it seems that the program considers the char '<' as the first input, hence causing an "error" in my program. I checked it with another program and it... (2 Replies)
Discussion started by: nadbar
2 Replies

7. Shell Programming and Scripting

Help reading an input file in KSH

First I' d like to say you guys are awesome. :) I have a word document that I cut and paste into Textpad and it removed all the fancy formatting which is fine with me. I WinScp'd it to the box and and called it inputfile.txt. Opened it in vi and don't see any special characters or stuff that... (2 Replies)
Discussion started by: zilla30066
2 Replies

8. Shell Programming and Scripting

Script for reading an input file

#!/bin/sh rpt="/export/home/legato/rpt_offsite"/test_eject.tape cat <$rpt while read line do echo $line perform routine done I am trying to read the contents of this file line by line and perform a routine for each line read. The file contents are numbers.. What is wrong with my... (1 Reply)
Discussion started by: gzs553
1 Replies

9. Shell Programming and Scripting

Reading Input from File and Duplicates Output

Greetings to all, I would like to read input from a file and make duplications from it with Linux shell. For e.g. Input file ----------- ABC ABB ABA ------------------------------- Output file ------------ ABC ABC ABC ABB ABB (6 Replies)
Discussion started by: noelcantona
6 Replies

10. UNIX for Dummies Questions & Answers

Reading input to create a variable in a script?

I would like to prompt for input and then use it as a variable in a script. Something like this. #!/bin/ksh echo "What is your name?: \c" read response echo "Your name is $reply" >file.txt done exit 0 What am I missing? Thanks, (7 Replies)
Discussion started by: darthur
7 Replies
Login or Register to Ask a Question