dishanm
dishanm

Reputation: 120

sed command not working only for 1 scenario

I have this sed command line in my UNIX shell script to append the variable value to the existing line in a file.

sed "s/.*/&$OptionalCols/" header_scrubbed_vendor_list.dat \
    >>final_header_scrubbed_vendor_list_${TODAY}.dat

where the OptionalCols value is fetched from below

OptionalCols=`head -1 ${INDIR}Direct_Target_List_*.dat |cut -d'|' -f 19-48`

The appending task is working perfectly fine for all. but it is not working only for below one.

And 1st line in Direct_Target_List_*.dat file is

CID|FirstName|MiddleName|LastName|Suffix|Specialty|AddressLine1|AddressLine2|City|State|ZipCode|Phone|Fax|MENumber|NPINumber|EmailAddress|StateLicenseNumber|GSKBrandPAR|ProfessionalDesignatio|Segment Name |GLP-User Flag|Tanzeum Action Group|Tanzeum See-status|Total_Access_042014to032015|SF_Access_042014to032015|SF_access_012015to032015|Received Samples|Tanzeum Engagers|Tanzeum Writers|NRx_201410to201503|TRx_201410to201503|DM_BRC_dont_use|Email_DMD_Match|CT_Mayo_Match|CT_FPN_Match|CT_IMN_Match|CT_Cons_Match|T/C|Group|Email_Segments|DM_Segments|Alerts_Segments

The error am getting is

sed: command garbled: s/.*/&ProfessionalDesignatio|Segment Name |GLP-User Flag|Tanzeum Action Group|Tanzeum See-status|Total_Access_042014to032015|SF_Access_042014to032015|SF_access_012015to032015|Received Samples|Tanzeum Engagers|Tanzeum Writers|NRx_201410to201503|TRx_201410to201503|DM_BRC_dont_use|Email_DMD_Match|CT_Mayo_Match|CT_FPN_Match|CT_IMN_Match|CT_Cons_Match|T/C|Group|Email_Segments|DM_Segments|Alerts_Segments/

Do anybody have idea why this is happenning?

Upvotes: 0

Views: 58

Answers (2)

Toby Speight
Toby Speight

Reputation: 30924

You need to use a different separator for the s command. The / in the replacement is interpreted as the end of the command, and the following C|Group|Email_Segments|... makes no sense to the interpreter.

Either delimit your substitution with something you know won't be in the variable - e.g. ! here:

sed -e "s!\$!$OptionalCols!" infile >>outfile

or massage the variable to replace / with \/ when substituting:

sed -e "s/\$/${OptionalCols//\//\\\/}/" infile >>outfile

or, if that's hard to read, a combination of both:

sed -e "s!\$!${OptionalCols//!/\\!}!" infile >>outfile

(I've also changed s/.*/&.../ to s/$/.../ to reduce the workload).

Upvotes: 3

Chad Nouis
Chad Nouis

Reputation: 7051

The slash in T/C is likely being considered the delimiter. It's kind of tricky to see, so here is an extract:

...CT_Cons_Match|T/C|Group...

You can have sed use a different delimiter. Choose one that's guaranteed not to be in the input. For example:

sed -e 's#regex#replacement#'

Upvotes: 1

Related Questions