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
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
/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.
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.