LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   sed: unterminated address regex (https://www.linuxquestions.org/questions/programming-9/sed-unterminated-address-regex-4175521507/)

slugman 10-08-2014 11:38 PM

sed: unterminated address regex
 
I have a simple problem.

I basically want a regex that will exclude everything after the last dot in my hostname:

V677.Centauri.net

to

V677

which I was going to use the following:

Code:

/\.[^]*$/
So I was going to use sed to do something like this:

Code:

sed '/\.[^]*$/p' /etc/HOSTNAME
which gives me the following output:

Code:

sed: -e expression #1, char 9: unterminated address regex
Not sure whats going on.

How do I terminate the address? Or what else should I do to allow the command to run?

pan64 10-09-2014 01:20 AM

the ^ has special meaning inside [] that is not used properly. From the other hand what was the goal of it: [^]?
what is that p at the end of the regexp?

slugman 10-09-2014 01:30 AM

Thats a sed operator. There are two associated w/ regex's:
- p: print
- d: delete

.. and I believe s which subtitutes.

I could use a perl one liner which I will most likely do, but I would also like to get this down because I know sed is simpler and ready to employ in certain cases/ stripped down systems.

Quote:

I basically want a regex that will exclude everything after the last dot in my hostname:

V677.Centauri.net

to

V677
The goal is to auto update the hostname of the system via hostname command. In slackware network configuration is modified in rc.inet1.conf. I have modified my rc.inet1.conf file to pull modified /etc/hosts and push it to /etc/HOSTNAME. Basically I was going to create another if statement to set the hostname if /etc/hosts is different than the current systems hostname.

pan64 10-09-2014 01:53 AM

sed 's/\..*//' will do that I think. Sed automatically prints, so p is not required....

ntubski 10-09-2014 12:56 PM

Quote:

Originally Posted by slugman (Post 5251167)
I was going to use the following:

Code:

/\.[^]*$/

You probably meant
Code:

/\.[^.]*$/
What you put originally is not a valid perl regex either, I think.

rknichols 10-09-2014 01:07 PM

Quote:

Originally Posted by pan64 (Post 5251198)
the ^ has special meaning inside [] that is not used properly. From the other hand what was the goal of it: [^]?

The other problem with that is that the ] character is taken literally and does not terminate the bracket expression. The ^ character negates the match for the list that follows. A ] character that is first in the list is taken literally (the only way to include a ] character in the list).

There is no way to write a bracket expression with an empty list (Why would anyone want to do that?), nor can a bracket expression contain just a single literal ^ character (must be placed anywhere but first in the list).

And as pan64 asked, what was the intent of that "[^]*" anyway? I read that as intended to mean, "any number of characters that are not nothing," better expressed as ".*".

slugman 10-10-2014 01:16 AM

You know, I apologize. It was late, and when I read your reply pan, I did not see that you were asking the purpose of the [^] expression.

Quite honestly, I have a basic understanding of regular expressions. I tested the expression at the following site:

http://www.regexr.com/

.. and it gave me the result I was looking for. So, to answer your question folks, I couldn't answer other than that is what made the expression give me the results I wanted.

I'll test this momentarily.

pan64 10-10-2014 01:54 AM

that site cannot evaluate properly this expression: [^] (which is syntactically incorrect, but that site accepts)
http://www.myezapp.com/apps/dev/regexp/show.ws

ntubski 10-10-2014 11:40 AM

Quote:

Originally Posted by slugman (Post 5251733)
I tested the expression at the following site:

http://www.regexr.com/

It says, under Reference>Character Classes>match any [\s\S]:
Quote:

A character set that can be used to match any character, including line breaks.

An alternative is [^], but it is not supported in all browsers.

slugman 01-21-2015 12:55 AM

Pan, I was reading over this again and I have to say, the latter solution is simple and I like it! Thanks for sharing :)


All times are GMT -5. The time now is 12:15 AM.