Wednesday, December 27, 2006

Reviewing Sed - Part 1

There is a good collection of sed one liners at http://www.student.northpark.edu/pemente/sed/sed1line.txt.

So I have just begun to review few them using sed manpage because of the earthquake in Taiwan.

# Following line will insert a blank space after everyline in temp.txt

sed G temp.txt

G - Append hold space to pattern space.

So hold space holds nothing initially and if you append that to pattern space, you get a new line.

So thats the way to add blank lines after everyline.
Sed goes through each line and loads it to the pattern space.

# Adds only a single blank line to all lines to temp.txt

sed '/^$/d;G' temp.txt

/regexp/ - Match lines matching the regular expression regexp.

d - Delete pattern space. Start next cycle.

There are two things to consider.
For blank lines : 'd' is executed that deletes the pattern space and starts next cycle(loads next line and starts all over again)

For non blank lines : as the pattern doesn't match, d is not executed and next command G gets executed.
We get a blank line after every non blank line.

The command would delete the blank lines and skip the G command for blank lines.
Thing to note is that d command applies only to lines matching the regex /^$/ whereas
G command applies to all lines and is not tied to the regex. However when regex is matched the d command will cause G command to be skipped.

Wednesday, December 13, 2006

Sed and No DuPlication

I needed to remove duplicate names from a file so I thought sed should be a good choice for it.
uniq is too easy so I found out how sed does it. Herez how

$ sed '$!N; /^\(.*\)\n\1$/!P; D' filename

So now time for an explanation.

1 $!N - sed reads one line at a time and then works on it. It doesn't read in the newline at the end of the line into pattern space (what sed works on). So N command appends the newline and the next line to the pattern space.
$ denotes the last line and ! means NOT, so it means for the last line don't execute N command.Heck as if there is anything to read after the last line..read it if you can for all I care.

2. /^\(.*\)\n\1$/!P; - If you see start of pattern space and then anything followed by a newline which is followed by exactly that anything. Don't "print the first part of pattern space till the newline"(P) else print first part of pattern space till the newline

3. D - "just delete the first part of pattern space till the newline and restart the command cycle i.e go back to N"

Friday, December 08, 2006

Its time for tee

I was asked this question and suddenly i realized that bash doesnt have redirection like operator to do this. Do what? Print Standard Output/Error as it is and also redirect it to a file. So therefore its time for tee.


$ls i_dont_exist 2>&1 | tee output.txt
ls: i_dont_exist: No such file or directory
$cat output.txt
ls: i_dont_exist: No such file or directory