Skip to main content
Data Science Wizardry Blog by Attila Vajda

Scikit open-source learning.

tr -s '[:space:]'

Wow, I know -s now! 🦏

-s represents something different after tr than it does when it is used with seq. tr substitutes or deletes characters from the input

$ "abc" | tr -s
-bash: abc: command not found
usage: tr [-Ccsu] string1 string2
       tr [-Ccu] -d string1
       tr [-Ccu] -s string1
       tr [-Ccu] -ds string1 string2
$ "abc" | tr -s "a" "d"
-bash: abc: command not found
$ echo "abc" | tr -s "a" "d"
dbc

-s squeezes all occurrences of a character into a single character after all substitution and deletion is completed:

$ echo "aaaaaaabc" | tr -s "a" "d"
dbc
$ echo "aaaaaaabc" | tr "a" "d"
dddddddbc

What is the meaning of our original one-liner?

tr -s '[:space:]' '\n' < filename.txt

This code replaces every space with a newline and squeezes all occurrences of \n into a single one. I don't see how, but I might be able to construct a simpler form of the problem.

$ echo "a b c" | tr '[:space:]' '\n'
a
b
c
$ echo "a b c" | tr -s '[:space:]' '\n'
a
b
c

-v inverts the match, it selects lines (lines? grep selects lines!) Comprehending this line twists my mind, and proves to be excellent complementation of an effective brain workout routine. A space-workout in the confines of your mind.

What on earth is meant by '^$'. It might be the pattern that is searched for, in the lines of filename.txt! In regex ^ denotes the beginning of the line, while $ signifies its end. Before finding this connection, I thought $ had something to do with a variable and ^ was negation, perhaps an unvariable. unvariability denotes immutability, or simply, an unyielding state of inertia, a solid foundation. Of course, all of this is speculation. What is permanent in existence, other than the mystical $\lnot\Delta\infty$?

$ grep -oE 'l+' <<< 'hello world'
ll
l
$ grep -v 'l+' <<< 'lll ll l'
lll ll l