Articles with tag ‘SotD‘

 
 

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.

Upcase a File - SotD

I just came up with something fun. Sotd - Script of the Day. Whenever I find/make a good script I will try to include it here, with the tag, and some fun content to work around it. Today’s script is the simple task of converting an entire file to uppercase. To standardize solutions I will say the current file is in.txt and we want the output file to go to out.txt. Here goes…

This simple task came from a question posted on Experts Exchange in the Scripting zone. Specifically Perl, however I putzed around with Perl and couldn’t quickly figure out what my print uc($_); was doing wrong, so I moved on to try my luck with Ruby. One minute without a need to look up any functions and 30 seconds of it not realizing I was still in the irb… and I had my Ruby solution:

> ruby -e "puts File.read('in.txt').upcase" > out.txt
> ruby -ne 'puts $_.upcase' < in.txt > out.txt

Which helped me produce the perl solution:
> perl -ne 'print uc' in.txt > out.txt

But I figured there had to be an easier way. I decided to manipulate tr///, transliterate, to convert lowercase characters to uppercase with the following one-liner piping the input file through tr and redirecting the output:

> cat in.txt | tr a-z A-Z > out.txt

I was pretty happy with it. How would you have done it?


Recent Resources

Clicky Web Analytics