Getting lines from .txt file

Hi

I have a file with contents:

NAMES
John
carrey
williams

How can I get all the names and store them in seperate variables(or arrays)

please keep in mind that the no. of such names is not known.Three here is a bogus value

~thanks

Since you've not specified the language, I've assumed that you need a bash script.

[user@host ~]$ cat>file
John
carrey
williams
[user@host ~]$ cat test.sh
#! /bin/bash

i=0

while read line
do
    namesArr[$i]=$line
    (( i++ ))
done < file

echo ${namesArr[@]}
[user@host ~]$
[user@host ~]$ ./test.sh
John carrey williams
[user@host ~]$

my bad !
Its bash :slight_smile:
how can we use awk for this ??

Using awk

awk '{a[i++]=$0} END{for(i in a){print a}}'  inputfile

If you don't want the Names' file to be among the files to be worked upon, you could also try

awk 'BEGIN {while (getline A[++i] < "file"); . . . 

In bash , you might want to consider the mapfile builtin.