Sed Question 
Author Message
 Sed Question

Hi,

Please forgive me if this is the wrong audience for this question.

I am trying to understand exactly what this line does?

domain=`domainname | sed 's/\([^\.]*\)\..*/\1/'`

I am trying to understand what each part does in detail.

Many thanks for any help.

Regards

Michael.



Fri, 25 Jan 2002 03:00:00 GMT  
 Sed Question

Quote:

> Hi,

> Please forgive me if this is the wrong audience for this question.

> I am trying to understand exactly what this line does?

> domain=`domainname | sed 's/\([^\.]*\)\..*/\1/'`

> I am trying to understand what each part does in detail.

domain=

sets an environment variable named domain to the contents of the remainder of
the line.

the back quotes (`) cause the shell to run a command line which is delimited
by the quotes, substituting the standard output of the command.

domainname must be a command which produces something on standard output

the vertical bar is a "pipe" which sends the standard output of "domainname"
to the standard input of "sed"

sed is the stream editor

the single-quoted part is an argument to sed, quoted to prevent the shell from
interpreting what is between the single quotes

s is a substitute command, which uses to delimited sections; the input
matching the pattern in the first section is replaced by the contents
represented by the second section

the forward slashes are the section delimiters

the backslash-escaped parentheses are used to group what is contained between
them, for later use by numbered reference

the square brackets enclose a character class

the caret, when it appears immediately after the opening square bracket,
negates the remainder of the class

the backslash-escaped period represents a single period (if not escaped, a
period represents any character)

the asterisk means zero or more occurrences of what precedes it (the character
class)

after the group, there's another backslash-escaped period

which is followed by an unescaped period representing any character

and the asterisk after that refers to zero or more occurrences of the "any
character"

next is another slash acting as a delimiter

the replacement section consists solely of a backslash-escaped number 1, which
refers to the 1st backslash-escaped parenthesized group in the pattern section

the final slash ends the sed substitute command

the single quote and backquote end the quoting of the sed command and the
shell command substitution, respectively

The end result is that if "domainname" spits out something like "foo.bar.gov",
the environment variable "domain" will be set to the part before the first
period, in this case "foo". If the output from "domainname" begins with a
period, "domain" will be empty. If the output from "domainname" doesn't
contain any period, the pattern is not matched, and there is no substitution;
"domain" is set to whatever "domainname" produces.

If there is a directory named "s" containing files with weird names, one could
get unexpected results. Placing the sed option "-e" before the substitute
command script would prevent that.



Fri, 25 Jan 2002 03:00:00 GMT  
 Sed Question

Quote:


> > domain=`domainname | sed 's/\([^\.]*\)\..*/\1/'`
[...]

> If there is a directory named "s" containing files with weird names, one could
> get unexpected results. Placing the sed option "-e" before the substitute
> command script would prevent that.

For clarification,

sed 's/foo/bar/g'

might be expected to replace all occurrences of "foo" with "bar", globally.
However, if there is a directory s with a subdirectory foo, and a
sub-subdirectory bar containing a file "g", sed may (depending on what other
flags and options are specified) try to read the file s/foo/bar/g as input.

sed [...] -e 's/foo/bar/g' [...]

won't have that problem, as the -e flag specifies that the following argument
is a script.



Sat, 26 Jan 2002 03:00:00 GMT  
 Sed Question


% Please forgive me if this is the wrong audience for this question.

It is. Nobody here would  understand this.

% I am trying to understand exactly what this line does?
%
% domain=`domainname | sed 's/\([^\.]*\)\..*/\1/'`

This runs domainname command and pipes its output to sed.
sed takes each row and strips off everything from the first
dot to the end. Finally, the result of this is assigned to
the shell variable domain.

In sh and its derivative shells, when you enclose a command in
back-ticks, the standard output of that command is effectively
inserted into the command line, so if you have this:
 domain=`domainname`
and your NIS domain name is sun.com., this is the same as
 domain=sun.com.

sed is an editor which reads from its standard input, writes to its
standard output, and executes one or more editor commands along the
way. In this case, it's executing the s (for substitute) command.
The s (for substitute) command replaces a regular expression with
a string. The regular expression and the string are delimited by
whatever the character is that follows the s (by convention, people
use /, but it could be any character.

In this case, the regular expression is
 \([^\.]*\)\..*
the \( and \) delimit something that can be brought back in the substitution
string as \1. The substitution string in this case _is_
 \1
which means we're replacing the first string that matches the regular
expression with the part of that that matches [^.].

 [^.]* matches 0 or more things that aren't .
 \.    matches .
 .*    matches anything

so, this regular expression matches 0 or more things that aren't a dot,
and puts that into the replacement parameter 1, then it matches the dot
and anything at all that follows the dot. It replaces all that with
the replacement parameter. It would change
 sun.com.
to
 sun

and it would assign that to domain.
--

Patrick TJ McPhee
East York  Canada



Sat, 26 Jan 2002 03:00:00 GMT  
 Sed Question


   >Hi,
   >Please forgive me if this is the wrong audience for this question.
   >I am trying to understand exactly what this line does?
   >domain=`domainname | sed 's/\([^\.]*\)\..*/\1/'`
   >I am trying to understand what each part does in detail.
   >Many thanks for any help.
It does pretty much the same thing as
domain=`domainname | awk -F. '{print $1}'`
Here's how:
[^\.] matches any character except '.' (the \ might not be needed inside [ ])
[^\.]* matches any number of consecutive non '.' characters.
The replacement string \1 is replaced by the first string that matches the
expression inside \(  \).

Net-Tamer V 1.08X - Test Drive



Sat, 26 Jan 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. How to do this.. (SED question)

2. a sed question

3. SED question

4. Urgent VI/SED question

5. This might be a sed question

6. sed question

7. A sed question

8. awk vs. sed question

9. A sed question

10. sed question: unreliable "/^$/d" ?

11. Newbie awk (sed??) question, regular expressions

12. Awk/Sed Filehandler question

 

 
Powered by phpBB® Forum Software