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?
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]
$
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.
$ 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.