GithubHelp home page GithubHelp logo

Comments (4)

korhadris avatar korhadris commented on April 28, 2024

Here's a sed command to remove duplicated '.' characters:

sed 's/\.\+/./g'

This is probably faster as it will only make the replacement if there are two or more '.' characters ...

sed 's/\.\.\+/./g'

from pi-hole.

kurumushi avatar kurumushi commented on April 28, 2024

The real challenge is to see if these can be combined into one, I'm not very good at simplification.
Here's a few more useful ones:
sed -e 's/ \.//g': remove periods at the start and the middle of domains.
sed -e 's/\[. ]+$/$/g' remove domains ending in a period, and trailing white space.

from pi-hole.

korhadris avatar korhadris commented on April 28, 2024

Yeah, simplifying regexp statements to be optimized is rough. As long as I really don't require the speed I usually combine statements like this:

sed -e 's/\.\.\+/./g' -e 's/^[. ]*//' -e 's/\.* \+\.*/ /g' -e 's/[. ]\+$//'

This way it's easier for a human to look and see what's going on.
This removes duplicated . characters and replaces with a single .; removes spaces and . characters at the front of the line; removes extra spaces and . characters in the middle (leaving a single space); removes spaces and . characters at the end of the line.

I tested it with the following example:

echo "  .192.168.23.515.. ...foo...bar..com.  " | sed -e 's/\.\.\+/./g' -e 's/^[. ]*//' -e 's/\.* \+\.*/ /g' -e 's/[. ]\+$//'
192.168.23.515 foo.bar.com

I also fixed our regexp examples in this post (we both had some errors). I had an extra / at the end of my commands above (now editted); sed needs to have + escaped, or it will try to match a + character; $ in the second substitution section will put in an actual $ character; there was an extra \ before the [, which would try to match a [ character instead of starting a list of possible characters to match. I also removed the g from the end of the that can only match at the end of the line, since the global part isn't needed (doesn't break having it there though).

from pi-hole.

jacobsalmela avatar jacobsalmela commented on April 28, 2024

It looks pretty good, but it's a bit out of my skill level. Maybe someone could make a pull request and I can test it out.

from pi-hole.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.