LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash search and replace question (https://www.linuxquestions.org/questions/linux-newbie-8/bash-search-and-replace-question-765067/)

jessjames 10-28-2009 11:08 AM

bash search and replace question
 
Hi

I need to achieve something very specific, and all the responses I find do not quite match my requirements. Therefore, if I am repeating a question I apologise.

I am trying to import many many users from one server to another. I can automate the user creation, but I need to copy over the passwords. To achieve this I'm planning to copy the hash from /etc/shadow on the old server to the same file on the new one.

I do not know the individual users' passwords from a plain text point of view, but I have confirmed that simply copying the hash over to the new shadow file maintains the password and the login works.

So what I'm trying to do is this :

I have a lookup list of usernames and hash's from the old server that looks like this :

Quote:

username $1$EK/hjwGC$MtKJj/EpCvj32Ay4v4FAi1
So... two fields seperated by a space.

I have a destination shadow file which might look like this :

Quote:

username:!!:14532:0:99999:7:::
...since I've created all my new users without passwords at the moment.

And so, my solution would involve searching for each individual username in the destination shadow file, (username is specified by field 1 in my lookup list), and on that line replace the string "!!" with the correct password hash (hash specified by field 2 in my lookup list).

I can pull out the two fields from my lookup list by doing something like :

Quote:

while read username passwordhash
do
echo "Username : " $username ", password : " $passwordhash
done < $SOURCEFILENAME
But I then need to use sed (or similar) to grep for one string, and on the resulting line replace another string with the new hash using the information from my lookup list for the search and replace strings, and without affecting the rest of the file.

I realise this is a very specific requirement, and there might be a better way to approach it altogether, but logically this seemed like a good solution. I'm just missing the scripting logic/commands.

Any suggestions on this would be awesome, since I'm not much of a bash guru, and it's driving me slightly insane :)

Many thanks in advance.
Jess

ghostdog74 10-28-2009 11:20 AM

you can see a similar example here. Just change the field numbers around.


All times are GMT -5. The time now is 12:26 AM.