Operating System - Linux
1760015 Members
3006 Online
108889 Solutions
New Discussion юеВ

Re: Incrementing two lists for variables at the same time.

 
SOLVED
Go to solution
Patrick Ware_1
Super Advisor

Incrementing two lists for variables at the same time.

Hello,

I need help with finding a solution to an ongoing challenge for me. I need to find a way to increment lists for variables at the same time. Here is an example of my current challenge:

I need to change a whole slew of users accounts to a new login ID.
I have two lists. One list has the current user ID's, and the other list has the new user ID's. How can I script this, so that both lists are read in order, and in sync? In other words, I want the first line of the first list to correspond with the 1st line of the 2nd list, and so on. How would I do this?

8 REPLIES 8
Oviwan
Honored Contributor

Re: Incrementing two lists for variables at the same time.

Hey

please post the format of the files.

Regards
Patrick Wallek
Honored Contributor
Solution

Re: Incrementing two lists for variables at the same time.

If the files you want are currently sorted in the order you want, why not use 'paste' to combine the 2 files into a single file?

# paste file1 file2 > file3

Then when you script your user modification routine do something like:

while read OLDUID NEWUID
do
...do stuff here...
done < file3

Patrick Ware_1
Super Advisor

Re: Incrementing two lists for variables at the same time.

Both files have the following format:

user1
user2
user3
user4
user5
user6
user7
user8
user9
user10


...and so on...
David Bellamy
Respected Contributor

Re: Incrementing two lists for variables at the same time.

Patrick paste is the perfect solution for what you need.
file1:
01
02
03
04
05
file2:
06
07
08
09
010

paste file1 file2 >file3

file3
01 06
02 07
03 08
04 09
05 010
hope this helps
Patrick Ware_1
Super Advisor

Re: Incrementing two lists for variables at the same time.

I think I understand now. Let me give this a try, and I'll let you all know how I make out. If you have other ways, don't hesitate to post them also. Thanks to all!
Patrick Ware_1
Super Advisor

Re: Incrementing two lists for variables at the same time.

Ok, so is it possible to just pull from two lists the way I originally specified?
Patrick Ware_1
Super Advisor

Re: Incrementing two lists for variables at the same time.

Patrick W.,

When I do this:

while read OLDUID NEWUID
do
echo $OLDUID $NEWUID
done < users.txt


I get this:

user1-1 user2-1
user1-2 user2-2
user1-3 user2-3
user1-4 user2-4
user1-5 user2-5
user1-6 user2-6


It is working! Now to modify it to my needs!! Thx all!
Steven Schweda
Honored Contributor

Re: Incrementing two lists for variables at the same time.

A shell-only solution can be constructed
using more exotic I/O redirection, providing
a look (open, read, ...) closer to that of a
regular programming language (like DCL, for
example):

dy # cat ./nm.dat
aaa
bbb
ccc
dy # cat ./nr.dat
111
222
333
dy # cat ./rdr.sh
#!/bin/sh

exec 3<./nm.dat
exec 4<./nr.dat

while read nm <&3 ; do
read nr <&4
echo "nm: >${nm}<, nr: >${nr}<."
done

dy # ./rdr.sh
nm: >aaa<, nr: >111<.
nm: >bbb<, nr: >222<.
nm: >ccc<, nr: >333<.

It's also possible to add some error handling
to cope with file pairs with different line
counts, and it may be easier to do this well
if you keep the files' data separate. (After
"paste" smooshes them together, it may not be
obvious which one has the problem.)


Reverse the "<" characters, and it's possible
to write to more than one file at a time
(more than only stdout), too, just as this
script shows how to read from more than one
file at a time (more than only stdin).