I've used the sed command more than a few times, but I've never paid attention to the use of the semicolon (;) in the script. You'll recall that the generic syntax of the sed command is:
sed [options] 'script' filename
The command that brought this to my attention was the following:
sed 's/^[ \t]*//;s/[ \t]*$//' filename
I'll comment on what the actual command does in a moment, but for now, I want to focus on what caught my attention on the command - that's the use of 2 sed commands in the script. In the above command, the script is 's/^[ \t]*//;s/[ \t]*$//'
That script is comprised of 2 sed commands - the substitute command (s). Now, it's nothing new about the use of 2 sed commands in a sed script. What is not-so-common is the use of the "same" sed command in the sed script - in this case, the 's' (substitute) command is used twice. This is made possible by the use of the semicolon.
I know, you know, there's many wonderful, exotic constructs that can be composed within a sed script, thanks to the use of RegEx characters. I've seen some that seemed as though they were constructed by a mad scientist
Now, as far as an explanation of that sed command that I mentioned above, what it does is remove the presence of spaces or tabs, at the beginning and/or end of each line in a file.
Here's a little breakdown of the sed script syntax:
s/^[ \t'*//
- this removes all leading whitespace (spaces or tabs)
- ^ matches the start of a line
- [ \t]* refers to zero or more spaces or tabs
s/[ \t]*$//
- this removes all trailing whitespace
- $ anchors the pattern (i.e.[ \t]*) to the end of the line
The magic piece that makes this entire construct possible is the semicolon (;) - this allows both substitutions to be run in a single sed command; a single sed script.
Just one more something to let me know that I'll never be able to say that I know it all
@Trevor awsome post ! Love this! The semicolon is basically sed’s backstage pass andthat is how it lets multiple commands perform in one act !
"... sed's backstage pass ..." Wow! Well, let's pin one more title ribbon on you: Master of Analogies
I'd like your permission to refer to the semicolon in this manner, please!!!
As always Chetan, thank you for your continued support!!!
@Trevor you dont need my permission
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.