Mathias Verhoeven
Mathias Verhoeven

Reputation: 1057

Bash : adding text from textfile to variable using awk

I am trying to use the values from my textfile in my bash script

AantalUsers=$(cat newusers.txt | wc -l) 
echo $AantalUsers
lijstUsername="" 
lijstWachtwoord=""

for ((i = 0 ; i < $AantalUsers ; i++ )); 
do
  lijstUsername=`awk -F" " '{print $1}' test.txt` 
  lijstWachtwoord=`awk -F" " '{print $2}' test.txt` 
done <test.txt 

echo $lijstUsername 
echo $lijstWachtwoord

My textfile looks like this :

enter image description here

This is the output that I get :

enter image description here

The first list works great but for the second one I only get the last value of the textfile.

What could be the problem?

Upvotes: 1

Views: 32

Answers (1)

anubhava
anubhava

Reputation: 784938

You can use this awk command:

awk '{
   for (i=1; i<=NF; i++)
     a[i,NR] = $i;
   n = (n < NF ? NF : n)
}
END {
  for (i=1; i<=n; i++)
     for (j=1; j<=NR; j++)
        printf "%s%s", a[i,j], (j==NR?ORS:OFS)
}' file

Output:

user1 user2 user3
user1pass user2pass user3pass

Upvotes: 1

Related Questions