Category Archives: UNIX & Linux How Tos and Tutorials

Text Manipulation: Multiple Line Regex Deletions Using Perl

Removing portions of files that match a multiple line regular expression can be tricky, unless you’re using perl. Let’s take an example file:

We would like to remove all headers that are not followed by any data. Not much of an example but it’ll demonstrate the technique nethertheless! The easiest way to do this with perl is to read the entire file into a single scalar variable, and then just parse that substituting our multiple line regular expression with nothing. Observe:

Obviously you’ll need to redirect the output of this file, or just write $data out to a new file within the perl script itself.

Running the script on the example data gives the expected output:

Using this method, you can easily modify the regular expression in the perl script to suit your needs.

Files and Filesystems: Determining the End of Line Character for a File

You would like to find out which end of line characters are used in a file. Run a command similar to the following:

For a DOS file, you get:

For a UNIX file, you get:

Where cr is a carriage return (\r), and nl is a newline (\n).

Alternatively,

gives the slightly more logical output of

for a DOS file, or

for a UNIX file.

Mac files terminate with \r only.

Text Manipulation: How to Delete the First Line of Text in a Large File

Editing a very large file can be a resource- (and time-) consuming nightmare. Having a requirement to delete the first line in such a file in-place whilst avoiding opening the file up in SomeEditor(TM) can be done in various ways, with various resource overheads.

Let me introduce you to the three methods we’ll be trying. The first uses GNU sed and the -i (inplace) option to edit the file in-place.

The second methods uses perl to get the job done.

The final example uses printf (so use a shell that supports it) and ex (command-line vi).

Let’s use these methods and time them…

So the moral of this tip? Use perl for performing edits on extremely large files!

How to Get Started with Solaris Containers

Disclaimer: This post was originally posted in 2008 as an article on the now-defunct website zazzybob.com. While the software version and actual commands used may vary, the concepts are still similar and give a general idea of how to approach a given problem.

Solaris Containers, available starting with Solaris 10, allow us to portion a physical server into one or more logical units. Whilst Containers are a form of virtualisation, they are not in the traditional sense (multiple OS instances with VMware, or hardware partitioning with LDoms).

The container can be thought of more like a “chroot” environment (in the case of sparse zones) where system resources are also in effect “chrooted” so that processes cannot run away and consume all of the resources of physical parent (the global zone), thus rendering the system inoperable. Only a single instance of Solaris 10 is ever installed (in the global zone) making package and patch management simple. Just apply the patch to the global zone, and all child zones will use the same binary set.

Some, or all, of the parent’s filesystems can be mounted read-write or read-only within the zone. Special care must be taken when mounting a global zone filesystem read-write, as the child zone may be able to cause a denial of service to the global zone by filling a disk.

Up until recently, Solaris Containers could only inherit the global (i.e. physical parent) zones TCP/IP stack. Now, we can assign exclusive phyical interfaces to the Container, but in this article I’ll be creating a dynamic link aggregation of three NICs in the global zone, and then allowing the zones to create virtual interfaces on this aggregation.

There are many more features of Solaris Containers I’ve not had time to mention - Sun do a perfectly good job of this over at docs.sun.com - as can other elimentary topics that have not been covered in this discussion.

Continue reading

Text Manipulation: How to Globally Delete Lines Matching a Certain Pattern

In order to delete complete lines that match a certain pattern, you can use various tools. I find that the easiest tools to use in this situation are perl or our humble elderly friend ed.

For example, say we want to delete all lines containing the string “delete_me”, appearing at the very beginning of the line. The following two commands will have the desired effect of deleting the required lines.

Using perl:

Using ed:

Of course, there are many more ways of achieving our goal but these are my two personal favourites. It goes without saying that you can modify the search pattern to meet your exact requirements.

GNU and UNIX Commands: GNU date and the -d option

If your system has GNU date installed, you can perform some very cool and quick date arithmetic with the -d option. For example:

You get the idea. Read the man and info pages for date for further information!

How to View LDOM Configuration as a Non-Privileged User

If you try to view LDOM configuration information as a non-privileged user, you’ll probably be greeted with this:


You can assign the “LDoms Review” profile to grant this privilege, i.e.:


Now, you can view the LDOM Configuration as the non-privileged user to which the privilege was assigned: