0

I have a csv file with following details :

Userid,First Name,Last Name
jaina24,Aayush,Jain

my requirement is to add a new column in the existing file like this :

Userid,Email Address,First Name,Last Name
jaina24,[email protected],Aayush, Jain

2nd column should copy value from first column and then append @xyz.com to it.

5 Answers 5

3

You can use something like (if you have exactly 3 fields):

awk -F\, 'FNR==1 {print $1",Email Address,"$2","$3} FNR!=1{print $1","$1"@xyz.com,"$2","$3}' input_file
3

You could use awk

awk 'BEGIN{FS=OFS=","}{$1=$1 OFS (FNR>1 ? $1 "@xyz.com" : "Email address")}1' file

Or sed

sed '1s/[^,]*/&,Email address/;1!s//&,&@xyz.com/' flie

Or bash (but don't use bash for processing text - see Why is using a shell loop to process text considered bad practice?)

0
1
$ perl -F, -lane 'if ($. == 1) {
                    splice @F, 1, 0, "Email Address";
                  } else {
                    splice @F, 1, 0,$F[0] . "\@xyz.com";
                  };
                  print join ",", @F' input.csv
Userid,Email Address,First Name,Last Name
jaina24,[email protected],Aayush,Jain
1

Input File

Userid,First Name,Last Name

jaina24,Aayush,Jain
Rob738,Rob,Everhard
Scorch661,Samuel,Scorhezi

command

[ ~]# awk -F, 'NR==1, OFS="," {print $1, "Email Address", $2, $3 }''NR>2 { print $1, $1"@xyz.com", $2, $3 }' ex.csv

Output

Userid,Email Address,First Name,Last Name
jaina24,[email protected],Aayush,Jain
Rob738,[email protected],Rob,Everhard
Scorch661,[email protected],Samuel,Scorhezi
8
  • @RomeoNinov Please explain further so I am not providing bad advise and can understand what may be wrong with my code. It is one file being processed so the difference between NR and FNR should be minimal if at all noticeable.
    – sseLtaH
    Commented Jul 7, 2021 at 13:28
  • My mistake, i misread the name of variable. Nevertheless your script print only the CSV header Commented Jul 7, 2021 at 13:46
  • What's the rationale behind using the range pattern NR==1, OFS=","? And i t seems NR>2 should be NR>=2, or NR>1
    – rowboat
    Commented Jul 7, 2021 at 13:53
  • @RomeoNinov No problem. I have been looking into your former complaint all this time and discombobulating myself as I simply could not find the difference if the source is a single file. I am once again slightly confused as to how the code is printing only a header. Could you possible expand your statement? Thanks
    – sseLtaH
    Commented Jul 7, 2021 at 13:54
  • @rowboat Well.... The code basically has two parts. NR==1 Means only line 1 and OFS returns the field seperators to , Without it, it has spaces as the delimiter. As there is no input in line 2 in this example, there is no need for >= What is the difference with announcing Nr>=2, NR>1 or even NR!=1?
    – sseLtaH
    Commented Jul 7, 2021 at 13:59
0

Creating the Email Address field using Miller (mlr):

$ mlr --csv put '$["Email Address"] = $Userid . "@xyz.com"' file
Userid,First Name,Last Name,Email Address
jaina24,Aayush,Jain,[email protected]

Also moving the new field to the second column:

$ mlr --csv put '$["Email Address"] = $Userid . "@xyz.com"' then reorder -f Userid,"Email Address" file
Userid,Email Address,First Name,Last Name
jaina24,[email protected],Aayush,Jain

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .