sed question

Cameron Simpson cs at zip.com.au
Fri Aug 19 22:22:13 UTC 2011


On 19Aug2011 14:59, Rob DeSanno <rdesanno at gmail.com> wrote:
| On Fri, Aug 19, 2011 at 2:28 PM, Josh Miller <joshua at itsecureadmin.com>wrote:
| > On 08/19/2011 11:06 AM, Rob DeSanno wrote:
| >> sed -e "s/^'/\<tr\>\<td\>/" $stores_down | sed -e "s/'>/\<\/td\>\<td\>/" |
| >> *sed
| >> -e "s/-[0-9]/\<\/td\>\<td\>/"* | sed -e "s/$/\<\/td\>\<\/tr\>/">>  $log
| [...]
| > Try this, I've split the sed command into a sed script (file.sed) [...]
| 
| Works like a champ and I was sooo close. Thanks a lot for your help!!

Further to Josh's fix for you, I have some suggestions for your future
sed uses:

  - Josh's approach of putting the whole thing in a file is generally a
    win for anything non-trivial, so try to do it.

  - If you're working on the command line directly with "sed -e", it
    is far far better to use single quotes instead of double quotes if you
    can. In double quotes the $ and backslash characters are subject to
    shell interpretation, making their use in sed commands more tricky
    and error prone.
    I see you've got some single quotes in your opening sed commands, hence
    perhaps your choice of double quotes, but even then I'd be personally
    inclined to use single quotes and to leave them just to get the
    benefit of single quotes in general, eg:

      sed -e 's/^'\''/<tr><td>/'

    You see the embedded single quote has become more ugly but the need
    for other backslashes is greatly reduced.

    Having once written a sed command line needing exactly 13 conseutive
    backslashes, I now seek to reduce the need for such nesting.

  - If you're running multiple sed commands it is more efficient
    (performant) if they are all in one sed command - this saves copying
    your data between multiple seds. Of course, this doesn't matter much
    for small files or infrequent commands, but large files and commands
    run many times in loops will show the difference. Here again Josh's
    "put it all in a file" approach makes it easier.

  - If you're debugging, as you are, multiple sed commands piped together
    lets you examine the changes more easily, especially via the use of the
    tee command:

      sed -e ... | tee stage1.txt | sed -e ... | tee stage2.txt | ...

    You can then examine the stage1.txt etc files for each
    transformation in turn. The diff command can also be useful for this
    examination:

      diff -u stage1.txt stage2.txt

    to see what changes the second sed wrought.

Cheers,
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

Copyright and Patents: To prevent the Progress of Science and useful Arts, by
securing for unlimited Times to Authors and Inventors and Trolls the
exclusive Right to all Writings and Discoveries.
- http://science.slashdot.org/story/11/04/26/211258/Copyright-Law-Is-Killing-Science




More information about the redhat-list mailing list