Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Write an article for LinuxForums Today! Win Great Prizes!
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > Your Distro > Mandriva Linux Help > Chomp alternatives? Remove newline/carriage-returns

Forgot Password?
 Mandriva Linux Help   For help and discussion about Mandriva (formally Mandrake) Linux.

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds
Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 04-25-2007   #1 (permalink)
Just Joined!
 
lahonda_99's Avatar
 
Join Date: Nov 2006
Posts: 15
Chomp alternatives? Remove newline/carriage-returns

Is there a command that I can use through the command prompt to remove all newlines from a file? I would like the text in a text file to be in one straight line.

Is there a perl script that I could use that would accomplish the same thing? I don't know how to pass a file to chomp, so I would appreciate it if the code could be shown to me.

Any other ways that you can think of? The KATE editor does not remove newlines, unfortunately.

Thank you for reading!
lahonda_99 is offline  



Reply With Quote
Old 04-25-2007   #2 (permalink)
Banned
 
CodeRoot's Avatar
 
Join Date: Sep 2005
Posts: 567
Here is a very simple 'standard input to standard output' version:

Code:
#!/usr/bin/perl
while (<>) { chomp; print; }
Use:

Code:
[name of perl program] <input_file >output_file
This will, of course, place the first character of each line directly after the last character of the previous line. If you want the newline to be replaced with a space, you could do something like this:

Code:
#!/usr/bin/perl
 while (<>) { chomp; printf("%s ", $_); }
This will, of course, add an [unnecessary] space at the end of the one-line output -- which, if you don't care, that is great -- but, if you do, then more code will be required...
__________________
* Linux 101 * Linux 102 * Computer 101 * Software 101 *
-
-
Remember: amateurs built the ark... professionals built the Titanic
CodeRoot is offline   Reply With Quote
Old 04-25-2007   #3 (permalink)
Just Joined!
 
lahonda_99's Avatar
 
Join Date: Nov 2006
Posts: 15
I greatly appreciate your help! I had found a bash command in these forums months ago to do the same thing, but I searched for an hour and couldn't find it again. I'm okay with using PERL, but for curiousity's sake would you know of the command that I have described?
lahonda_99 is offline   Reply With Quote
Old 04-25-2007   #4 (permalink)
Linux Guru
 
anomie's Avatar
 
Join Date: Mar 2005
Location: Texas
Posts: 1,697
Here's an awk way to do it.

Code:
[hector@troy ~]$ cat some-file 
this
should
all
be
on
one
line

[hector@troy ~]$ awk '{ str1=str1 $0 }END{ print str1 }' some-file 
thisshouldallbeononeline

[hector@troy ~]$ awk '{ str1=str1 $0 " "}END{ print str1 }' some-file 
this should all be on one line
First example is with no spaces, second example puts spaces between each concatenated line.

But if you have carriage returns in the mix as well, that may be a different issue. (You might have to strip them out.)
anomie is offline   Reply With Quote
Old 04-26-2007   #5 (permalink)
Banned
 
CodeRoot's Avatar
 
Join Date: Sep 2005
Posts: 567
This will translate newlines into spaces:
Code:
tr "\n" " " <input_file >output_file
__________________
* Linux 101 * Linux 102 * Computer 101 * Software 101 *
-
-
Remember: amateurs built the ark... professionals built the Titanic
CodeRoot is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 05:07 PM.






© 2000 - - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.1