01-19-07 06: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.
[ Post a follow-up to this message ]
|