Link to home
Start Free TrialLog in
Avatar of jewbama
jewbama

asked on

remove carriage returns from a text file

What's a fast way to remove carriage returns from a text file so a large file can become one really long line with a space between everyword?

I tried using "J" in VI over and over but I would always get "Internal error: vredraw".  Any ideas?

Thanks.
Avatar of ozo
ozo
Flag of United States of America image

perl -i -pe 'BEGIN{$\=" "}chomp;' file
Avatar of rbr
rbr

cat file | awk -v ORS= "{ print $1} > newfile
tr < file '
' ' ' > newfile
while read rec
do
   echo $rec"\c" >>outfile
done < inpfile
Found one little problem: Each new record needs to be separated by a space as well:

while read rec
do
   echo $rec" \c" >>outfile
done < inpfile

Dgrimes: the problem with this is that the shell 'read' builtin does more than just read the line into a buffer.  Rather it does all kinds of whitespace mangling and also deals with line continuation (\) processing.  The whilespace mangling may actually be a good thing for this particular application, but the line continuation probably is not.  In some shells you can get around this with 'read -r'.
Right, you would want to read with the -r switch. Also, not all shells support this feature. I was using ksh.

Thanks
jewbama: Did any of the comments solve your problem. If yes post to whom you give the points.
ASKER CERTIFIED SOLUTION
Avatar of mdgreen7
mdgreen7

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hmm, given the suggestions above, these both seem like monumentally bad ways of doing it.
How does this accomplish the intended goal. However, if jewbama doesn't respond, then guess who will get the points!
Oops.  My bad.  I misread the question.
I stated quick ways to convert from cr/lf to lf....

It was my first reply - I got a little anxious :)  Jeez - "monumentally bad".
Monumentally bad because it not only doesn't solve the problem, but requires a lot more effort than other suggestions already listed above that solve the problem 100%.

Don't feel bad about it, just read the comment before posting next time...