Code

That’s Awksome!

Have you ever been doing something on the terminal and realise that most of the information thats being reported is useless to you? Ever thought, “Hey it would be really cool if it would only show me ‘just that bit’”?

Awk could be the answer. Awk is a DSL or Domain Specific Language aimed at text processing, which is used by many command line guru’s. Although awk has an extremely large amount of uses, I will only be detailing its print statement in use with a pipe.

The awk print statement is used, obviously, to print things, not that spectacular but the real magic comes with awk’s variables. When you pass a stream to awk it counts the number of columns, or parts of the stream separated by the input field separator, which by default is whitespace, it then assigns each section a variable $num. So in the following line

This is a line.

$1, $2, $3, $4 = This, is, a, line.

So you may be able to see how it is useful, but you may not be able to see how practical this is.

Lets do an example run using ls. If I run

ls -lh

in a terminal I get the following output:

drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:46 Angus,Thongs.And.Perfect.Snogging[2008]DvDrip-aXXo
-rw-r--r-- 1 bod bod 149M 2009-06-22 07:50 archlinux-2009.02-ftp-i686.iso
drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:46 Bend It Like Beckham (2002) DVDRip XviD [desidhamal.com]
drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:46 Bride Wars[2009]DvDrip[Eng]-FXG
drwxr-xr-x 2 bod bod 4.0K 2009-06-23 13:46 Desktop
drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:46 downloads
drwxr-xr-x 6 bod bod 4.0K 2009-06-22 07:46 Hes.Just.Not.That.Into.You.DVDRip.XviD-DiAMOND
drwxr-xr-x 3 bod bod 4.0K 2009-06-22 07:46 Hitch[2005]DVDrip[Eng]AC3[5.1]-Atlas47
drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:46 Ice Road Truckers.S 03e02.rookie.run
-rw-r--r-- 1 bod bod 348M 2009-06-22 08:17 Ice.Road.Truckers.S03E03.Canadian.Invasion.DSR.XviD-KRS.avi
drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:46 livecd-i686-installer-2008.0-r1
drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:46 lose a guy torrent
-rw-r--r-- 1 bod bod  303 2009-05-29 19:03 not_found.html
-rw-r--r-- 1 bod bod  303 2009-05-29 19:03 not_found.html.1
drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:46 Ocean's.12[2004]DvDrip[Eng]-aXXo
drwxr-xr-x 2 bod bod 4.0K 2009-06-23 08:46 perl
drwxr-xr-x 2 bod bod 4.0K 2009-06-25 17:51 python
drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:52 Relapse
drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:46 Spore-RELOADED
drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:46 The.Accidental.Husband[2008]DvDrip-aXXo
drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:46 The.Illusionist[2006]DvDrip[Eng]-aXXo
-rw-r--r-- 1 bod bod 2.4K 2008-12-11 21:54 t_skariah.asc.gpg
-rw-r--r-- 1 bod bod 3.0K 2009-06-23 13:59 tuto.txt
-rw-r--r-- 1 bod bod  575 2009-06-23 08:45 tuto.txt~
drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:46 Underworld Rise of the Lycans[2009]DvDrip[Eng]-FXG
drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:46 Yes.Man.2008.DvDRip-FxM

Now lets say, I only wanted to know about the name and the permissions. I could use awk to only give me those sections. Note: The use of the ls flag –quoting-style=locale makes ls quote filenames meaning awk will print filenames with spaces as one column instead of splitting them.

ls -lh --quoting-style=locale | grep -v "total " | awk '{print $1 "\t" $8}'

That would output this:

