How to delete lines from a text file?

I have the following data, and I need to put all this on one line.

I have it:

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

I need this:

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



EDIT

None of these commands work fine.

Most of them allow such data:

 22791 ;14336 ;22821 ;34653 ;21491 ;25522 
+79
linux scripting bash shell sed
Jun 28 '10 at 17:46
source share
18 answers
 tr -d '\n' < yourfile.txt 

Edit:

If none of the commands listed here work, you have something other than a new line separating your fields. Perhaps you have the final lines in DOS / Windows in the file (although I would expect Perl solutions to work even then)?

Try:

 tr -d "\n\r" < yourfile.txt 

If this does not work, you will need to check your file more carefully (for example, in a hex editor) to find out which characters are actually there that you want to delete.

+177
Jun 28 '10 at 17:51
source share
 perl -p -i -e 's/\R//g;' filename 

Required to do the work.

+8
Jun 28 '10 at 19:00
source share
 paste -sd "" file.txt 
+7
Jun 28 '10 at 17:56
source share
 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 contains a bunch of other methods for removing newlines.

edited to remove feline abuse :)

+4
Jun 28 '10 at 17:51
source share

use

 head -n 1 filename | od -c 

to understand what is a disturbing symbol. then use

 tr -d '\n' <filename 

for lf

 tr -d '\r\n' <filename 

for CRLF

+4
Dec 08 '14 at 18:58
source share

You can edit the file in vim:

 $ vim inputfile :%s/\n//g 
+4
03 Oct '15 at 5:12
source share

Nerd fact: use ASCII instead.

 tr -d '\012' < filename.extension 

(Edited reason: I did not see the friggin answer that had the same solution, only the difference was that I had ASCII)

+2
Apr 27 '17 at 19:55
source share
  $ perl -0777 -pe 's / \ n + // g' input> output 
  $ perl -0777 -pe 'tr / \ n // d' input> output 
+1
Jun 28 '10 at 17:52
source share

If the data is in a .txt file, then:

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

" $(<file.txt) " reads the file and gives the contents as a series of words, which are then echoed by echo between spaces. Then the tr command removes any spaces:

 22791;14336;22821;34653;21491;25522;33238; 
+1
Jun 28 '10 at 18:19
source share

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 
+1
Jun 28 '10 at 6:42 on
source share

xargs also consumes newlines (but adds a trailing trailing newline):

 xargs < file.txt | tr -d ' ' 
+1
Apr 17 '13 at 6:13
source share

Assuming you want to keep numbers and semicolons, the following should do the trick, assuming there are no serious encoding problems, although it will also delete the last "new line":

 $ tr -cd ";0-9" 

You can easily modify the above to include other characters, for example. if you want to keep decimal points, commas, etc.

+1
Dec 09 '15 at 5:04
source share

Using the gedit text editor (3.18.3)

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

Note: this doesn’t exactly solve the original 7 year old OP problem, but should help some noob linux users (like me) who find their way here from SE with similar questions like “how do I get text on one line”,

+1
Jan 03 '18 at 18:26
source share

If we had the same case today, super easy in vim or nvim, you can use gJ to concatenate strings. For your use case just do

 99gJ 

This will combine all your 99 lines. You can adjust the number 99 as needed depending on how many lines to join. If you simply join 1 line, then only gJ enough.

+1
Jan 26 '19 at
source share

I would do this with awk, for example.

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

(the disadvantage is that a “accumulates” in memory).

EDIT

Forgot about printf! So,

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

or, most likely, it is better that it has already been specified in another ans using awk.

0
Jun 28 '10 at 17:59
source share

I usually get this usecase when I copy a piece of code from a file and I want to paste it into the console without adding extra new lines, I ended up creating a bash alias (I called it oneline , if you're interested)

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

  • tr -d '\n' removes newlines

  • tr -s ' ' removes duplicate spaces

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

after that I paste the new contents of the clipboard into the oneline in the console or something else.

0
Oct 11 '17 at 15:43 on
source share

You are missing the most obvious and quick answer, especially when you need to do this in the GUI to fix some weird word breaks.

  • Open gedit

  • Then Ctrl + H , then enter the Find \n text box and Replace with its blank space. then fill in the Regular expression check box and voila.

0
Jul 24 '18 at 18:58
source share

Use sed with POSIX classes

This will remove all lines containing only spaces (spaces and tabs)

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

Just take what you work with and direct it to it.

example

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

0
Apr 12 '19 at 6:16
source share



All Articles