Unix Shell - sed/regexp: remove empty lines

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > January 2007 > sed/regexp: remove empty lines





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author sed/regexp: remove empty lines
rolf randen

2007-01-19, 1:18 pm

hello there,

i'm having a problem with a shell-script for removing empty lines from a
logfile.

i do
sed -e '^$//'
and get the error
": invalid command code ^

this is the whole script:

#!/bin/bash
cat /Volumes/Save/daily.log | sed -e 's/xtar.*Icon..d.*directory//' |
sed -e 's/xtar.*Icon.*d.*directory//' | sed -e 's/xtar.*Network Trash
Folder.*denied//' | sed -e '^$//' | mail -s "backup-daily"
logcheck@mailserver.local

without removing the empty lines the script runs fine.
i'm working with bash on macos 10.3.9

please help!

rolf
Icarus Sparry

2007-01-19, 1:18 pm

On Fri, 19 Jan 2007 18:16:48 +0100, rolf randen wrote:

> hello there,
>
> i'm having a problem with a shell-script for removing empty lines from a
> logfile.
>
> i do
> sed -e '^$//'
> and get the error
> ": invalid command code ^
>
> this is the whole script:
>
> #!/bin/bash
> cat /Volumes/Save/daily.log | sed -e 's/xtar.*Icon..d.*directory//' |
> sed -e 's/xtar.*Icon.*d.*directory//' | sed -e 's/xtar.*Network Trash
> Folder.*denied//' | sed -e '^$//' | mail -s "backup-daily"
> logcheck@mailserver.local
>
> without removing the empty lines the script runs fine.
> i'm working with bash on macos 10.3.9
>
> please help!
>
> rolf


sed is objecting to the ^ character. What you want is
sed -e '/^$/d'

the /..../ matches a pattern. The pattern ^$ is start-of-line followed
immediately be end-of-line. The 'd' is the action to take when the pattern
matches, in this case delete the line.
Bill Marcum

2007-01-19, 1:18 pm

On Fri, 19 Jan 2007 18:16:48 +0100, rolf randen
<randenrolf@gmx.net> wrote:
>
>
> hello there,
>
> i'm having a problem with a shell-script for removing empty lines from a
> logfile.
>
> i do
> sed -e '^$//'
> and get the error
> ": invalid command code ^
>

sed -e '/^$/d'


--
BOFH excuse #92:
Stale file handle (next time use Tupperware(tm)!)
Ed Morton

2007-01-19, 1:18 pm

rolf randen wrote:

> hello there,
>
> i'm having a problem with a shell-script for removing empty lines from a
> logfile.
>
> i do
> sed -e '^$//'
> and get the error
> ": invalid command code ^
>
> this is the whole script:
>
> #!/bin/bash
> cat /Volumes/Save/daily.log | sed -e 's/xtar.*Icon..d.*directory//' |
> sed -e 's/xtar.*Icon.*d.*directory//' | sed -e 's/xtar.*Network Trash
> Folder.*denied//' | sed -e '^$//' | mail -s "backup-daily"
> logcheck@mailserver.local
>
> without removing the empty lines the script runs fine.
> i'm working with bash on macos 10.3.9
>
> please help!
>
> rolf



You got your specific answer (/^$/d) but you could simplify the whole
script:

cat /Volumes/Save/daily.log | sed -e 's/xtar.*Icon..d.*directory//' |
sed -e 's/xtar.*Icon.*d.*directory//' | sed -e 's/xtar.*Network Trash
Folder.*denied//' | sed -e '^$//' | ...

The "cat" is useless since sed can open a file just as easily as cat
can, so this:

cat /Volumes/Save/daily.log | sed -e 's/xtar.*Icon..d.*directory//' |

could be just this:

sed -e 's/xtar.*Icon..d.*directory//' /Volumes/Save/daily.log |

The first of these 2 sed commands:

sed -e 's/xtar.*Icon..d.*directory//' |
sed -e 's/xtar.*Icon.*d.*directory//' |

is a subset of the second since ".." (any 2 characters) is a subset of
".*" (any sequence of characters), so the above can be reduced to just:

sed -e 's/xtar.*Icon.*d.*directory//' |

so, so far, you're left with:

