I have a problem with a SED command I need to replace the @ character with a string and append a string to a column in a csv file

So

 

Would become

 

I’m using the following sed command

sed -i .bak -E 's/([^,\n\“]*)@([^,\n\“]*)/\1_X_\2@ Test.csv 

But I have a new line break somewhere in the command like so

foo_X_exxample.com @newdomain.com 

Can anyone advise where the error in my SED command is?

6

1 Answer

It appears that your original file test.csv is Windows-formatted, with return (0x0d or \r) and line feed (0x0a or\n) for each new-line. Your sed search pattern does not exclude \r so this will be retained in the second matched field and output before the appended text.

So your original command:

sed -i .bak -E 's/([^,\n\“]*)@([^,\n\“]*)/\1_X_\2@ Test.csv 

gives the output file (using od -c to see the control characters):

0000000 f o o _ X _ e x a m p l e . c o 0000020 m \r @ n e w d o m a i n . c o m 0000040 \n 

If you add \r to the excluded characters:

sed -i .bak -E 's/([^,\r\n\“]*)@([^,\r\n\“]*)/\1_X_\2@ Test.csv 

then the original new-line sequence is maintained:

0000000 f o o _ X _ e x a m p l e . c o 0000020 m @ n e w d o m a i n . c o m \r 0000040 \n 

Note that you don't need \r or \n in the first matching field, as this form of sed command will match only within single lines and not through new-lines.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy