[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2014-01-14
11
Medium Priority
?
611 Views
Last Modified: 2014-01-18
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.
0
Comment
Question by:rleyba828
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39778899
tr -d '\r' < inputfile > outputfile
0
 
LVL 84

Accepted Solution

by:
ozo earned 1600 total points
ID: 39778906
perl -i -pe 's/\\\r\n/\n/' file
0
 
LVL 48

Expert Comment

by:Tintin
ID: 39780647
Depending on your system

dos2unix file
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 12

Assisted Solution

by:tel2
tel2 earned 240 total points
ID: 39783967
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
 
LVL 35

Assisted Solution

by:Duncan Roe
Duncan Roe earned 160 total points
ID: 39790181
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
 
LVL 12

Expert Comment

by:tel2
ID: 39790184
> 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
 
LVL 35

Expert Comment

by:Duncan Roe
ID: 39790185
Yes I assumed it originated from a DOS / Windows system so all lines would end CrLf
0
 
LVL 12

Expert Comment

by:tel2
ID: 39790187
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
 
LVL 35

Expert Comment

by:Duncan Roe
ID: 39790195
@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
 

Author Comment

by:rleyba828
ID: 39791712
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
 
LVL 12

Expert Comment

by:tel2
ID: 39791724
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

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
Six Sigma Control Plans
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question