Commavee

from John Minnihan, founder of Freepository.

You are currently browsing the Tech Tips category.

Tech Tip: Multi-line Perl regex pattern match

Yesterday, I encountered an analysis issue that appeared to be resolvable with a simple pattern replacement technique.  This turned out to be a bit more complex than estimated, as the pattern spanned multiple lines and I struggled to get the regex constructed to match this use case.

I finally figured it out, and although the overall problem remains unresolved (it has many layers), this particular part works well.  My error was in how I viewed use of  /s and /m as mutually exclusive; they aren’t of course, and I have made that mistake one other time over the years.   I also hadn’t set the input record separator ($/) to paragraph read mode.   I’m posting details here to document my specific usage.

Assumptions:

  • A literal pattern exists that is present in multiple places in a file
  • The literal pattern spans multiple lines in the file
  • Literal pattern is

    • Node-path: branches/%%BRANCHES%%
    • Node-kind: dir
    • Node-action: add
  • Perl is being used to read the file and current line of file is $line
  • Objective is to delete all occurrences of this pattern from the file one $line at a time

Here’s how I did that:

[read the file & crawl thru the lines]

$/ = ”;

$line =~ s/^Node-path:\sbranches\/%%BRANCHES%%.*?^Node-kind:\sdir.*?^Node-action:\sadd.*?//sm;

Tags: , ,

Popularity: 48% [?]

Posted 2 weeks, 3 days ago.

Comments

Quick file & directory cleanup tools

Every sys admin needs to quickly free up disk space from time to time, and here are two well-tested scripts I use for exactly this purpose.

The first is a simple command-line use of exec that uses find to construct of list of matching file names, in this case all gzipped tarballs, and then runs the rm command on them.  The curly braces "{}" act as a holder for each file name from the list, and the trailing "\" is used to escape the ";", so that it is passed to find as a literal command terminator.

#find . -name *.tar.gz -exec rm {} \;

You may find (pun intended) that you need a slightly more sophisticated way to construct the list of things to remove.  For example,  to recursively delete all files & directories from an arbitrary depth in the file-system, except for special files or directories.  To accomplish this, I use a small Perl utility I wrote that uses the File::Find module. 

Here is the actual script I use generalized out with a fake $top and /some-pattern-to-exclude/. This script will crawl down (actually the direction that finddepth goes is up) a list of files & directories, and unless the pattern is matched, each file in the directory & then the directory itself is deleted.

How does this work?  Pretty simple – the File::Find module loads some routines, one of which is finddepth. The first argument it expects is a routine to run against all files & directories found, starting at the location named in the second argument.  The wanted routine is run on each element in finddepth’s list & you can use  finddepth’s variables to make your wanted routine smarter.

Tags: , ,

Popularity: 8% [?]

Posted 2 weeks, 5 days ago.

Comments

Converting .DOC to .PDF. Is this difficult?

If you are using a Mac running a recent version of OSX, the answer is No.  In fact this is super easy and is available from inside Microsoft Word.   If you run Windows, this feature isn’t available without an additional plugin from a third-party.

 

Continue Reading…

Popularity: 1% [?]

Posted 1 year, 5 months ago.

Comments

Gmail, Apple Mail & the iPhone

Since Gmail began supporting IMAP, setting up Gmail to work with both Apple Mail & the iPhone is a common need for those of us with both iPhones and new Macbooks.

I began using Gmail about two weeks and had noticed the inconsistencies mentioned in the below article, and was both pleased to see that the solution was so simple as well as slightly embarrassed that I hadn’t yet resolved the issue myself.

Here’s the HOWTO:

http://5thirtyone.com/archives/862 

Tags: , , ,

Popularity: 2% [?]

Posted 1 year, 7 months ago.

Comments

AJAX Logfile Tailer & Viewer

Recently I had a need for a simple logfile viewer for use in some stuff we have planned at Freepository. But this log file viewer had a few requirements that made it unique: it had to get the log file contents from the server in small chunks, not tie up the browser (such as an old-style synchronous request would do), and refresh in the browser without reloading the page.

I thought I could easily find one that someone had already written, but Google was not my friend. I found nothing even close, so I wrote my own. Here it is.

Continue Reading…

Popularity: 57% [?]

Posted 2 years, 2 months ago.

Comments