drwxr-xr-x	`Angus,Thongs.And.Perfect.Snogging[2008]DvDrip-aXXo'
-rw-r--r--	`archlinux-2009.02-ftp-i686.iso'
drwxr-xr-x	`Bend
drwxr-xr-x	`Bride
drwxr-xr-x	`Desktop'
drwxr-xr-x	`downloads'
drwxr-xr-x	`Hes.Just.Not.That.Into.You.DVDRip.XviD-DiAMOND'
drwxr-xr-x	`Hitch[2005]DVDrip[Eng]AC3[5.1]-Atlas47'
drwxr-xr-x	`Ice
-rw-r--r--	`Ice.Road.Truckers.S03E03.Canadian.Invasion.DSR.XviD-KRS.avi'
drwxr-xr-x	`livecd-i686-installer-2008.0-r1'
drwxr-xr-x	`lose
-rw-r--r--	`not_found.html'
-rw-r--r--	`not_found.html.1'
drwxr-xr-x	`Ocean\'s.12[2004]DvDrip[Eng]-aXXo'
drwxr-xr-x	`perl'
drwxr-xr-x	`python'
drwxr-xr-x	`Relapse'
drwxr-xr-x	`Spore-RELOADED'
drwxr-xr-x	`The.Accidental.Husband[2008]DvDrip-aXXo'
drwxr-xr-x	`The.Illusionist[2006]DvDrip[Eng]-aXXo'
-rw-r--r--	`t_skariah.asc.gpg'
-rw-r--r--	`tuto.txt'
-rw-r--r--	`tuto.txt~'
drwxr-xr-x	`Underworld
drwxr-xr-x	`Yes.Man.2008.DvDRip-FxM'

Pretty cool yeah? So, thats all well and good, but I want to add my own spin to the output, the following is a bit different but shows awk’s brilliance even more.

ls -lh --quoting-style=locale | grep -v "total " | awk '{print "Size is ", $5 "\tfor ", $8}'

This will print out the following:

Size is  4.0K	for  `Angus,Thongs.And.Perfect.Snogging[2008]DvDrip-aXXo'
Size is  149M	for  `archlinux-2009.02-ftp-i686.iso'
Size is  4.0K	for  `Bend
Size is  4.0K	for  `Bride
Size is  4.0K	for  `Desktop'
Size is  4.0K	for  `downloads'
Size is  4.0K	for  `Hes.Just.Not.That.Into.You.DVDRip.XviD-DiAMOND'
Size is  4.0K	for  `Hitch[2005]DVDrip[Eng]AC3[5.1]-Atlas47'
Size is  4.0K	for  `Ice
Size is  348M	for  `Ice.Road.Truckers.S03E03.Canadian.Invasion.DSR.XviD-KRS.avi'
Size is  4.0K	for  `livecd-i686-installer-2008.0-r1'
Size is  4.0K	for  `lose
Size is  303	for  `not_found.html'
Size is  303	for  `not_found.html.1'
Size is  4.0K	for  `Ocean\'s.12[2004]DvDrip[Eng]-aXXo'
Size is  4.0K	for  `perl'
Size is  4.0K	for  `python'
Size is  4.0K	for  `Relapse'
Size is  4.0K	for  `Spore-RELOADED'
Size is  4.0K	for  `The.Accidental.Husband[2008]DvDrip-aXXo'
Size is  4.0K	for  `The.Illusionist[2006]DvDrip[Eng]-aXXo'
Size is  2.4K	for  `t_skariah.asc.gpg'
Size is  3.0K	for  `tuto.txt'
Size is  575	for  `tuto.txt~'
Size is  4.0K	for  `Underworld
Size is  4.0K	for  `Yes.Man.2008.DvDRip-FxM'

Now, lets pick that command apart. The first section:

ls -lh --quoting-style=locale

Gives us a listing of the current working directory in a human readable long listing style. Meaning it prints the file sizes with K/M/G/T on the end, long listing means it gives you loads of extra info, like permissions, group user ownership, timestamps etc.

The next section:

grep -v "total "

Means, before we pass the text to awk, get rid of any line which contains “total “, ls -lh would usually print “total ” as the first line, which would muck up our awk command.

The final section:

awk '{print "Size is ", $5 "\tfor ", $8}'

Means, print the words “Size is” followed by column 5 then the word “for” then print column 8. The ‘\t’ is an escape character which means tab, it tells awk to insert a tab there. The various ‘,’ and whitespace in the quotes are just for presentation, you’ll have to experiment with them yourselves.

In case you didn’t know, the pipes

|

