I try to make these things 24 hours, but I'm relocating to another state, and goodbyes had to be said. And packing and then thins that have to do with that and whatever. Anyways, let's talk about Regex:
I love regex. I once regexed a regex to prove a point. I don't know why but this sort of pattern matching is a puzzle I enjoy. Regex is one of the few things I like helping out with. Regex Crossword is one of my favorite things. I use them more than I should and it probably gets me in trouble. Notably there are many reasons you shouldn't use regex. As JWZ said (sort of but not really):
Some people, when confronted with a problem, think
“I know, I'll use regular expressions.” Now they have two problems.
One problem in particular is parsing xhtml. This particular stack overflow post about it is hilarious.
Another problem with regex is performance, particularly in the case of "catastrophic backtracking". That occurs, usually when you have multiple groupings, and there isn't a match. Because of the order of operations of the regex, it becomes computationally expensive.
If you want to know more about catastrophic backtracking, and preventing it with atomic grouping, regular-expressions.info has a good write up. The regex buddy tool is an excellent way to debug regexes as well.
LibrePlanet in March will have Edward Snowden as a guest.
There is an upcoming Hack the Left hackathon. Hackathon's are getting interesting as there may now be pro hackathoners
I don't have a one liner or regex of the day today, sorry.
Tuesday, February 2, 2016
Everything I learned about linux in the last, I don't know 64 hours? 1454425611
Friday, January 22, 2016
Everything I learned about linux today 1453518423
I'm going to try and do a daily, update on everything I learn about linux the day of. I don't know how long I will do this, or if it will continue after I start working a regular job again. Today I watched "Linux Sucks" the 2016 one that happened on Thursday the 21st:
January 2016
Su Mo Tu We Th Fr Sa
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
I was excited to watch it so I had been hoping the link would show in one of my feeds, and eventually it did. I knew Lunduke was going to talk about systemd, and I was pretty sure what his opinion would be, and I was write. I'm not going to tell it to you, you should watch it. If your reading this after Friday you are probably better of checking lunduke's channel, because I imagine he will have a higher quality version posted.
My primary complaint with systemd is that it feels very much at odds with the unix way. The core tenants being:
(i) Make each program do one thing well. To do a new job, build afresh rather than complicate old programs by adding new features.(ii) Expect the output of every program to become the input to another, as yet unknown, program. Don't clutter output with extraneous information. Avoid stringently columnar or binary input formats. Don't insist on interactive input.(iii) Design and build software, even operating systems, to be tried early, ideally within weeks. Don't hesitate to throw away the clumsy parts and rebuild them.(iv) Use tools in preference to unskilled help to lighten a programming task, even if you have to detour to build the tools and expect to throw some of them out after you've finished using them.Or summed down to a one liner, perl style:Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface.Systemd is a monolithic program that does many things. It also stores logs in binary, not plain text. So no I don't prefer it, but I can't say I really love what Mac did with launchd. And in fairness systemd improves boot times in comparison to init.
I have other gripes with it as well, but I haven't decided if they're unfounded or not. Lennart in general does not particularly ascribe to the linux or posix rules.
So that said does this make him a bad guy, or systemd bad. It doesn't. I do really want devuan to be successful and I would really love to see a yum or dnf package managed system take it's lead. But systemd is assuredly great at speeding up boot times, and it may be great at the many other things it does as well.
Clearly that's just a rant I had in my head for a long time, not something I learned today but whatever.
Your daily one liner or regex:
I started with this, it was just because I was mad because I left the slash off a regex and wanted to prove to the smart ass that pointed it out, that I know my shit and stfu.
A vim regex:
%s/s\/potatoe\/potato/s\/potatoe\/potato\//g
then /u/EnTantoEnCuanto
:%s|toe|to/|g
And then my friend from IRC and /u/xurdm:
:%s|$|/|g
Thursday, April 7, 2011
Last.fm I must leave thee.
I really like last.fm, I like what it keeps track of. That's what I like about it. I don't like it's algo, the math they use to determine what songs you like. Yeah I listened to ICP in the 5th grade...no I don't need every single song I listen to today to be from Psychopathic Records. At one point on my "Reccomended" I could only get comedy because I liked a few comedians tracks, and a lot of nerdcore. ::facepalm::
Do not need.
So I don't care, they keep track of what I'm listening to, that's what I use the service for, and for the create your own station by tag or whatever the option is called. This station only mildly has to do with that tag, which for better or worse is usually better than the radio....wait, not if you live near/in Nashville. We have 91.1...at least for a little while. Losing that station is going to suck.
I digress. So I went to listen on my phone today, to Last.fm and sure enough there's a pay wall for mobile devices now. I did a double take...seriously. I started poking around there website to see what the deal was, and wonderfully enough there is an export in XML format, for your loved tracks.
I took the chance, and indeed Paul Simon was right. I hopped on the Bus Gus. So I grabbed that file. I decided to make it more readable for non geeks, so I did this to put a more readable version in a file called titles:
cat bsdpunk_lovedtracks.xspf |perl -pe "s/.*<title>(.*)<\/title><creator>(.*)<\/creator>.*/Song: \1 Artist: \2/gi"> titles
Yeah yeah I know, don't parse XML with regex, it will anger Chthulu. I still don't know what you are supposed to use to parse it without regex, and even if I did I probably wouldn't use it for this, it took me less than a few minutes to write.
Also does that snippet of code help...no not really. I mean that's to give you something that is human readable to other people, you know...normies, so you have a:
Song: Twenty-Three Artist: MC Lars Song: Listen Close Artist: MC Frontalot Song: In Arrears Artist: MC Frontalot Song: Why So Nerdcore Artist: MC GoblinMajick
What will I ultimately do with this xml file...obviously not this, unless I just wanted to paste it up somewhere like a web page.
I think I actually want to build my own algo. In some way it can be run independently, of pay services that actually provide the music, like last.fm.
How will I do this...I have no idea. It's 4 in the morning, and I have to be at work in a few hours, I just couldn't sleep.
Until then I will use Pandora on my phone( evo shift ). My very awesome phone I am very much in love with(not sarcasm).
Wednesday, March 9, 2011
Favorite way to regex a file using perl
Sometimes you want to do some magic fu on your file, while it's not loaded in a vim buffer. This is what I use with perl. I know there is probably a way to do this with sed, but I couldn't find it quickly enough. Most of the time when I do a search and replace out side of vim I want to replace something that's already in the file, with itself in a different position. For example:
I want to replace variablewordspace with variablewordtime, so in the file var.txt I have:
variablewordspace
hammerspace
So what I would do is:
cat var.txt | perl -pe 's/(\w+)space/\1time/'
which would give me the output:
variablewordtime
hammertime
For a brief description:
You are display the file var.txt, through the cat command. Then using the pipe (|) you are passing that to perl, with the switches p and e. Switch e allows you to run from the command line without explicitly writing a perl program, while switch p allows you to run a program against every line on standard input, and prints whatever is in $_ after each line. A search and replace using s/// with the search term in the first set of slashes
/(\w+)space/
The parentheses mark that as a group.
The \w+ means any number of word characters, as where the 'space' just represents the word space.
In the last set of slashes is what you want to replace it with.
/\1time/
\1 represents what is in that first set of parenthesis.
And time of course, represents the letters 'time'
Tuesday, December 14, 2010
Pull the alt / title tag from xkcd picture from command line
How to pull the alt (actually the title tag), from xkcd with the command line
[root@queerlikerice ~]# curl xkcd.com | grep -E -oh 'title=".* alt=' | grep 'title' | grep -E -oh '".*?"'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 8375 100 8375 0 0 137k 0 --:--:-- --:--:-- --:--:-- 538k
"And if you labeled your axes, I could tell you exactly how MUCH better."
Bash cli:
curl xkcd.com | grep -E -oh 'title=".* alt=' | grep 'title' | grep -E -oh '".*?"'
Sunday, July 5, 2009
Perl Tutorial on Getting Internal IP and beginning regex
I am working on an tiny project for remote server administration and this was a bit I can show you guys. This is how one would figure out the internal IP's for a computer running Mac or Linux, with perl, with a code explanation for each below:
For Mac:
#!/opt/local/bin/perl
my @ifconfig = `ifconfig`;
my $inetter = qr/(inet \d+\.\d+\.\d+.\d+)/;
foreach (@ifconfig) {
my $line = $_;
if ($line =~ $inetter){
print $&."\n";
}
}
The first line as alwas is the shebang, it tells the script where the perl interpretter is located, if you don't know, you can run your script by typing perl nameof.pl, rather than ./nameof.pl. The second line says that we want to create an array so that, we can load the output of ifconfig into it. Any time you put backticks it runs that command and returns the output. We use an array because it is multiple lines and we want to loop through it. In the next line we are writting or regular expression statement previos to running it. We are putting it in a scalar and using the quote regex function on our statement. We are looking for the phrase inet then a space then a digit then a period etc.. You may notice a lot of back slashes, it's because you have to escape the periods(otherwise our program won't parse them correctly), and the "\d+" is a replacement for saying a digit. Now we do a foreach loop, which says for each line in our array @ifconfig run what is in the curly brackets. The next line says declare a variable called line, it is assigned $_. $_ is a special variable in perl and means the defualt string, in this case the line from ifconfig we are looking at.Then we say if we found the match(inetter) Then print the matched phrase($&), then join the information that is in quotes "/n" and print that as well. /n by the way is a newline character, meaning a newline will begin. then we close out our brackets.
For Linux:
#!/usr/bin/perl
my @ifconfig = `ifconfig`;
my $inetter = qr/(inet addr:\d+\.\d+\.\d+.\d+)/;
foreach (@ifconfig) {
my $line = $_;
if ($line =~ $inetter){
print $&."\n";
}
}
The below paragraph is the same as the one explaining mac with one exception, the regex match expression($inetter) is diffrent because of slight diffrences in the comman ifconfig. Many people do not realize that Mac OS X is based off of a type of unix, BSD in particular(hence the name of the blog).
The first line as alwas is the shebang, it tells the script where the perl interpretter is located, if you don't know, you can run your script by typing perl nameof.pl, rather than ./nameof.pl. The second line says that we want to create an array so that, we can load the output of ifconfig into it. Any time you put backticks it runs that command and returns the output. We use an array because it is multiple lines and we want to loop through it. In the next line we are writting or regular expression statement previos to running it. We are putting it in a scalar and using the quote regex function on our statement. We are looking for the phrase inet then a space and then addr: and then another space then a digit then a period etc.. You may notice a lot of back slashes, it's because you have to escape the periods(otherwise our program won't parse them correctly), and the "\d+" is a replacement for saying a digit. Now we do a foreach loop, which says for each line in our array @ifconfig run what is in the curly brackets. The next line says declare a variable called line, it is assigned $_. $_ is a special variable in perl and means the defualt string, in this case the line from ifconfig we are looking at.Then we say if we found the match(inetter) Then print the matched phrase($&), then join the information that is in quotes "/n" and print that as well. /n by the way is a newline character, meaning a newline will begin. then we close out our brackets.
You may also like
How to get your external IP, using LWP and regex
Or Arp Sniffing Tutorial with perl
