Home > sed > sed – exclude character in search

sed – exclude character in search

Problem:
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.

Solution:
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=2.4.6.3&width=680&height=290&embeddedIn=http%3A%2F%2Fviewster.com%2Fsplash%2FOscar-Videos-1.aspx%3Futm_source%3Dadon_272024_113535_24905_24905%26utm_medium%3Dcpc%26utm_campaign%3DUSYME%26adv %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]*&.

Source:
http://www.unix.com/shell-programming-scripting/194189-extract-key-words-print-their-values.html

Advertisements
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 )

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