send the output of the command preceding the pipe, to the input of the command succeeding the pipe.

Hopefully now you can see the power of awk. This is by no means whatsoever a complete guide, It barely covers the basics. If you wnt to find out more, check out chapter 6 of the bash beginners guide.

Thanks for reading.

PS: Angus,Thongs.And.Perfect.Snogging[2008]DvDrip-aXXo is not porn, its a film my girlfriend bought :)

Read More
Opinion

Do You Know How Matt Mullenweg Works?

Today I read a great article of Matt explaining the way he works over at Automattic published  at Inc.Com: It is into two versions, Matt’s version and Inc’s one. Inc seems to write your words in first person their way. That is why Matt decided to rewrite it for his blog readers. I like more the Inc version, even though Matt says: “ but it’s a little misleading“. I am bringing bits here:

***

I go out for lunch whenever I can, which fits well with my preference for no meetings before 11 AM. There’s something very personal about sharing food with someone; it’s a deeper connection than shaking hands in a boardroom. Often when I’m in town I’ll have lunch with Toni Schneider, my CEO. He and I get along super well which is one of the reasons I think the business has worked. He brings gravitas because he’s a digital native but also has great startup experience including being the CEO of Oddpost, a webmail company Yahoo acquired in 2004. Sometimes we’ll go to lunch at 12:30 and stay until 5.

***

For WordPress we’re trying to set up a community that will be around 10 to 30 years from now, one that’s independent from the whims of the market. My role is somewhat like Linus for Linux or Shuttleworth for Ubuntu, affectionately referred to as BDFL, and it’s my responsibility to meet as many users as possible and direct the software in a way that reflects their interest. Last year I probably met 5,000 or 6,000 WordPress users, about half of them who make their living from it. We want to be like Google, eBay, Amazon — they all enable other people to make far more money than the company captures. That’s ultimately what we’re trying to do, we’re trying to create a movement.

My Mom started a blog a couple of months ago. Six years into this, and we finally made it easy enough for my Mom to use. (She hates it when I say that.)

I enjoyed it very much. All I can say is: thanks Matt for giving us the pleasure.

Read More
Code

How to Install XAMPP on Ubuntu

Introduction

If you are a Linux (or Ubuntu) web developer or even a web designer with interest in CMS systems like WordPress, Drupal, Joomla etc., you are for sure in need of a web development environment on your work computer to test websites or themes that you built for any CMS. If you have not fund a solution yet, the good news is that there is one – it is XAMPP.

Read More
Business

How to Become a WordPress Freelancer

In the coming weeks and months WPlancer.com will be transforming itself. From its humble beginnings as a Professional WordPress Freelancer’s showcase, the site will become the singular source of information on WordPress Freelancing on the Web. I have joined Baki (aka Banago) as a co-author and will be bringing you articles on how find your niche in the wonderful independent world of Freelancing.

Some things that you can look forward to:

Setting up shop as a Freelancer – How to have your own humble beginnings!

  • Finding your niche in the world. You might have expertise on a certain subject or a special talent that is hard to find. We’ll show you how to take advantage of that as a Freelancer.
  • Technical How-to’s. Even though WordPress is becoming an increasingly powerful CMS (Content Management System) it can also be quite technical. With articles and step-by-step tutorials we will be able to walk you through even the toughest of tasks.
  • WordPress Themes. Our team will also be bringing you customizable themes for you to work with so you don’t always have to start from scratch as well as Premium themes ready to deploy for that time-pressed client.
  • Much much more! We are open for suggestions and always welcome constructive criticism to make this the best WordPress Freelancing resource on the web.

To start things off though, we would like to tell you the simplest way to get started with WordPress is: JUST GET STARTED! The easiest way to begin your career as a freelancer is to just begin. By that we mean, get yourself a project, ANYTHING! Find yourself a friend in dire need of a decent website, offer to help him or her for a small fee (or even free of charge!) and then set him up with WordPress and a suitable theme and Ey Presto! You’re a Freelancer!

