WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: need help on reading each line from a file into a separate variable  (Read 3292 times)

Offline sbp

  • Sr. Member
  • ****
  • Posts: 429
    • piCorePlayer homepage
Hi I need help in making a script where I will read from a txt file.
In this file each line should be read and saved as a new variable like var1, var2 etc.


I have tried this code:
Code: [Select]
#!/bin/sh
while read LINE
do
var1=$(echo $LINE)
echo $var1
var
done < FILE.txt


This will read the file (FILE.txt) and list the content of each line on the screen, but I need to assign each line to a new variable.

The content of FILE.txt
Code: [Select]
CONTENT_A
CONTENT_B
CONTENT_C
etc

And the output I would like would be:
var1=CONTENT_A
var2=CONTENT_B
var3=CONTENT_C
etc..


Thanks
Steen

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Try a bash forum.
What you want to do is extremely difficult.

Offline Lee

  • Hero Member
  • *****
  • Posts: 645
    • My Core wiki user page
The following seems clumsy, but it works.  I hate having to use temp files, but I suspect there's a more elegant way.
Code: [Select]
# create a data file for testing
echo -e "Value A\nValue B\nValue C" >/tmp/datfile

# initialize the variable part of the variable name
VARVAR=0

# read the data and write a temp file
cat /tmp/datfile |while read X ; do {
  echo "VAR${VARVAR}=\"${X}\"" >>/tmp/dotme
  VARVAR="`expr ${VARVAR} + 1`"
} done

# source the temp file
. /tmp/dotme

# demonstrate that it worked
echo -e "VAR0=${VAR0}\nVAR1=${VAR1}\nVAR2=${VAR2}"
32 bit core4.7.7, Xprogs, Xorg-7.6, wbar, jwm  |  - Testing -
PPR, data persistence through filetool.sh          |  32 bit core 8.0 alpha 1
USB Flash drive, one partition, ext2, grub4dos  | Otherwise similar

Offline Greg Erskine

  • Sr. Member
  • ****
  • Posts: 404
Another temp file fudge using your input file format.

Code: [Select]
awk '{ print "var"NR"="$0 }' FILE.txt > temp.sh && . temp.sh
regards

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11573
Hi sbp
If you provided some more details on exactly what you are trying to do, someone might be able to offer a better solution. In the mean
time, try doing a Google search for:
Code: [Select]
linux script dynamic array

Offline sbp

  • Sr. Member
  • ****
  • Posts: 429
    • piCorePlayer homepage
Another temp file fudge using your input file format.

Code: [Select]
awk '{ print "var"NR"="$0 }' FILE.txt > temp.sh && . temp.sh
regards

Hi Greg

Thank you for your suggestion it is working fine. I need it for allowing update of piCorePlayer in situ.
I might be needing more help, but I'm trying to get it working.

Steen

Offline Greg Erskine

  • Sr. Member
  • ****
  • Posts: 404
Another temp file fudge using your input file format.

Code: [Select]
awk '{ print "var"NR"="$0 }' FILE.txt > temp.sh && . temp.sh
regards

Hi Greg

Thank you for your suggestion it is working fine. I need it for allowing update of piCorePlayer in situ.
I might be needing more help, but I'm trying to get it working.

Steen

Hi Steen,

Happy to help if I can. Ironically I only knew an answer because I have been going over your piCorePlayer scripts.

regards

Offline theYinYeti

  • Full Member
  • ***
  • Posts: 177
    • YetI web site
If using bash, an array seems best to me:
Code: [Select]
while read line; do
  vars[${#vars[*]}]="$line"
done </path/to/file
Else use eval:
Code: [Select]
cpt=0
while read line; do
  cpt=`expr $cpt + 1`
  eval "var${cpt}=\"\$line\""
done </path/to/file
« Last Edit: May 30, 2014, 06:07:02 AM by theYinYeti »