Archive

Archive for July, 2012

sed – exclude character in search

July 24, 2012 Leave a comment

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

Categories: sed

ln – symbolic links

July 16, 2012 2 comments

Problem:
I keep getting this wrong. I installed xmind and needed to add it to my PATH. What I decided to do was add a symbolic link to xmind in /usr/local/bin which was in my PATH. This is a two second job that took 5 mins coz I keep on fudging up ln -s by interchanging LINKNAME and TARGET :-).

RTFM:
From info coreutils ‘ln invocation’

`ln’ makes links between files. By default, it makes hard links; with
the `-s’ option, it makes symbolic (or “soft”) links. Synopses:

ln [OPTION]… [-T] TARGET LINKNAME
ln [OPTION]… TARGET
ln [OPTION]… TARGET… DIRECTORY
ln [OPTION]… -t DIRECTORY TARGET…

* If two file names are given, `ln’ creates a link to the first file
from the second.

TARGET = /usr/local/xmind/xmind
LINKNAME = /usr/local/bin/xmind

Which results in
root@computer#ln -s /usr/local/xmind/xmind /usr/local/bin/xmind

Categories: bash Tags: ,

awk – Convert columns to rows

July 3, 2012 Leave a comment

Problem:
You have a file with columns that you want to convert to rows.

From:

user@computer:~$less infile
field1 field2 field3
field1 field2
field1
field1 field2 field3 field4
...

To:

user@computer:~$less outfile
field1 field1 field1 field1
field2 field2 field2
field3 field3
field4

Solution:
awk

user@computer:~$ awk '{
      for (f = 1; f <= NF; f++)
         a[NR, f] = $f
   }
   NF > nf { nf = NF }
   END {
      for (f = 1; f <= nf; f++)
         for (r = 1; r <= NR; r++)
            printf a[r, f] (r==NR ? RS : FS)
   }' infile | tr -s " " " " | sed 's/^ //' > outfile

user@computer:~$less outfile
field1 field1 field1 field1
field2 field2 field2
field3 field3
field4

Source:
http://www.unix.com/unix-advanced-expert-users/188705-convert-column-data-row-data-using-shell-script.html
and
http://www.unix.com/78463-post3.html

Categories: awk, bash

paste – Changing columns to rows

July 3, 2012 Leave a comment

Problem:
Converting from columns to rows based on certain criteria of the input data.

From:

USER_ID
12/31/69 19:00:00
12/31/69 19:00:00
USER_ID
12/31/69 19:00:00
12/31/69 19:00:00
USER_ID
12/31/69 19:00:00
12/31/69 19:00:00
USER_ID
12/31/69 19:00:00
12/31/69 19:00:00
...

To:

USER_ID	12/31/69 19:00:00	12/31/69 19:00:00
USER_ID	12/31/69 19:00:00	12/31/69 19:00:00
USER_ID	12/31/69 19:00:00	12/31/69 19:00:00
USER_ID	12/31/69 19:00:00	12/31/69 19:00:00

Solution:
use paste

user@computer:~$ paste - - -  < infile 
USER_ID	12/31/69 19:00:00	12/31/69 19:00:00
USER_ID	12/31/69 19:00:00	12/31/69 19:00:00
USER_ID	12/31/69 19:00:00	12/31/69 19:00:00
USER_ID	12/31/69 19:00:00	12/31/69 19:00:00

NB:
The assumption is that you will always have two rows after USER_ID. If that is not the case then this will not work.

How it works:
My initial guess after reading the man is that since ‘- means read from standard input, paste gets the first 3 lines ie ‘- ‘- ‘- and puts them all on one row. Repeats the same process for all the rest.

With no FILE, or when
FILE is -, read standard input.

Source:
http://www.unix.com/unix-dummies-questions-answers/192249-append-following-lines-1st-line-every-3-lines.html

Categories: bash