If you can’t find a friend with that sort of need then just create a need. Create your own WordPress site. Now there’s no need to go crazy and rush out and buy the most expensive hosting and a professionally designed theme. You can check out some WordPress hosting reviews to find an inexpensive but reliable service for you. Than set yourself up and get familiarised with the system if you aren’t already. Start writing about your specific niche and circulate your site around your interest groups. When someone asks who did your site, proudly proclaim that you did it yourself and that you’d be happy to do theirs.

It is actually quite simple to become a WordPress Freelancer, or any Freelancer for that matter! You just have to get started and get yourself noticed. Are you going to be able to charge hundreds of dollars per hour right away? No. But you will definitely get yourself on the path towards your goal, whether that is to be a full-time pro Freelancer or just some extra cash on the side.

Thanks for reading and we look forward to seeing you here!

Read More
Code

Accessible CSS

wordpress-themes-cssWhen I needed to hack a WordPress theme and I got my hands dirty with CSS code, sometimes I came across with in-line written CSS code i.e. every CSS selector occupied only one row. I found this practice very ugly and not scannable. I was used to CSS written in the classic way where every value is written in a new line. So, I would often break the single lines into multiple ones to distinguish them better. But this did not last forever. What happened?

Not very far in the past, I came across a article about single line CSS, where Steve Smith, the designer at OrderedList.Com had written of the same topic. He had gone thought the same dilemma I went and his conclusion is no different from mine. He says:

But imagine now that you have over 200 selectors, each with a laundry-list of attributes. That’s quite a mess to search through. My issue with CSS files like these is when they get large, it becomes very difficult to scan the document for a particular selector. They’re all separated by loads of attribute and value pairs. So what if we took out that separation, and put each selector, and it’s attributes and values all on the same line?

Let’s look at a live example taken from a site I have been developing lately. This is the way the code would look like if it was coded classically.

h1{
    float: left;
    width: 120px;
    height: 40px;
    margin-top: 35px;
}
h1 span{
    display: none;
}
h2 {
    padding: 0 0 15px 0;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 18px;
    line-height: 20px;
    color: #159089;
    font-weight: 200;
}
h2 a {
    color: #159089
}
h3 {
    padding: 0 0 15px 0;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 16px;
    line-height: 18px;
    color: #159089;
    font-weight: 200;
}
h4 {
    font-family: Geneva, Arial, Helvetica, sans-serif;
    font-size: 22px;
    line-height: 24px;
    color: #d21d1d;
    font-weight: 500;
    text-align: left;
}
h5 {
    padding: 10px 0 0 0;
    font-family: Geneva, Arial, Helvetica, sans-serif;
    font-size: 16px;
    line-height: 19px;
    color: #4b4141;
    font-weight: 500;
    text-align: left;
}

And this is actual code I have coded of the site using the single line method.

h1 { float: left; width: 120px; height: 40px; margin-top: 35px; }
h1 span { display: none;  }
h2 { padding: 0 0 15px 0; font-family:Arial, Helvetica, sans-serif; font-size:18px; line-height: 20px; color: #159089; font-weight:200;}
h2 a { color: #159089; }
h3 { padding: 0 0 15px 0; font-family:Arial, Helvetica, sans-serif; font-size:16px; line-height: 18px; color: #159089; font-weight:200; }
h4 { font-family: Geneva, Arial, Helvetica, sans-serif; font-size:22px; line-height: 24px; color: #d21d1d; font-weight:500; text-align:left; }
h5 { padding: 10px 0 0 0; font-family: Geneva, Arial, Helvetica, sans-serif; font-size:16px; line-height: 19px; color:#4b4141; font-weight:500; text-align:left; }

Note the length of the first sample and keep in mind that this is only less than 5% of all the CSS code I wrote for that site. Can you imagine how long the file would be if it weren’t in a single line and how difficult it would be to look for a certain selector to edit? You would spend more time browsing up and down thought the code rather than actually writing or editing it. That is why I chose to write all my CSS code in single line selectors method, so that I can speed up my work and spend less time looking for selectors in a super-long CSS file.

So, why not give it a try yourself? I think it is worth trying and I strongly hope you will get to love it as I do now. What do you think?

Read More
1 5 6 7 8 9 15