Results 1 to 5 of 5
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 04-25-2007 #1
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!
- 04-25-2007 #2
Here is a very simple 'standard input to standard output' version:
Use:Code:#!/usr/bin/perl while (<>) { chomp; print; }
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:[name of perl program] <input_file >output_file
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...Code:#!/usr/bin/perl while (<>) { chomp; printf("%s ", $_); }
- 04-25-2007 #3
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?
- 04-25-2007 #4
Here's an awk way to do it.
First example is with no spaces, second example puts spaces between each concatenated line.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
But if you have carriage returns in the mix as well, that may be a different issue. (You might have to strip them out.)
- 04-26-2007 #5
This will translate newlines into spaces:
Code:tr "\n" " " <input_file >output_file


Reply With Quote
