You can use the following syntax using ksh:
#!/bin/kshSame code should work with bash too:
file="/home/vivek/data.txt"
while read line
do
# display $line or do somthing with $line
echo "$line"
done <"$file"
#!/bin/bashYou can also read field wise:
file="/home/vivek/data.txt"
while IFS= read -r line
do
# display $line or do somthing with $line
echo "$line"
done <"$file"
#!/bin/bash
file="/etc/passwd"
while IFS=: read -r f1 f2 f3 f4 f5 f6 f7
do
# display fields using f1, f2,..,f7
echo "Username: $f1, Shell: $f7, Home Dir: $f6"
done <"$file"
No comments:
Post a Comment