Home > sed > sed – exclude character in search

sed – exclude character in search

Needed to match characters in a string. Due to the greedy nature of regex in sed I had to be more specific with the search and include [a-z0-9] when matching the text. Fortunately, there is a much simpler way of doing this.

Say you have an entry like the one below. (Copied from unix.com)

$less infile
GET /dynamic_branding_playlist.fmil?domain=915oGLbNZhb&pluginVersion=3.2.7_2.6&pubchannel=usa&sdk_ver= %3D573900%26req%3D5006e9ce1ca8b26347b88a7.1.825&sdk_url=http%3A%2F%2Fdivaag.vo.llnwd.net%2Fo42%2Fhtt p_only%2Fviewster_com%2Fv25%2Fyume%2F&viewport=42

You want to match the values of domain and sdk_version. This should work.

$sed 's/.*domain=\([^&]*\).*sdk_ver=\([^&]*\).*/\1 \2/' infile

What I learnt from this was the use of [^&]* which allows the regexp to match any character except the &. The longer way of matching would be [0-9a-zA-Z]*&.


Categories: sed
  1. No comments yet.
  1. No trackbacks yet.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.