Need PERL or BASH script the can convert \[cr][lf] to [lf]

Hi Team,

   Just need some assistance for a query that I have in the PHP section.   -->

I just want a way to do a global search and replace of every occurence of \[cr][lf] in a text file (i.e. the backslash character + carrier return character + line feed character) and convert it to [lf]   (just the linefeed character by itself).  

I'm sure this is just a one liner in perl...just can't get my head around this.

Thanks for all the help.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tr -d '\r' < inputfile > outputfile
perl -i -pe 's/\\\r\n/\n/' file

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Depending on your system

dos2unix file
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Hi rleyba828,

    tr -d '\r' < inputfile > outputfile
Will remove all CR chars, regardless of context, and will not remove the leading "\"s.

    dos2unix file
will also not remove the leading "\"s.

    perl -i -pe 's/\\\r\n/\n/' file
should do what you have asked for.

Here's an alternative:
    sed -i 's/\\\r$//' file
which is slightly more concise, and on my system is more case that matters.
Duncan RoeSoftware DeveloperCommented:
I just tested ozo's solution and it does exactly what you asked for.
But ... you are left with a file which has a mixture of lines ending CrLf and ending Lf. Is this really what you want?
13:59:09$ cat -A t5
13:59:14$ perl -i -pe 's/\\\r\n/\n/' t5
13:59:28$ cat -A t5

Open in new window

The above assumes all lines end CrLf now. If you've already fixed all lines except those ending backslash, ozo is 100% on the money.
> But ... you are left with a file which has a mixture of lines ending CrLf and ending Lf. Is this really what you want?

True, but only if the input data contains a mixture of
and just
as your sample has, Duncan.

What's the situation, rleyba828?
Duncan RoeSoftware DeveloperCommented:
Yes I assumed it originated from a DOS / Windows system so all lines would end CrLf
I expect they will, Duncan.  I thought our question is, whether the CrLf will be preceeded by a '\', as only some of your sample input data lines are.
Duncan RoeSoftware DeveloperCommented:
@tel2 - your sed script does the same as ozo's perl script. They both do exactly what was asked for. It just seemed slightly odd to me to do a selective dos2unix  - but I was certainly assuming this was a regular Windows text file.
rleyba828Author Commented:
Hi guys....the response of ozo worked best for me.    

To answer Duncan's question,   the mix of [cr][lf] and [lf] characters is how phpmyadmin exports a mysql table where fields in the text contain line returns.   It is able to export properly such a file to a csv file which will load properly in excel.  I found that simply doing a table dump from the mysql command line doesn't accomplish this....not sure why.
Hi rleyba828,

Glad you have a solution, but when you say the response of ozo worked best for you, did you try my sed solution?  It should do exactly the same as ozo's Perl solution, but faster, as I indicated.  You're running UNIX/Linux, right?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.