sed -e 's/xtar.*Icon.*d.*directory//' /Volumes/Save/daily.log |
sed -e 's/xtar.*Network Trash Folder.*denied//' |
sed -e '/^$/d' |
mail -s "backup-daily"
logcheck@mailserver.local

sed only supports BREs but if you switch to awk you can take advantage
of EREs to combine the first 2 sed commands to this:

awk '{sub(/xtar.*(Icon.*d.*directory|Network Trash
Folder.*denied)/,"")}1' /Volumes/Save/daily.log

and you can also test the NF (Number of Fields) variable in awk to get
rid of empty lines thus removing the need for the final sed command and
so the whole scipt becomes just:

#!/bin/bash
awk '{sub(/xtar.*(Icon.*d.*directory|Network Trash
Folder.*denied)/,"")}NF' /Volumes/Save/daily.log |
mail -s "backup-daily"
logcheck@mailserver.local

Use of "NF" as above will also remove lines that contain just blank
characters. If you WANT those to be printed, then use "!/^$/" instead:

#!/bin/bash
awk '{sub(/xtar.*(Icon.*d.*directory|Network Trash
Folder.*denied)/,"")}!/^$/' /Volumes/Save/daily.log |
mail -s "backup-daily"
logcheck@mailserver.local

Regards,

Ed.
Chris F.A. Johnson

2007-01-19, 1:18 pm

On 2007-01-19, rolf randen wrote:
> hello there,
>
> i'm having a problem with a shell-script for removing empty lines from a
> logfile.
>
> i do
> sed -e '^$//'


sed -e '/^$/d'

> and get the error
> ": invalid command code ^
>
> this is the whole script:
>
> #!/bin/bash
> cat /Volumes/Save/daily.log | sed -e 's/xtar.*Icon..d.*directory//' |
> sed -e 's/xtar.*Icon.*d.*directory//' | sed -e 's/xtar.*Network Trash
> Folder.*denied//' | sed -e '^$//' | mail -s "backup-daily"
> logcheck@mailserver.local


sed -e 's/xtar.*Icon..d.*directory//' \
-e 's/xtar.*Network Trash Folder.*denied//' \
-e '/^$/d' /Volumes/Save/daily.log |
mail -s "backup-daily" logcheck@mailserver.local

> without removing the empty lines the script runs fine.
> i'm working with bash on macos 10.3.9
>
> please help!
>
> rolf



--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Alan Curry

2007-01-19, 7:29 pm

In article <hob784-bkj.ln1@don.localnet>,
Bill Marcum <marcumbill@bellsouth.net> wrote:
>sed -e '/^$/d'


"grep ." is easier to type, easier to read, harder to get wrong; why is
sed '/^$/d' the popular idiom?

--
Alan Curry
pacman@world.std.com
Bill Marcum

2007-01-20, 1:27 am

On Fri, 19 Jan 2007 21:08:32 +0000 (UTC), Alan Curry
<pacman@TheWorld.com> wrote:
>
>
> In article <hob784-bkj.ln1@don.localnet>,
> Bill Marcum <marcumbill@bellsouth.net> wrote:
>
> "grep ." is easier to type, easier to read, harder to get wrong; why is
> sed '/^$/d' the popular idiom?
>

I was just correcting the sed command in the original post. "grep ." is
indeed easier.

--
"The lesser of two evils -- is evil."
-- Seymour (Sy) Leon
rolf randen

2007-01-22, 1:16 pm

rolf randen schrieb:
> hello there,
>
> i'm having a problem with a shell-script for removing empty lines from a
> logfile.
>
> i do
> sed -e '^$//'
> and get the error
> ": invalid command code ^
>
> this is the whole script:
>
> #!/bin/bash
> cat /Volumes/Save/daily.log | sed -e 's/xtar.*Icon..d.*directory//' |
> sed -e 's/xtar.*Icon.*d.*directory//' | sed -e 's/xtar.*Network Trash
> Folder.*denied//' | sed -e '^$//' | mail -s "backup-daily"
> logcheck@mailserver.local
>
> without removing the empty lines the script runs fine.
> i'm working with bash on macos 10.3.9
>
> please help!
>
> rolf



hey, unbelievable,

this is the first time i get useful answers in a newsgroup... and then a
whole bunch of it! thanks a lot to all of you!

rolf
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com