1

Have a csv file with tons of rows, small example:

id,location_id,name,title,email,directorate 
1,1, Amy lee,Singer,, 
2,2,brad Pitt,Actor,,Production 
3,5,Steven Spielberg,Producer,[email protected],Production

Need to:

  • change first and last name to uppercase, example, Brad Pitt, Amy Lee.
  • create email with pattern first letter of first name + last name, all in lowercase with @google.com and value from location_id, example - [email protected], [email protected]
  • save it to new file.csv, with the same structure, example:
id,location_id,name,title,email,directorate 
1,1, Amy Lee,Singer,[email protected], 
2,2,Brad Pitt,Actor,[email protected],Production 
3,5,Steven Spielberg,Producer,[email protected],Production

I started from create a array and iterate through it, with bunch of sed, awk, but it gives to me random results. Please give me advice, how resolve this task.

while read -ra array; do
    for i in ${array[@]};
    do
        awk -F ',' '{print tolower(substr($3,1,1))$2$3"@google.com"}'
    done

    for i in ${array[@]};
    do
        awk -F "\"*,\"*" '{print $3}' | sed -e "s/\b\(.\)/\u\1/g"
    done

done < file.csv

awk -F ',' '{print tolower(substr($3,1,1))$2$3"@google.com"}' working not correct.

3
  • 1
    Please share what you have tried so far. Commented Nov 4, 2022 at 8:30
  • @HatLess I'm sorry I didn't do this earlier. Commented Nov 4, 2022 at 9:38
  • See stackoverflow.com/questions/45420535/… Commented Nov 4, 2022 at 15:52

2 Answers 2

3

Using GNU sed

$ sed -E 's/([^,]*,([^,]*),) ?(([[:alpha:]])[^ ]* +)(([^,]*),[^,]*,)[^,]*/\1\u\3\u\5\L\4\6\[email protected]/' input_file
id,location_id,name,title,email,directorate
1,1,Amy Lee,Singer,[email protected],
2,2,Brad Pitt,Actor,[email protected],Production
3,5,Steven Spielberg,Producer,[email protected],Production
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. Just 1 line of script to solve this task, without any while and for loops...
help me plz again? how script will be modified, if location_id need to add only when emails a equal? example: 1,1,Amy Lee,Singer,[email protected], 2,2,Brad Pitt,Actor,[email protected],Production 10,30,Andrew Lee,Singer,[email protected]
@lunnyj I am not sure I fully understand the new request. Can you open a new question and add all requirements, what you have tried and the expected output, I will try and help.
3

With your shown samples please try following awk.

awk '
BEGIN{ FS=OFS="," }
{
  split($3,arr," ")
  val=(substr($3,1,1) arr[2]"@google.com,")
  $NF=tolower(val) $NF
  val=""
} 
1
' Input_file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.