There are supposed to be 2 files. One file has list of users and the other files has list of passwords. User file has 5 users and password file also has 5 passwords. What I have to do here is create a new file and pair each user with all the available passwords in password file. For example: user 1 is to be paired with password 1, 2,3,4,5. user 2 is to be paired with password 1,2,3,4,5 and so on.
My question is: 1. What loop should be used here? 2. My head says for pairing we need to use nested for loop. is it correct? 3. I can somehow imagine the 1st part of copying the contents but I'm not able to picture how to pair them. So I need help and suggestions.
EDIT:
Sample input are the 2 files named Username and Password.
Username file:
User1
User2
..
User5
Password file:
Pass1
Pass2
..
Pass5
Expected output is:
User1-Pass1
User1-Pass2
..
User1-Pass5
User2-Pass1
User2-Pass2
..
User2-Pass5
and so on till we reach User5-Pass5
Thanks.
Ad 1., 2. Yes, nested loop required.
Let's expect one user per line and one password per line, then code would be:
> concat # delete file content
for usr in `cat file_with_users`
do
for pwd in `cat file_with_passwords`
do
echo "$usr-$pwd" >> result_file
done
done
If you write input sample and expected output, I can write something more specific.
always there is a better way
$ join -t, -j 99 users pwords | cut -d, -f2-
for example
$ echo u{1..5} | tr ' ' '\n' > users
$ echo p{1..5} | tr ' ' '\n' > pwords
$ join -t, -j 99 users pwords | cut -d, -f2-
u1,p1
u1,p2
u1,p3
u1,p4
u1,p5
u2,p1
u2,p2
...
u5,p4
u5,p5
for -
delimiter change to join -t- -j 99 users pwords | cut -d- -f2-
join -t- -j 99 users pwords | cut -d- -f2-
This is using a fictional column (99th) to create a join between each rows, which is called cross-product. Since the key is missing in the first position of the output we need to cut it out at the end.
Do either of these help:
grep -Fwf file1 file2 or
awk 'NR==FNR{A[$1];next}$1 in A' file1 file2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.