SotD – Remove the First N Characters From a Line

I was asked today to remove the first 6 characters from every line in a document. It was known that each line contained at least 6 characters. For me, that means a find a replace. Here is how I did it (in a number of different ways). Can you think of any other ways to do it?

cut -c7-
rr ^.{6}
colrm 1 6
rr s/^.{6}//
sed 's/^.\{6\}//'
perl -pe 's/^.{6}//'
ruby -pe 'sub /^.{6}/,""'
gawk 'BEGIN{FIELDWIDTHS="6 999"}{print$2}'

I was pretty happy with rr taking the prize for least characters. I do want to point out that since rr defaults to multi-line and global replacements the “^” is required. If you wanted to remove the “^” (like you could with sed, perl, and ruby) then you would have to use the “–line” or “-l” and “–notglobal” or “-ng” options. So a single character replaces 7! If you want to grab rr just do:

$ sudo gem install regex_replace

Also, here is a link to gawk – GNU’s awk. This has the very nice FIELDWIDTHS variable, which is extremely useful!

Also keep in mind that the regular expressions above ^.{6} only work because the lines were known to have at least 6 characters. If we didn’t know that we would have had to use something like ^.{0,6} to allow up to 6 characters (and even that could be ^.{1.6} ignoring blank lines which can’t change). So again the requirements for this challenge were important.

Update!

I came across a neat little unix utility I didn’t know about called “cut”. As you can see, the new command tops the list quite handily too. “cut -c 7-” is actually cutting from each line the 7th character onwards (counting starts from 1). In this case once it spits what it cuts out to stdout; so it leaves behind those first 6 characters and therefore accomplishes our goal. In the above list I removed the optional space after “-c” to make it just a tad shorter. Pretty neat.

Then I found “colrm” which is the most straightforward. This one wins in simplicity. No hacks, just straightforward does exactly what you think it does. Very cool.

Double Update!

I figured since quite a bit of my rr usage has an empty string for the second argument, I figured it would be okay to throw that in as the default case. So now there is a third usage for rr, which is great for pipes, that just strips something out. That brings rr back up to a tie for first place. Very cool.

Code Golf

Programmers are notorious for solving puzzles and games. Essentially code golfing was born from that mind set. The goal of golfing is to get the lowest score. Code golfing as defined from the Code Golf home is:

Based on the original perl golf, Code Golf allows you to show off your code-fu by trying to solve coding problems using the least number of keystrokes.

Essentially the goal is to make your program as small as possible. The fewest bytes, the fewest characters, the smallest it can be. I am rather new to the sport but from what I have gathered it really kicking into action when scripting languages like Perl became popular. The original golf was Perl golf, as mentioned in the quote. Perl is a really cool programming language. Its powerful and robust but most importantly its fun to program. Its syntax is simple and its special variables and dynamic typing make it a joy to use. Interested in learning? However, recently languages like Ruby, PHP, and Python have shown they are just as effective.

How about an example? Lets start with something really basic. Lets add all of the numbers from 1 to N, where N is the number the user provides on the command line. A traditional approach might look like:

#!/usr/bin/perl
use warnings;
use strict; 

my $N = $ARGV[0];  # The given upper limit
my $sum = 0;       # To hold the summation

# Perform the summation
for ($i = 1; $i < $N; $i++) {
  $sum += $i;
}

# Print the result
print $sum, "\n";

Here was my Perl solution weighing in at 28 bytes complete with the terminating newline:

map$i+=$_,1..pop;print$i,$/

Example output:

$ perl sumToN.pl 15
120

It is completely possible that there might even be a smaller solution using a different approach, but you can see what is happening. The code is massively condensed to the point where there is nothing extraneous, nothing but pure code.

For those of you who are wondering why would anyone ever want to do this... you are forgetting that this is merely a game. Its a challenge, a puzzle to occupy your time and entertain your brain. As with all games you get better with practice, you learn new tactics and tricks, my original solution weighed in at 29 bytes using a for loop until I experimented with the map function to cut two characters. Golfing tests your knowledge of the language and helps you learn the secrets at the same time. When is a space or a semicolon necessary? Do I need to create an extra variable? It may not look like it from above, but you will become a better programmer.

More challenges include:

  1. Converting Roman Decimals to Decimal Numbers
  2. Drawing an Egg Timer of size N (Very Cool)
  3. Prime factorizing numbers

The list of games are endless, and the solutions equally immense. So what are you waiting for, take a swing at this challenge inspired by Worse Than Failure (the DailyWTF). Generate a random password of N length of alphanumeric characters (lowercase, uppercase, and numbers). Simply state your language of choice. Remember that the input length N, is provided on the command line as if you were to run the command from your terminal. My solutions weighed in at 59 bytes but I have a very cool 61 byte solution. Here is some example usage for you to work off of:

$ perl genpassword.pl 35
Pm0vfue12fWB45Ctr4Zc2zXNt5wkREGHQan

Give Perl a Chance

I had always wanted to learn Perl. I had a few brief encounters with it, using my php knowledge to drive my Perl programming in a very php styled way. However, I knew Perl could do so much more. I had nearly one week free and decided to give Perl a serious chance. I happened upon a great book available free from perl.org and was more than pleased.

Beginning Perl is available in PDF form for free from the Perl website. The book gives a very high quality, engaging, and through description of Perl. If you have been interested in learning Perl give this book a go and I assure you that you will get hooked into how the author daftly portrays subjects and concepts with excellent concise code examples and explanations. This is the kind of programming book that every programmer searches for. It is so engaging you will find that you will go through a chapter a day. The author has a gift for breaking subjects down to simple concepts, which is after all the real core of programming.

Make sure that you print out the appendixes. Read them over if you really want to grasp the full power of Perl and its endless options, special variables, and famous regular expressions. The actual chapter on regular expressions even tipped me off to certain tricks that I was even unaware of (lookaheads and lookbehinds). However if you are looking for a more general explanation of regular expressions I still recommend the links that I posted at the bottom of this article.

Many have considered Perl to be a confusing language. The book’s introduction declares that it means to dispel that rumor. Here is the direct quote from the introduction, hopefully it will motivate some of you to grab the PDFs and take a look:

However, since Perl is so easy to learn and to use, especially for quick little administrative tasks, ‘real’ Perl users tend to write programs for small, specific jobs. In these cases, the code is meant to have a short lifespan and is for the programmer’s eyes only. The problem is, these programs may live a little longer than the programmer expects and be seen by other eyes too. The result is a cryptic one-liner that is incomprehensible to everyone but the original programmer. Because of the proliferation of these rather concise and confusing programs, Perl has developed a reputation for being arcane and unintelligible – one that I hope we can dispel during the course of this book.

I will admit that the book is flawless in its persistence of providing full quality Perl scripts. I always strive to optimize my code, normally trying to increase efficiency and readability in my algorithms. However, Perl’s incredible flexibility does allow programmers to do some rather unbelievable tasks in well under 100 bytes (1 byte = 1 character). I will soon write an article about the dark side of every programmer… code golfing.

search