197

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
Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Alucard
  • 16,628
  • 7
  • 24
  • 23
  • 20
    Copy-paste into the browser's address bar or another text field. Quick'n'dirty but works for small amounts of data. – ignis Feb 20 '13 at 14:58

21 Answers21

353
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.

Elijah Lynn
  • 12,272
  • 10
  • 61
  • 91
Tyler McHenry
  • 74,820
  • 18
  • 121
  • 166
  • 15
    How do you write this output to the same or another file? – JobHunter69 Sep 30 '16 at 00:30
  • 3
    I think `tr` is not suitable for empty lines. What do you think? - - I think `sed` is the best option like described here http://stackoverflow.com/q/16414410/54964 – Léo Léopold Hertz 준영 Nov 05 '16 at 13:23
  • 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). – plugwash Feb 29 '20 at 12:51
  • 2
    To write the output to the same file, use a subshell `echo -n $(tr -d "\n" < yourfile.txt) > yourfile.txt` – admirableadmin Aug 05 '20 at 08:07
  • @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 – raddevus Nov 16 '21 at 20:58
23
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 :)

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
14
perl -p -i -e 's/\R//g;' filename

Must do the job.

ZyX
  • 52,536
  • 7
  • 114
  • 135
12

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!

Nagev
  • 10,835
  • 4
  • 58
  • 69
  • That's a [useless `cat.`](https://stackoverflow.com/questions/11710552/useless-use-of-cat) Several comment threads here already explain how to redirect the result to a new file. – tripleee Jan 04 '21 at 12:30
  • 1
    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. – Nagev Jan 05 '21 at 12:49
  • 2
    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' file2.txt` – tripleee Jan 05 '21 at 12:56
  • 1
    We need to pay attention to not use the same filename for reading and writing, otherwise the file will be empty. – baptx Jul 01 '21 at 16:23
11
paste -sd "" file.txt
Amardeep AC9MF
  • 18,464
  • 5
  • 40
  • 50
  • 1
    On Solaris (10) `paste -sd ""` doesn't work on STDIN by default, so if you're piping to it, use: `(some command) | paste -sd "" -` – JohnGH Jul 29 '13 at 09:32
  • 1
    This does not work if you also want to remove the *final* newline. It only removes intermediate newlines. – josch Sep 01 '17 at 14:11
  • This didn't work on Mac OS, Sundeep's version did. `paste -sd'\0' -` – Trenton Sep 23 '19 at 17:31
7

You can edit the file in vim:

$ vim inputfile
:%s/\n//g
Alex Van Liew
  • 1,339
  • 9
  • 15
loganaayahee
  • 809
  • 2
  • 8
  • 13
5

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

MrE
  • 19,584
  • 12
  • 87
  • 105
4

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'

jasonleonhard
  • 12,047
  • 89
  • 66
3

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.

LeOn - Han Li
  • 9,388
  • 1
  • 65
  • 59
2

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

xargs < file.txt | tr -d ' '
marky
  • 29
  • 1
2

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
carlmund
  • 21
  • 1
2

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)

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
Byfjunarn
  • 41
  • 5
2

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.

Hastig Zusammenstellen
  • 4,286
  • 3
  • 32
  • 45
1
$ perl -0777 -pe 's/\n+//g' input >output
$ perl -0777 -pe 'tr/\n//d' input >output
Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
1

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;
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 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'. – Jonathan Leffler Sep 05 '15 at 15:41
1

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.

peak
  • 105,803
  • 17
  • 152
  • 177
1

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.

Mohammad AbuShady
  • 40,884
  • 11
  • 78
  • 89
0

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.

ShinTakezou
  • 9,432
  • 1
  • 29
  • 39
0

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.

Eduard Florinescu
  • 16,747
  • 28
  • 113
  • 179
-1

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',''))"
crizCraig
  • 8,487
  • 6
  • 54
  • 53
  • The tags suggest the OP is looking for a shell-based solution. (FWIW, I personally understand the appeal of Python for these use cases) – David J. Aug 30 '21 at 17:33
-1

fastest way I found:

  1. open vim by doing this in your commandline
  2. vim inputfile
  3. press ":" and input the following command to remove all newlines
  4. :%s/\n//g
  5. Input this to also remove spaces incase some characters were spaces :%s/ //g
  6. make sure to save by writing to the file with
  7. :w

The same format can be used to remove any other characters, you can use a website like this https://apps.timwhitlock.info/unicode/inspect to figure out what character you're missing You can also use this to figure out other characters you can't see and they have a tool as well Tool to learn of other invisible characters

Isham7920
  • 11
  • 4
  • 1
    Original question has the `sed` tag; needs a way for automating work, not to do that by hand – pierpy Dec 11 '22 at 07:07