LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices

Reply
 
Search this Thread
Old 11-27-2007, 12:02 PM   #1
RajRed
Member
 
Registered: Mar 2005
Posts: 66

Rep: Reputation: 15
use script to create a data file of mulitple users


How do I write a script that read a line from a file and recreate mulitple lines in another file? for example, I have one line information about a user, the prefix user name is 'test', user's email address is 'test@abc.xyz.com', the alias is 'test' etc. I want to populate this data into the following,


test1 test1@abc.xyz.com test1
test2 test2@abc.xyz.com test2
test3 test3@abc.xyz.com test3
:
test5000 test5000@abc.xyz.com test5000

In other word, the script needs to read the first token (user name prefix) from the first file and create mulitple users by appending the counts and replace the email address from the first time with the new user name and the alias. The script will accept the command line parameter for the max number of users like

"createuser.sh 5000" will create 5000 users, 1 line per user.

Any help is appreciate.
 
Old 11-27-2007, 01:18 PM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
This is either mondo sysadmin stuff or homework. Assuming the former, this is kinda crappy, but
Code:
user="$(awk '{ print $1 }' file)"
>file2
for (( i=1; i<=$1; i++)); do
    sed "s/$user/$user$i/g" file >> file2
done
might do if file contains only a single line in the format you gave. Otherwise, you'd need a way to extract the line from the first file. And this might do that,
Code:
user="$(awk '/'$1'/{ print $1 }' file)"
>file2
for (( i=1; i<=$2; i++)); do
    sed "s/$user/$user$i/g" file >> file2
done
Doesn't work if USER is 'John Doe', though. A 'John Doe5000' doesn't make much sense, though.

I'm sure there's a better way, but that's the first thing that came to mind.
 
Old 11-27-2007, 01:22 PM   #3
JSkywalker
Member
 
Registered: Aug 2007
Distribution: openSUSE 12.1
Posts: 96

Rep: Reputation: 24
something like:
Code:
#!/bin/bash

for ((x=1; x<=$1; x++)) do
        echo test$x test$x@abc.xyz.com test$x;
done
 
Old 11-27-2007, 03:22 PM   #4
RajRed
Member
 
Registered: Mar 2005
Posts: 66

Original Poster
Rep: Reputation: 15
thanks, for the code you provided below, where I specify the first file?

user="$(awk '/'$1'/{ print $1 }' file)"
>file2
for (( i=1; i<=$2; i++)); do
sed "s/$user/$user$i/g" file >> file2
done
 
Old 11-27-2007, 06:05 PM   #5
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
The first $1 is bash expecting the user name you want, like 'test'. The second $1 is awk grabbing the first field. The $2 is the number argument, like '5000'. So it'd be 'createuser.sh test 5000'. I thought the file names were supposed to be 'hard coded'. If you wanted to provide the file names as command line arguments, just substitute 'file' (input) and/or 'file2' (output) with $3 and $4 throughout. 'Course, then you're getting into the ballpark of maybe wanting to use getopts and letter options, rather than trying to remember the order of everything.
 
  


Reply


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
Replace word in file-mulitple times. AlanL Linux - Software 5 06-25-2007 02:14 AM
Create a script to display file name, Inode, and size of any file. Has to be a script JaxsunApex Linux - Newbie 7 01-29-2007 08:15 PM
Script to create multiple directories under users homes shawnbishop Linux - Software 4 03-30-2006 08:11 AM
perl script help required to create users and changing the owner to pericular folder sridhar11 Programming 2 10-25-2005 02:16 PM
create users using script polis Linux - General 1 05-18-2004 08:19 AM


All times are GMT -5. The time now is 03:16 PM.

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