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.   -->

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28336410.html

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.
rleyba828Asked:
Who is Participating?
 
ozoConnect With a Mentor Commented:
perl -i -pe 's/\\\r\n/\n/' file
0
 
woolmilkporcCommented:
tr -d '\r' < inputfile > outputfile
0
 
TintinCommented:
Depending on your system

dos2unix file
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
tel2Connect With a Mentor Commented:
Hi rleyba828,

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

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

ozo'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 efficient...in case that matters.
0
 
Duncan RoeConnect With a Mentor Software 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
aaa^M$
bbb\^M$
ccc^M$
ddd\^M$
eee^M$
13:59:14$ perl -i -pe 's/\\\r\n/\n/' t5
13:59:28$ cat -A t5
aaa^M$
bbb$
ccc^M$
ddd$
eee^M$
13:59:30$ 

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.
0
 
tel2Commented:
> 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
    \[CR][LF]
and just
    [CR][LF]
as your sample has, Duncan.

What's the situation, rleyba828?
0
 
Duncan RoeSoftware DeveloperCommented:
Yes I assumed it originated from a DOS / Windows system so all lines would end CrLf
0
 
tel2Commented:
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.
0
 
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.
0
 
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.
0
 
tel2Commented:
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?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.