I'm trying to edit an existing process we have that is called from SQL Server 2008r2 via xp_cmdshell. It replaces/removes certain characters in a csv file before it is then imported. I've managed to add in removing fullstops but am having trouble getting it to remove backticks. Every way I have tried to escape it I get the error sed: -e expression #1, char 97: Unterminated `s' command.
In SQL:
DECLARE
@FileLocation VARCHAR(255) = '\\FILELOCATION\'
,@ArchiveLocation VARCHAR(255) = '\\FILELOCATION\Archive\'
,@FileName VARCHAR(50) = 'DATE_FILENAME.csv'
,@UtilitiesLocation VARCHAR(100) = '\\NETWORKLOCATION\UTILITIES\'
,@Command VARCHAR(2000)
DECLARE @SedCommand VARCHAR(100) =
's/£//g;' -- remove pound signs
+ 's/\.//g;' -- remove full stop
+ 's/\`//g;' -- remove grave accent (backtick)
+ 's/\"\([A-Za-z- '']*\),\([A-Za-z- '']*\)\"/\1|\2/g;' -- "Last, First" replaced by Last|First
+ 's/\"\([0-9.-]*\),\([0-9.-]*\)\"/\1\2/g;' -- remove thousands separators in numbers
SET @Command =
'copy '
+ @FileLocation + @FileName + ' '
+ @ArchiveLocation + 'QN' + @FileName
PRINT @Command -- copy \\FILELOCATION\DATE_FILENAME.csv \\FILELOCATION\Archive\QNDATE_FILENAME.csv
EXEC xp_cmdshell @Command
SET @Command =
@UtilitiesLocation + 'sed --text -e '
+ '"' + @SedCommand + '" '
+ @ArchiveLocation + 'QN' + @FileName
+ ' > '
+ @FileLocation + @FileName
PRINT @Command -- \\NETWORKLOCATION\UTILITIES\sed --text -e "s/£//g;s/\.//g;s/\`//g;s/\"\([A-Za-z- ']*\),\([A-Za-z- ']*\)\"/\1|\2/g;s/\"\([0-9.-]*\),\([0-9.-]*\)" \\FILELOCATION\Archive\QNDATE_FILENAME.csv > \\FILELOCATION\DATE_FILENAME.csv
EXEC xp_cmdshell @Command
Is anyone able to help please? I've tried using 3 backslashes, ^ as the escape character, putting the backtick in double quotes but can't get anything to work.