Good Ol’ sed

I’m sure someone could do the following more elegantly. But this sed script works, even though it does so in steps. That is what I like about sed. It just works, and there are times when you want to doctor up a file and not break out your favorite programming language and make everything formal.

The line in question — sample embedded in the script — won’t load into an Informix database table properly with all the white space.

{
# Delete blank lines

/^$/d

# These steps strip white space.
# from a line like this:
# 2011 | 43615|2011-06-23 16:08:41|r | 123681|

s/^s*([0-9][0-9][0-9][0-9])s*(.*)/12/
s/(^[0-9]*|)s*([0-9]*)(.*)/123/
s/(^[0-9]*|[0-9]*|)s*([0-9]*)(.*)/123/
s/(^[0-9]*|[0-9]*|.*)(|r)s*(|)s*([0-9]*|)/1234/
}
Advertisements

Leave a comment

Filed under Linux/Unix Utilities

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s