[linux] How do I remove newlines from a text file?

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

This question is related to linux bash scripting shell sed

The answer is


perl -p -i -e 's/\R//g;' filename

Must do the job.


use

head -n 1 filename | od -c 

to figure WHAT is the offending character. then use

tr -d '\n' <filename

for LF

tr -d '\r\n' <filename

for CRLF


paste -sd "" file.txt

I would do it with awk, e.g.

awk '/[0-9]+/ { a = a $0 ";" } END { print a }' file.txt

(a disadvantage is that a is "accumulated" in memory).

EDIT

Forgot about printf! So also

awk '/[0-9]+/ { printf "%s;", $0 }' file.txt

or likely better, what it was already given in the other ans using awk.


To also remove the trailing newline at the end of the file

python -c "s=open('filename','r').read();open('filename', 'w').write(s.replace('\n',''))"

Was having the same case today, super easy in vim or nvim, you can use gJ to join lines. For your use case, just do

99gJ

this will join all your 99 lines. You can adjust the number 99 as need according to how many lines to join. If just join 1 line, then only gJ is good enough.


xargs consumes newlines as well (but adds a final trailing newline):

xargs < file.txt | tr -d ' '

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 :)


You are missing the most obvious and fast answer especially when you need to do this in GUI in order to fix some weird word-wrap.

  • Open gedit

  • Then Ctrl + H, then put in the Find textbox \n and in Replace with an empty space then fill checkbox Regular expression and voila.


Assuming you only want to keep the digits and the semicolons, the following should do the trick assuming there are no major encoding issues, though it will also remove the very last "newline":

$ tr -cd ";0-9"

You can easily modify the above to include other characters, e.g. if you want to retain decimal points, commas, etc.


If the data is in file.txt, then:

echo $(<file.txt) | tr -d ' '

The '$(<file.txt)' reads the file and gives the contents as a series of words which 'echo' then echoes with a space between them. The 'tr' command then deletes any spaces:

22791;14336;22821;34653;21491;25522;33238;

Nerd fact: use ASCII instead.

tr -d '\012' < filename.extension   

(Edited cause i didn't see the friggin' answer that had same solution, only difference was that mine had ASCII)


You can edit the file in vim:

$ vim inputfile
:%s/\n//g

Using the gedit text editor (3.18.3)

  1. Click Search
  2. Click Find and Replace...
  3. Enter \n\s into Find field
  4. Leave Replace with blank (nothing)
  5. Check Regular expression box
  6. Click the Find button

Note: this doesn't exactly address the OP's original, 7 year old problem but should help some noob linux users (like me) who find their way here from the SE's with similar "how do I get my text all on one line" questions.


$ perl -0777 -pe 's/\n+//g' input >output
$ perl -0777 -pe 'tr/\n//d' input >output

Use sed with POSIX classes

This will remove all lines containing only whitespace (spaces & tabs)

sed '/^[[:space:]]*$/d'

Just take whatever you are working with and pipe it to that

Example

cat filename | sed '/^[[:space:]]*$/d'


Using man 1 ed:

# cf. http://wiki.bash-hackers.org/doku.php?id=howto:edit-ed 
ed -s file <<< $'1,$j\n,p'  # print to stdout 
ed -s file <<< $'1,$j\nwq'  # in-place edit

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.


I usually get this usecase when I'm copying a code snippet from a file and I want to paste it into a console without adding unnecessary new lines, I ended up doing a bash alias
( i called it oneline if you are curious )

xsel -b -o | tr -d '\n' | tr -s ' ' | xsel -b -i
  • xsel -b -o reads my clipboard

  • tr -d '\n' removes new lines

  • tr -s ' ' removes recurring spaces

  • xsel -b -i pushes this back to my clipboard

after that I would paste the new contents of the clipboard into oneline in a console or whatever.


Examples related to linux

grep's at sign caught as whitespace How to prevent Google Colab from disconnecting? "E: Unable to locate package python-pip" on Ubuntu 18.04 How to upgrade Python version to 3.7? Install Qt on Ubuntu Get first line of a shell command's output Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running? Run bash command on jenkins pipeline How to uninstall an older PHP version from centOS7 How to update-alternatives to Python 3 without breaking apt?

Examples related to bash

Comparing a variable with a string python not working when redirecting from bash script Zipping a file in bash fails How do I prevent Conda from activating the base environment by default? Get first line of a shell command's output Fixing a systemd service 203/EXEC failure (no such file or directory) /bin/sh: apt-get: not found VSCode Change Default Terminal Run bash command on jenkins pipeline How to check if the docker engine and a docker container are running? How to switch Python versions in Terminal?

Examples related to scripting

What does `set -x` do? Creating an array from a text file in Bash Windows batch - concatenate multiple text files into one Raise error in a Bash script How do I assign a null value to a variable in PowerShell? Difference between ${} and $() in Bash Using a batch to copy from network drive to C: or D: drive Check if a string matches a regex in Bash script How to run a script at a certain time on Linux? How to make an "alias" for a long path?

Examples related to shell

Comparing a variable with a string python not working when redirecting from bash script Get first line of a shell command's output How to run shell script file using nodejs? Run bash command on jenkins pipeline Way to create multiline comments in Bash? How to do multiline shell script in Ansible How to check if a file exists in a shell script How to check if an environment variable exists and get its value? Curl to return http status code along with the response docker entrypoint running bash script gets "permission denied"

Examples related to sed

Retrieve last 100 lines logs How to replace multiple patterns at once with sed? Insert multiple lines into a file after specified pattern using shell script Linux bash script to extract IP address Ansible playbook shell output remove white space from the end of line in linux bash, extract string before a colon invalid command code ., despite escaping periods, using sed RE error: illegal byte sequence on Mac OS X How to use variables in a command in sed?