Read the contents of a file and store them in a variable

Hi Gurus,

I am trying for a scenario where in I want to read the contents of a file line by line and then store them in variables. Below is the script:

#!/bin/ksh

while read line

   do

    id=`echo $line | cut -f1 -d |`
    name=`echo $line | cut -f2 -d |`
     
      echo $id
      echo $name
   done < temp.txt

The contents of the temp file is

kgsc12333,mike anderson
oxftd14442,mc donald
tddff232233,william carlson

I want to strip the word before and after the "," and store them in 2 variables id and name. But the script is throwing some error.

Please help me on this.

Regards,
Sam

try this:

#!/bin/ksh
while read line
do
  id=`echo $line | cut -d, -f1`
  name=`echo $line | cut -d, -f2`
  echo $id
  echo $name
done<1.txt

There's no need to use external commands:

#!/bin/ksh

while read line
do
  id=${line%,*}
  name=${line#*,}
  echo $id
  echo $name
done < temp.txt
1 Like

Thanks Anurag and Franklin for your quick response. I tried Franklin's approach, its working.

Franklin's approach looks better and working well for me.
There might be some problem in your script. Please post your script printing each line twice.

Hi all,
You could try this , by letting the IFS (input field separator) handle the logics for You:

#!/bin/ksh
IFS=,
while read id name; do
echo $id
echo $name
done < id-name.lst

If You know Your input is well formatted.

Best regards,
Lakris

Thanks everyone. I tried Franklin's approach and it is working fine. Now my question is how to get the value from 3rd column. I suppose Franklin's script will only work if there 2 columns. But I also need to get the value from 3rd column. Please help me on this.

kgsc12333,mike anderson,rwx
oxftd14442,mc donald,rx
tddff232233,william carlson,r

Thanks in advance

Use Lakris' solution, it will work

#!/bin/ksh
IFSsav="$IFS"
IFS=,
typeset -i cnt
cat temp.txt | while read line
do
    set $line
    cnt=1
    while [ $cnt -le $# ]
    do
        eval "val=\$$cnt"
        echo $val
        cnt=cnt+1
    done
done
IFS="$IFSsav"

for temp.txt

kgsc12333,mike anderson
oxftd14442,mc donald
tddff232233,william carlson
vhfdgfkd,jfhjfhsdjklf,jhfhdf,1234
jkfgh djfh, djfh  dfhfh, jkhdj 323

output is

kgsc12333
mike anderson
oxftd14442
mc donald
tddff232233
william carlson
vhfdgfkd
jfhjfhsdjklf
jhfhdf
1234
jkfgh djfh
 djfh  dfhfh
 jkhdj 323

Hi, something like this?

#!/bin/ksh
IFS=,
while read id name attr; do
echo $id
echo $name
echo $attr
done < id-name.lst

Best regards,
Lakris

@lakris. Do not forget to save IFS in a variable so it can be restored to its original value afterwards.
There is another possibility:

while IFS=, read id name attr x
do
  echo "$id"
  echo "$name"
  echo "$attr"
done < temp.txt

With this construct IFS does not need to be restored afterwards. (I included an extra variable (x), because I suspect the OP may have a line with even more fields...)

Good point, especially if one does other stuff after, that depends on IFS. Embedding it in the while statement is of course the best option.

/L