Search and Delete pattern with Control M

Hi
I have data file like below:

NET_SALES^M  ^M---- new fields for  --  ^M  ,YABA_FLAG^M  ,DOO_FLAG^M

My requirement is to search for atleast 2 -- and remove all the words till first ^M is encountered.
In above case, output should be like

NET_SALES^M  ^M ,YABA_FLAG^M  ,DOO_FLAG^M

TIA
Nitin

sed 's/--[^\r]*\r//g' file

Andrew

1 Like

Hi Andrew

Thanks for quick reply.
However I am not getting proper result

echo "NET_SALES^M  ^M---- new fields for  --  ^M  ,YABA_FLAG^M  ,DOO_FLAG^M"|sed 's/--[^\r]*\r//g'

Output:

  ,DOO_FLAGG

Expect Output:

NET_SALES^M  ^M^M  ,YABA_FLAG^M  ,DOO_FLAG^M

---------- Post updated at 04:24 PM ---------- Previous update was at 04:21 PM ----------

Surprisingly , it worked like charm on data file.
Thanks a lot.:b:

The problem is that your output terminal is translating the literal ^M as carriage returns and the line is being overwritten. Try sending the output to a file and reading it in an editor which will show the carriage returns, or try (for the sake of testing but only for testing) adding

 | sed 's/\r/^M/g'

to the above line. Or even

| cat -vet

Andrew

^M is usually a leftover from writing a file in a windows text editor, then copying/writing it to UNIX.
If you are processing the data for a file that gets loaded into another application or a database, you may not want ^M characters in the final file product.