Question

In: Computer Science

write a script in ruby to automate user account creation in Linux using a CSV file....

write a script in ruby to automate user account creation in Linux using a CSV file. Username is a combination of first initial last initial of the first name and last name in the last name followed by the first initial last initial(Ex. Sam Smith = smithsm). If two or more employees had the same first and last name appends a number to the end of the username. After accounts are created write the first and last names along with their username to a CSV file

CSV Example:

first_name last_name

Bob Key

Jeff Scolding

Peter Van't Blob

Sam Smith

Sam Smith

Solutions

Expert Solution

Program

require 'csv'

arr=CSV.read("input.csv")

outputArr=[['first_name'],['last_name'],['username']]

arr.each_with_index do |item, index|

    

    if index != 0 then # skip first row as it is heading of column

        # puts arr[index][0]

        outputArr[0].push( arr[index][0]) # firstname

        outputArr[1].push( arr[index][1]) # lastname

        initial_char=arr[index][0][0] # get last name from arr

        last_char=arr[index][0][arr[index][0].length-1] # get first name last char

        

        username=arr[index][1]+initial_char+last_char # join last name and first name initial char and firstname last char

        if outputArr[2].include? username then # if username already exists

            username=username+index.to_s # append index number converted to string to username

        end

        username=username.downcase #  cnvert username to lowercase

        outputArr[2].push( username)

    end

end   

csvArr=[]

for i in 0..outputArr[0].length()-1 # loop to convert rows to columns

    tempArr=[]

    for j in 0..outputArr.length()-1

        tempArr.push(outputArr[j][i])

    end

    csvArr.push(tempArr)

end

File.write("output.csv", csvArr.map(&:to_csv).join)

puts "csvArr array writen to output.csv successfully"

OUtput

> ruby parseCsv.rb
csvArr array writen to output.csv successfully

input.csv

output.csv

Both csv files will be in same folder where .rb file is kept


Related Solutions

Linux project deployment Script Create a script “setup.sh” to automate the deployment of a flask application....
Linux project deployment Script Create a script “setup.sh” to automate the deployment of a flask application. The script must install all software dependencies such as: Python3 Pip3 Git Use github to clone this repository. Install the dependencies given in requirements.txt Start the server by executing the following command “gunicorn wsgi:app” requirements.txt Click==7.0 Flask==1.0.2 Flask-Cors==3.0.7 Flask-JWT==0.3.2 Flask-SQLAlchemy==2.3.2 gunicorn==19.9.0 itsdangerous==1.1.0 Jinja2>=2.10.1 MarkupSafe==1.1.1 PyJWT==1.4.2 six==1.12.0 SQLAlchemy==1.3.1 werkzeug>=0.15.3
In Kali Linux Write a script that ask the user to enter an IP address and...
In Kali Linux Write a script that ask the user to enter an IP address and a port number, then use the provided entries from the user to perform a query on your local network and determine if the given port is open on the provide network. Need to submit solutions for both below. 1.A short report showing the functionality of your code 2. your bash script
Write a script that prompts the user for a pathname of a file or directory and...
Write a script that prompts the user for a pathname of a file or directory and then responds with a description of the item as a file along with what the user's permissions are with respect to it (read, write, execute).
Using Python The script is to open a given file. The user is to be asked...
Using Python The script is to open a given file. The user is to be asked what the name of the file is. The script will then open the file for processing and when done, close that file. The script will produce an output file based on the name of the input file. The output file will have the same name as the input file except that it will begin with "Analysis-". This file will be opened and closed by...
Write a Powershell script to create a Windows user account that is a non-admin user. This...
Write a Powershell script to create a Windows user account that is a non-admin user. This assignment assumes that you already have a Windows VM, with the administrator account created.. To complete the assignment, write the PowerShell script, run it on your VM, and submit the script here.
Please write a C++ code that inputs a CSV file, asks the user if they'd like...
Please write a C++ code that inputs a CSV file, asks the user if they'd like to remove certain words from the file, and removes the words from the file. Please note that the CSV file has two columns, the first with the words, and the second with a count of how many times each word appears.
Task 2.5: Write a script that will ask the user for to input a file name...
Task 2.5: Write a script that will ask the user for to input a file name and then create the file and echo to the screen that the file name inputted had been created 1. Open a new file script creafile.sh using vi editor # vi creafile.sh 2. Type the following lines #!/bin/bash echo ‘enter a file name: ‘ read FILENAME touch $FILENAME echo “$FILENAME has been created” 3. Add the execute permission 4. Run the script #./creafile.sh 5. Enter...
Write a brief shell script that will take in a specific file name, prompt the user...
Write a brief shell script that will take in a specific file name, prompt the user whether they would like to gzip, bzip2, or xz compress the file. Depending on response, the script then ought to compress the provided file with the corresponding method
Linux Sign into your lab account Write a bash shell script that does the following: Print...
Linux Sign into your lab account Write a bash shell script that does the following: Print out a friendly welcome message Ask the user for the name of their favorite animal Grep that animal from a text file noises.txt Tell the user what that animal says (i.e. what noise does it make) If the animal does not exist ask the user what noise the animal makes Store that new information in noises.txt
Linux Have your script request a word from the user, then checks if it is an...
Linux Have your script request a word from the user, then checks if it is an ordinary file. If yes display the size of the file. If the name is a directory, then display the number of objects in that directory, then display the second line of a long listing of that directory.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT