Categories
Linux regex

Sed 101

SED aka stream editor is a utility app in linux / bsd systems (among others) which allows you to parse text in files based on simple regexp and change them based on its own set of rules.

simple syntax for parsing and replacing text in a file called test.txt that contains the text I love cats and hate dogs n number of times and replace with I love sloths and hate ducks

sed -e 's/cats/sloths/' test.txt
sed -e 's/dogs/ducks/' test.txt

but to make it shorter you can actually add the options to same command.

sed -e 's/cats/sloths/g' -e 's/dogs/ducks/g' test.txt

the problem with above? it will only do so once per line, so if oyu have multiple instances it wont work. so you need to “globalize” this search/replace. note the added g after the words

sed -e 's/dogs/ducks/g' test.txt

what if you only want the replacements to happen on lines 2 – 7?

sed -e '2,7s/dogs/ducks/g' test.txt

How about if you want to only replace on lines begining with the word link? (replace link with any other word you please) and ending in the word pie (again replace pie with whatever)

sed -e '/^link/,/^pie/s/dogs/ducks/g' test.txt

what if your words have the separator / already in them? say in a path like /home/kingpin ? well you can replace the separator with anything arbitrary you choose at the beginning after the s and it will change the separator all throughout the sed command.

sed -e 's;dogs;ducks;g' test.txt

or with / in the words

sed -e 's;/home/kingpin;/home/notkingpin;g' test.txt

 

Well this is it for now… theres SO much more out there, tye man sed in your CLI to find out more or just google 🙂

Categories
PHP regex

php Filter var & common regex patterns

People … quit messing about with ereg_replace / preg_replace / eregi or other things you know nothing of… use the php filter_var. DO IT.
Commonly found bullshit on the interwebs :

function isValidEmail($email){
    return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)@[a-z0-9-]+(\.[a-z0-9-]+)(\.[a-z]{2,3})$",$email);
}

eregi is deprecated and silly, the above WILL miss some of the weirder or longer email addresses like school/company addresses. make your life simpler :

function isValidEmail($email)    {
    return filter_var($email, FILTER_VALIDATE_EMAIL);
}

while not the most perfect thing, its easier to remember AND add to your code.

More info regarding this : PHP manual Filter Var

Some examples :

/*** validate bools ***/
echo filter_var("true", FILTER_VALIDATE_BOOLEAN);

if(filter_var($url, FILTER_VALIDATE_URL) === FALSE)
        {
        /*** no ***/
        echo "Sorry, $url is not valid!";
        }
else
        {
        /*** yes ***/
        echo "The URL, $url is valid!
";
}

Categories
General

New blog! yay! etc.

So I’m starting a blog so i can keep track of tips. tricks and shortcuts I find/come up with or utilize during my daily work as an unappreciated IT drone. yes there’s hundreds of thousands of other blogs. but I have web space + bandwidth etc etc. so yeah.
This blog is hosted on quad quad core 24 GB ram gigabit connect machine using nginx, php-fpm, mysql 5.5. overkill? hell no. maybe I will get famous and get 10 million hits a day :D…… hey I can dream right?