sed reads a file line by line. It edits it according to the sed-script, and then prints the line. After that it read the next line, and runs the sed-script on it from the start. Bu default sed prints every line wether it was changed or not. The "-n" switch tells it not to do that, but only print when the sed-script says so.
The sed command "s/some string/replacement/" replaces "some string" with "replacement" in a each line. You can use "regular expressions" ("regexp") to match some string. Regexp's work similar to file-name matching with wildcards in the shell (cat *.txt), but more complex and with more possibilities. A dot ( . ) means "any character" and a asterisk ( * ) means any number of the character before it. So:
s/.*some string/replacement/
Will replace everything from the start of the line up to and including "some string" with "replacement". In the replacement you can use "\1" to refer to the part in the matched string that is in between "\(" and "\)", so:
s/.*some string\(.*\)/\1/p
Will replace any entire line that has "some string" in it with the stuff that is between "\(" and "\)", which is, in this case, the stuff after "some string" until the end of the line. The "p" tells sed to print the line only if a replacement was done (remember the "-n" switch).
To have the results on one line, we want to remove the newline characters ("\n"). to do this the results are piped ( | ) through another sed command.
This time sed has to print everything, so no "-n" switch here. the only thing it has to do is replace newlines with spaces. We can do this with "s/\n/ /". But there's one problem: sed does things line by line, so normally newlines are not available. To solve this, the "N" command appends the next line into sed's editing buffer (called "pattern space" in sed jargon). Now two lines are in sed's buffer, seperated by a newline ("\n") and we can replace the "\n" with a space " ". But this still works only for one extra line. We need to read every line, one by one, into the buffer and replace the newline:
":a" is just a label. It is a mark where to jump to later.
"$!N" means: read next line into the buffer (N), exept for the last line ('$' means "last", and '!' means "not")
"s/\n/ /" replaces a newline with a space.
"ta" means: if before there was a replacement, jump to the place in the script labeled ":a". If there is another line in the input, it will always find a newline, and thus will always jump to ":a". If there are no more lines in the input, it wil not jump, end the script, and then output the edited buffer.
The above sed-script is multi-line sed script (for clarity), but it can be put on one line by using semicolons ( ; ) to seperate the different sed-commands.
For more info about sed:
Type "info sed" in a terminal.
and/or read:
http://go.to/sed-faq