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

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.
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
Using MATLAB or Octave, Write a script that prompts the user for the coordinates of three...
Using MATLAB or Octave, Write a script that prompts the user for the coordinates of three points A, B, and C, namely (xA, yA), (xB, yB), (xC, yC), forming a triangle, storing each in a variable (for a total of 6 variables). The script should then calculate the centroid G = (xG, yG) using xG = xA+xB+xC 3 and yG = yA+yB+yC 3 , storing each in a variable. Finally, the script should print all four points. Sample output: Enter...
Linux Given a passed parameter to a script, request a number from the user and print...
Linux Given a passed parameter to a script, request a number from the user and print the parameter as many times as that number.
LINUX In Linux command line write a shell script ex1.sh that uses IF THEN to Prompt...
LINUX In Linux command line write a shell script ex1.sh that uses IF THEN to Prompt the user to "Enter a number between 1 and 10". (Hint: Use the 'echo' and 'read' commands in the script. See the slide about the 'read' command) If the number is less than 5, print "The number is less than 5" (Hint: You will read input into a variable; e.g. read NUM. In the IF statement, enclose $NUM in quotes; e.g. "$NUM". Also, remember...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT