How to read a text file and assign the values in the same to a variable in loop

Hi,

I have a text file with multiple lines, each having data in the below format <DOB>,<ADDRESS>

I have to write a script which reads each line in the text file in loop, assign the values to these variables and do some further processing in it.

Using the following code prints the values but I need them in variables.Also, it reads only first line of the file.
cat filename | paste -d"," - - - | awk 'BEGIN{FS=","}{print "DOB="$1" ADDRESS="$2" }'
Am I wrong in my approach?

post the sample input file

1 Like

The sample file will be a text/csv file with datas as

C:/My Documents/Data,20
D:/My photos,30

I need a way to read the values of the folder path and age of files in different variables from different lines in a loop.

for i in `cat filename`
do
 file_path=`echo  $i | cut -d"," -f1`
 age=`echo $i | cut -d"," -f2`
 
 echo $file_path
 echo $age 
done

1 Like

Its working, Thank you!

one more way

while read line ; do
path=`echo $line | awk -F',' '{print $1}'`
age=`echo $line | awk -F',' '{print $2}'`
echo $path 
echo $age
done < filename
1 Like

Which saves some processes since there are no pipelines. Just tell the shell the Input Field Separator is the character of the field separator in the file and it automatically will use that to parse each line:

#!/bin/ksh

file="inputfile.txt"

while IFS=',' read path age
do
  print "[$path] [$age]"
done < $file

exit 0

Output:

$ a
[C:/My Documents/Data] [20]
[D:/My photos] [30]
$

Definitely the cleanest, most efficient way to handle this scenario.

Just in case a novice thinks they need ksh to run that, /bin/sh should do just fine.

Regards,
Alister

But for /bin/sh one would need to change the "print" command to "echo" but the main logic will still work. For the sake of the novices, "print" is built-in to ksh and this runs faster where "echo" is actually another program that must be called and thus brings along some overhead.

My normal shell is ksh93 on Solaris so that's how my brain thinks these days. :slight_smile:

Hi, gary_w:

You are absolutely correct about print. I overlooked it.

However, you are mistaken about echo. It is actually built into ksh93. At least, it is into the one I just tested.

Even so, I'm not a big fan of echo myself. I much prefer the portability and safety of printf.

Regards,
Alister

$ echo $SHELL
/usr/dt/bin/dtksh
$ which echo
/bin/echo
$ ksh
$ which echo
/bin/echo
$

echo seems to be a separate program on our Solaris box, but I believe we are not on the latest version anyway. Just goes to show you not to assume anything and know that things may work differently then expected depending on the OS version and shell version you are using!

Just having an external echo doesn't mean your shell actually uses it. There's supposed to be back-up externals for any shell internal.

That's often taken literally to an extreme, to the point some vendors supply an external cd program. Think about that for a minute. What would an external cd program do in your shell.

$ command -V echo
echo is a shell builtin
$ command -V print
print is a shell builtin
$ which echo
/bin/echo
$ which print
no print in /opt/oracle/10.2.0/bin /opt/oracle/10.2.0/bin /sbin /usr/sbin /etc /usr/ccs/bin /usr/openwin/bin 
/usr/dt/bin /bin /usr/bin /usr/ucb /etc /home/efs/bin . 
$

So it seems echo is indeed built-in and external where print is only built-in.