1

I have a text file named "raj.txt" containing following content:


raj magesh popey ravi


How can I assign each word to array element? a[0]=raj a[1]=magesh a[2]=popey a[3]=ravi

1
  • each line or word in a line to array? Commented Nov 13, 2014 at 8:58

2 Answers 2

1

Try bash:

while IFS= read -r line
do
   set -- $line
   echo "$1"
   echo "$2"
done < file
Sign up to request clarification or add additional context in comments.

Comments

0

If your shell support array, like bash, zsh, ksh93, try:

$ array=($(<filename))
$ printf '%s\n' "${array[0]}"
raj
$ printf '%s\n' "${array[1]}"
magesh
$ printf '%s\n' "${array[2]}"
popey
$ printf '%s\n' "${array[3]}"
ravi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.