Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-28-2009, 11:08 AM   #1
LQ Newbie
Registered: Aug 2009
Posts: 2

Rep: Reputation: 0
bash search and replace question


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 :

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

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

...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 :

while read username passwordhash
echo "Username : " $username ", password : " $passwordhash
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.
Old 10-28-2009, 11:20 AM   #2
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you can see a similar example here. Just change the field numbers around.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Search and Replace in bash script daemoneye Other *NIX 1 04-08-2009 01:24 AM
Does anyone know of a bash script can search & replace txt in a file. jimwelc Linux - Newbie 6 09-15-2008 12:13 AM
Bash search and replace utility cpedros Programming 3 03-12-2008 01:02 PM
Search and Replace from bash hammertime1983 Linux - Newbie 4 11-02-2007 03:18 PM
sed question for search and replace jakev383 Linux - General 8 05-05-2007 05:40 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:39 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration