Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append a text after a comma in a csv file using Linux bash in a loop

Tags:

bash

shell

I need to modify a csv file using bash.

Input (a csv file):

firstletter="s"
surname="houston"
emaildomain"@zzz.com"
input=$(cat 1.csv)

1.csv:

1,1,Susan houston,Director of Services,,
2,1,Christina Gonzalez,Director,,
3,2,Brenda brown,"Director, Second Career Services",,

How can I add a text between the last 2 commas using Linux bash? I tried something like:

for i in $(cat $input);do
        sed -i "s/,$/${firstletter}${surname}${emaildomain},/g" $i;
done

However, that results in an error: sed: -e expression #1, char 5: unterminated `s' command

Expected output:

1,1,Susan houston,Director of Services,[email protected],
2,1,Christina Gonzalez,Director,[email protected],
3,2,Brenda brown,"Director, Second Career Services",[email protected],
like image 790
NetRanger Avatar asked Sep 18 '25 00:09

NetRanger


2 Answers

The question isn't clear but I think this might be what you're trying to do, using GNU awk for "inplace" editing and gensub():

$ cat 1.csv
1,1,Susan houston,Director of Services,,
2,1,Christina Gonzalez,Director,,
3,2,Brenda brown,"Director, Second Career Services",,
$ awk -i inplace 'BEGIN{FS=OFS=","} {$(NF-1)=tolower(gensub(/(.).* (.*)/,"\\1\\2",1,$3) "@zzz.com")} 1' 1.csv
$ cat 1.csv
1,1,Susan houston,Director of Services,[email protected],
2,1,Christina Gonzalez,Director,[email protected],
3,2,Brenda brown,"Director, Second Career Services",[email protected],

See What's the most robust way to efficiently parse CSV using awk? for more information on processing CSVs with awk.

like image 169
Ed Morton Avatar answered Sep 20 '25 15:09

Ed Morton


I'm sure there's some cleverness with sed that would achieve what you want, I'd personally go w/ GNU awk here.

cat a.csv
1,1,Susan houston,Director of Services,,
2,1,Christina Gonzalez,Director,,

gawk -i inplace   'BEGIN{FS=OFS=","}{fn=gensub(/(.).*/,"\\1","1",$3);split($3,ln," ");$5=fn"."ln[length(ln)]"@zzz.com";print}' a.csv

cat a.csv
1,1,Susan houston,Director of Services,[email protected],
2,1,Christina Gonzalez,Director,[email protected],

-i inplace is a GNU extension that allows awk to emulate seds -i.

The BEGIN section tells awk that both the input and output field separator are commas.

fn=gensub(...) pulls the first letter of the first name (full name being the third field, $3).

We then split the name into an array ln (assuming some people may have middle names).

We set the 5th field (the empty space between the last commas) to the the first letter, and the last element of the array followed by @zzz.com.

If $5 is not empty:

gawk -i inplace   'BEGIN{FS=OFS=","}$5==""{fn=gensub(/(.).*/,"\\1","1",$3);split($3,ln,"");$5=fn"."ln[length(ln)]"@zzz.com"}{print}' a.csv
like image 35
tink Avatar answered Sep 20 '25 14:09

tink