0

I have multiple .txt files in a folder i want to remove all spaces from every line of every file how do i do so?

So like file1.txt contains

 a a a a 
 2 2 2 2

and file 2.txt contains

3 3 3 3
4 4 4 4

I want the outcome to remove spaces so like,

file1.txt after

aaaa
2222

2 Answers2

0

Check this post out. How to remove all white spaces from a given text file

You want the tr command. You can do something like cat file.txt | tr -d " \t\n\r"

And if you wanna do it for all the files in the directory, you could use a find command. Something like find . | xargs tr -d " \t\n\r

EDIT - Just noticed you wanna keep the line breaks. You can omit the newlines from the tr command

Community
  • 1
  • 1
Steve
  • 4,457
  • 12
  • 48
  • 89
  • 1
    UUOC: `cat file.txt | tr -d " \t\n\r"` -> `tr -d " \t\n\r" < file.txt`. Consider using the POSIX character class `[:blank:]`. – Ed Morton Mar 29 '17 at 14:09
  • 1
    Good point Ed. It's always better to be explicit, makes things easier to read. – Steve Mar 30 '17 at 17:48
0

Using sed

 $sed -e "s/ //g" file1.txt
aaaa
2222

You can add -i to change the file in place.

matzeri
  • 8,062
  • 2
  • 15
  • 16