How do I remove newlines from a text file?

350,018

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!

Share:
350,018

Related videos on Youtube

Alucard
Author by

Alucard

Federal e o !

Updated on July 02, 2021

Comments

  • Alucard
    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
      ignis about 11 years
      Copy-paste into the browser's address bar or another text field. Quick'n'dirty but works for small amounts of data.
  • JohnGH
    JohnGH almost 11 years
    On 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
    Jonathan Leffler over 8 years
    This 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
    JobHunter69 over 7 years
    How do you write this output to the same or another file?
  • Michal aka Miki
    Michal aka Miki over 7 years
    I think tr is not suitable for empty lines. What do you think? - - I think sed is the best option like described here stackoverflow.com/q/16414410/54964
  • josch
    josch over 6 years
    This does not work if you also want to remove the final newline. It only removes intermediate newlines.
  • Remi Mélisson
    Remi Mélisson over 5 years
    and to replace with with spaces : :%s/\n/ /g
  • BradChesney79
    BradChesney79 almost 5 years
    sed -i '/^[[:space:]]*$/d' filename ...for in place editing; answer as above will output to the screen
  • Trenton
    Trenton over 4 years
    This didn't work on Mac OS, Sundeep's version did. paste -sd'\0' -
  • Trenton
    Trenton over 4 years
    Golfs to perl -pie 's/\R//g' filename
  • plugwash
    plugwash about 4 years
    To 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
    admirableadmin almost 4 years
    To write the output to the same file, use a subshell echo -n $(tr -d "\n" < yourfile.txt) > yourfile.txt
  • tripleee
    tripleee over 3 years
    That's a useless cat. Several comment threads here already explain how to redirect the result to a new file.
  • Nagev
    Nagev over 3 years
    Thanks 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
    tripleee over 3 years
    That's even more convoluted, with the well-known portability issues around echo -n. The simple obvious idiomatic way to do that is tr -d '\n' <file.txt >file2.txt
  • baptx
    baptx almost 3 years
    We need to pay attention to not use the same filename for reading and writing, otherwise the file will be empty.
  • David J.
    David J. over 2 years
    The tags suggest the OP is looking for a shell-based solution. (FWIW, I personally understand the appeal of Python for these use cases)
  • raddevus
    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
    glenn jackman over 2 years
    No, the -i takes an argument, so it can't be smushed up to -e: perl -i -pe ...