3
while IFS=, read -r col1 col2 col3
do
    email="$col3"
    email+='@gmail.com'
    echo $email

done < ~/Desktop/Names.csv

I don't have to do anything special with Column 1 or 2, but adding @gmail.com to column 3 just outputs @gmail.com or if the input string is longer @gmail.com+longer bit of input string.

ie.

If column 3 was Dekkars, I get @gmail.com. If it is aaaaaaaaaaa (one length longer than @gmail.com) I get @gmail.coma

I'm sure this is something to do with the @ sign, but I've tried using \ to escape it, single quotes, etc. Any ideas?

I've already read concatenating bash strings, and I'm doing what it suggests with different outcomes than are expected.

Here is input Data

Test Name,8,aaaaaaaaaaa
John Doe,8,bbbbbbbbbbbb
Name,Grade,ID

(Note, I have columns at bottom because otherwise my while loop won't read the bottom row)

Output

@gmail.coma
@gmail.combb
Dekkars
  • 49
  • 3
  • Did you try `email="${col3}@gmail.com"` instead? In not sure about `+=` in bash... The `@` is protected by single quotes so it's not interpreted. – Matthieu Oct 25 '19 at 11:44
  • Still doing the same thing! This is driving me batty. Wondering if it isn't just easier having another column on the .csv with the email already there... – Dekkars Oct 25 '19 at 11:50
  • 1
    when you work on column-based data, consider giving awk a try. – Kent Oct 25 '19 at 12:03
  • 1
    Please provide at least one line of the input data, better two -- the code work as is in bash and zsh when typed into the shell. – Tom Regner Oct 25 '19 at 12:11
  • Added input and output – Dekkars Oct 25 '19 at 12:19
  • 1
    I smell a problem with line endings. `$col3` might contain a CR (`\r`, `0x0D`) at the end; printing that and having it followed by `@gmail.com` writes `@gmail.com` to the start of the line. – glglgl Oct 25 '19 at 12:29
  • @glglgl you're right, that's it. I 'unix2dos'ed my example csv file and that produces the output Dekkars describes. @Dekkars: does `file ~/Desktop/Names.csv` say 'with CRLF line terminators'? Then `dos2unix ~/Desktop/Names.csv` and see if that helps. – Tom Regner Oct 25 '19 at 12:40
  • `awk -F , '{ print $3 "@gmail.com" }' Names.csv` should be noticeably faster as well as fix the quoting error. – tripleee Oct 25 '19 at 15:18

1 Answers1

0

dos2unix fixed the problem. Incorporated it into my script so I don't have to remember to call it every time I change the .csv...

Thank you all!

Dekkars
  • 49
  • 3