About a week ago, I learned a clever way to remove blank lines:
$ grep .
Now, I’ve just learned a new one, using tr‘s -s or --squeeze-repeats option:
$ tr -s '\n'
This command squeezes consecutive newlines into single one, that means removing the blanks lines. It’s longer in characters, but it could squeeze more than just newlines. Consider this:
$ echo 'abc123def456' | tr -s '[:digit:]' '[\n*]'
abc
def
It does two things:
- Translate digits into newlines: SET1 -> SET2
- Squeeze newlines from SET2
In other words, to look at it, it’s like the digits are delimiters to separate the text into multiple words, one per line.
Ain’t it squeezing amazingly?
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.