Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with passing data from a file to a variable 2

Status
Not open for further replies.
Aug 15, 2002
422
US
Hi Folks,

Given a file myfile.txt that contains two lines:

BOM BOM_SRS_DATE BOM_SRS_DATE_STANDARD
CE FND_DATE4 FND_STANDARD_DATE


How would I extract each line as a variable into my shell script?
Example:

VAR1 = BOM BOM_SRS_DATE BOM_SRS_DATE_STANDARD
VAR2 = CE FND_DATE4 FND_STANDARD_DATE


Any help is much appreciated!!

-pd
 
Cross-posting thread271-670385
Try something like this:
Code:
eval `awk '{printf "var%d=%s\n",NR,$0}' myfile.txt`
echo "var1='$var1'\nvar2='$var2'"
You can also read directly from the shell: man read

Hope This Help
PH.
 
Try this:

VAR1=$(head -1 myfile.txt)
VAR2=$(tail -1 myfile.txt)



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
I appreciate the responses (in both forums PHV)!

Unfortunately PHV, I get an error:

orausa:/u01/scripts/fixes> myscript.sh
myscript.sh[2]: BOM_SRS_DATE: not found.
var1=''
var2=''


LK, That would definately work. But I'll have more lines in the next file that requires parsing.

The sad part is that I was able to do this easily in Perl, but the DBA does not want to incorporate Perl into his work (at this point anyways)...

-pd
 
Sorry for the typo:
Code:
eval `awk '{printf "var%d=\"%s\"\n",NR,$0}' myfile.txt`
echo "var1='$var1'\nvar2='$var2'"

Hope This Help
PH.
 
Thank you very much. My awk skills are pretty weak!

So if I had 100 lines, what would I have to change?

-pd
 
You can also use arrays:

#!/bin/ksh
i=0
while read ln
do
var[$i]="$ln"
(( i += 1 ))
done <myfile.txt


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Nothing in the awk part. You will have to manage 100 shell variables named var1,var2,...,var100.
If you don't know the exact number of lines in your file you can try something like this:
Code:
eval `awk '{printf &quot;var%d=\&quot;%s\&quot;\n&quot;,NR,$0}
END{printf &quot;nbvar=%d\n&quot;,NR}' myfile.txt`
i=1
while [ $i -le $nbvar ]; do
  eval tmp=&quot;\$var$i&quot;
  echo &quot;var$i='$tmp'&quot; 
  i=`expr 1 + $i` # i=$((1+i)) if ksh used
done


Hope This Help
PH.
 
Great PHV!!

Thank you! That did the trick. That is fairly complicated! What roles do the eval statements play in that excerpt? I'm trying to get a handle on what eval is doing.

-pd
 
Code:
eval
reads his arguments as input to the shell and execute the resulting command(s).
In other words, the command is evaluated twice by the shell before execution.

Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top