From: peter sands on
Hi,
I have the following two sed commands that delete tabs in a file then
deletes
empty lines. I am having to use two seperated
commands to do this including the move of the files.

Is there a way I can reduce the two sed commands into one sed command
to make it more efficent, or is there a way just to make the following
commands
more efficent.

sed 's/ //g' /tmp/pat.txt > /tmp/pat.tmp
mv /tmp/pat.tmp /tmp/pat.txt

sed '/^$/d' /tmp/pat.txt >/tmp/pat.tmp
mv /tmp/pat.tmp /tmp/pat.txt

thanks
pete.
From: Sidney Lambe on
On comp.unix.shell, peter sands <peter_sands(a)techemail.com> wrote:
> Hi,
> I have the following two sed commands that delete tabs in a file then
> deletes
> empty lines. I am having to use two seperated
> commands to do this including the move of the files.
>
> Is there a way I can reduce the two sed commands into one sed command
> to make it more efficent, or is there a way just to make the following
> commands
> more efficent.
>
> sed 's/ //g' /tmp/pat.txt > /tmp/pat.tmp
> mv /tmp/pat.tmp /tmp/pat.txt
>
> sed '/^$/d' /tmp/pat.txt >/tmp/pat.tmp
> mv /tmp/pat.tmp /tmp/pat.txt
>
> thanks
> pete.

sed -e <first script> -e <second script> inputfile > ....

If you have GNU sed you can use the -i option which will edit
the file in place with no need to send output to temp file and
mv it back.


Sid

From: pk on
peter sands wrote:

> I have the following two sed commands that delete tabs in a file then
> deletes
> empty lines. I am having to use two seperated
> commands to do this including the move of the files.
>
> Is there a way I can reduce the two sed commands into one sed command
> to make it more efficent, or is there a way just to make the following
> commands
> more efficent.
>
> sed 's/ //g' /tmp/pat.txt > /tmp/pat.tmp
> mv /tmp/pat.tmp /tmp/pat.txt
>
> sed '/^$/d' /tmp/pat.txt >/tmp/pat.tmp
> mv /tmp/pat.tmp /tmp/pat.txt

You can do (with a modern sed)

sed '/^$/d;s/\t//g' file.txt > tempfile.txt && mv tempfile.txt file.txt

if your sed doesn-t recognize \t in the LHS, you can enter it literally
(ctrl-V + tab)

If you have GNU sed, you can do away with the temp file by doing

sed -i '/^$/d;s/\t//g' file.txt

(it still uses a temp file, but it's all managed by sed).
From: pk on
pk wrote:

> sed '/^$/d;s/\t//g' file.txt > tempfile.txt && mv tempfile.txt file.txt

If, as I seem to understand, the removal of tabs may produce empty lines
which you want to remove, just swap the two commands:

sed 's/\t//g;/^$/d' file.txt > tempfile.txt && mv tempfile.txt file.txt

From: Stephane CHAZELAS on
2010-05-11, 04:23(-07), peter sands:
> Hi,
> I have the following two sed commands that delete tabs in a file then
> deletes
> empty lines. I am having to use two seperated
> commands to do this including the move of the files.
>
> Is there a way I can reduce the two sed commands into one sed command
> to make it more efficent, or is there a way just to make the following
> commands
> more efficent.
>
> sed 's/ //g' /tmp/pat.txt > /tmp/pat.tmp
> mv /tmp/pat.tmp /tmp/pat.txt
>
> sed '/^$/d' /tmp/pat.txt >/tmp/pat.tmp
> mv /tmp/pat.tmp /tmp/pat.txt
[...]

You could also do:

tr -d '\t'|grep .

or:

sed 's/ //g;/./!d'


--
Stéphane