How do I remove newlines from a text file?
Solution 1
tr --delete '\n' < yourfile.txt
tr -d '\n' < yourfile.txt
Edit:
If none of the commands posted here are working, then you have something other than a newline separating your fields. Possibly you have DOS/Windows line endings in the file (although I would expect the Perl solutions to work even in that case)?
Try:
tr -d "\n\r" < yourfile.txt
If that doesn't work then you're going to have to inspect your file more closely (e.g. in a hex editor) to find out what characters are actually in there that you want to remove.
Solution 2
perl -p -i -e 's/\R//g;' filename
Must do the job.
Solution 3
tr -d '\n' < file.txt
Or
awk '{ printf "%s", $0 }' file.txt
Or
sed ':a;N;$!ba;s/\n//g' file.txt
This page here has a bunch of other methods to remove newlines.
edited to remove feline abuse :)
Solution 4
paste -sd "" file.txt
Solution 5
Expanding on a previous answer, this removes all new lines and saves the result to a new file (thanks to @tripleee):
tr -d '\n' < yourfile.txt > yourfile2.txt
Which is better than a "useless cat" (see comments):
cat file.txt | tr -d '\n' > file2.txt
Also useful for getting rid of new lines at the end of the file, e.g. created by using echo blah > file.txt
.
Note that the destination filename is different, important, otherwise you'll wipe out the original content!
Related videos on Youtube
Comments
-
Alucard almost 3 years
I have the following data, and I need to put it all into one line.
I have this:
22791 ; 14336 ; 22821 ; 34653 ; 21491 ; 25522 ; 33238 ;
I need this:
22791;14336;22821;34653;21491;25522;33238;
EDIT
None of these commands is working perfectly.
Most of them let the data look like this:
22791 ;14336 ;22821 ;34653 ;21491 ;25522
-
ignis about 11 yearsCopy-paste into the browser's address bar or another text field. Quick'n'dirty but works for small amounts of data.
-
-
JohnGH almost 11 yearsOn Solaris (10)
paste -sd ""
doesn't work on STDIN by default, so if you're piping to it, use:(some command) | paste -sd "" -
-
Jonathan Leffler over 8 yearsThis works as long as the input is not too big and as long as you're using Bash (tagged for Bash, so that's OK). Were I writing the answer now, it would be
tr -d '\n' < file.txt
, which is what the accepted answer does (and I'm surprised I didn't write it at the time). This was probably just written to show 'yet another way to do it'. -
JobHunter69 over 7 yearsHow do you write this output to the same or another file?
-
Michal aka Miki over 7 yearsI think
tr
is not suitable for empty lines. What do you think? - - I thinksed
is the best option like described here stackoverflow.com/q/16414410/54964 -
josch over 6 yearsThis does not work if you also want to remove the final newline. It only removes intermediate newlines.
-
Remi Mélisson over 5 yearsand to replace with with spaces :
:%s/\n/ /g
-
BradChesney79 almost 5 yearssed -i '/^[[:space:]]*$/d' filename ...for in place editing; answer as above will output to the screen
-
Trenton over 4 yearsThis didn't work on Mac OS, Sundeep's version did.
paste -sd'\0' -
-
Trenton over 4 yearsGolfs to
perl -pie 's/\R//g' filename
-
plugwash about 4 yearsTo route the output to another file just use output redirection. To route it to the same file pipe it to "sponge" (available in the "moreutils" package on Debian base systems).
-
admirableadmin almost 4 yearsTo write the output to the same file, use a subshell
echo -n $(tr -d "\n" < yourfile.txt) > yourfile.txt
-
tripleee over 3 yearsThat's a useless
cat.
Several comment threads here already explain how to redirect the result to a new file. -
Nagev over 3 yearsThanks for pointing that out, I just saw a comment with
echo -n $(tr -d "\n" < yourfile.txt) > yourfile.txt
but hadn't seen it before. -
tripleee over 3 yearsThat's even more convoluted, with the well-known portability issues around
echo -n
. The simple obvious idiomatic way to do that istr -d '\n' <file.txt >file2.txt
-
baptx almost 3 yearsWe need to pay attention to not use the same filename for reading and writing, otherwise the file will be empty.
-
David J. over 2 yearsThe tags suggest the OP is looking for a shell-based solution. (FWIW, I personally understand the appeal of Python for these use cases)
-
raddevus over 2 years@andpei Thanks very much for that and here is a simple shell script that takes the file name ($1) and does the same work: echo -n $(tr -d "\n" < $1) > $1 Put it in a removeNL.sh file & chmod 777 and run it $ ./removeNL yourfile.txt
-
glenn jackman over 2 yearsNo, the
-i
takes an argument, so it can't be smushed up to-e
:perl -i -pe ...