How would I remove ALL whitespaces in a given file in Linux?
4 Answers
Depending on your definition of whitespace, something like:
tr -d ' \t\n\r\f' <inputFile >outputFile
would do the trick.
7 Comments
Stephen P
+1 tr is "cheaper" than sed and you avoid invoking two programs
camh
GNU tr has the class
[:space:] for "all horizontal or vertical whitespace" (I assume "or" should be "and"). So tr -d '[:space:]' should do the trick too.tchrist
I don't thing that
tr works correctly on UTF-8 data, nor even ISO-8859-1, but only on mere ASCII.Darron
@tchrist: Some versions of tr will handle other character sets. In non-ASCII cases the use of [:space:] is probably advised.
giannisapi
this answer if am not mistaken would also delete new line character which is not the objective here. tr -d ' \t\r\f' should instead be used which does not remove the new line character
|
sed 's/\s//g' input.txt | tr -d '\n'
1 Comment
so.very.tired
Can you please explain why
\s in 's/\s//g' means whitespace? Thanks.