1

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?

3
  • What's your expected output? Commented Oct 12, 2015 at 9:33
  • 7 user1 user2 user3 user1pass user2pass user2pass Commented Oct 12, 2015 at 9:34
  • 1
    As a general piece of advice, it's a lot more useful to us if you post your input/output as plain text inside a code block instead of using screenshots. Commented Oct 12, 2015 at 10:06

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

I still have the same output as before.
This is copy/paste of output I get from awk. You might have \r in your lines. Run dos2unix to fix it first.
Okay I will do that next time. Thanks for the advice

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.