Does anyone know how to use Sed to delete all blank spaces in a text file? I haven been trying to use the "d" delete command to do so, but can't seem to figure it out
What kind of "space"?
To "delete all blank spaces" can mean one of different things:
- delete all occurrences of the space character, code
- delete all horizontal space, including the horizontal tab character, "
- delete all whitespace, including newline, "
\n" and others
The right tool for the job
sed it not a requirement for some hidden reason, better use the right tool for the job.
tr has the main use in translating (hence the name "tr") a list of characters to a list of other characters. As an corner case, it can translate to the empty list of characters; The option
--delete) will delete characters that appear in the list.
The list of characters can make use of character classes in the
tr -d ' ' < input.txt > no-spaces.txt
tr -d '[:blank:]' < input.txt > no-spaces.txt
tr -d '[:space:]' < input.txt > no-spaces.txt
When insisting on
With sed, the
[:...:] syntax for character classes needs to be combined with the syntax for character sets in regexps,
[...], resulting in the somewhat confusing
sed 's/ //g' input.txt > no-spaces.txt
sed 's/[[:blank:]]//g' input.txt > no-spaces.txt
sed 's/[[:space:]]//g' input.txt > no-spaces.txt