You are not logged in.

#1 2020-02-06 19:07:40

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,043

[solved]sed, remove spaces from blank line

Is it possible to remove spaces from blank lines in possix?
They may even create a new blank line..

Say I have 3 blank lines and the second one contains N spaces.

The problem is they may only be removed from blank lines.
If they are preceding some 'text' they may not be removed..

I have tried things like

s/^[[:space:]]*$/&\n/;

and

/^ *$/d;

but, after removing all blank lines but one with

/^*$/N;/^\n$/D

all blank lines are removed while I want to keep at least one..

Last edited by qinohe (2020-02-06 20:24:19)

Offline

#2 2020-02-06 19:47:18

8472
Member
From: Slovakia
Registered: 2010-05-15
Posts: 68

Re: [solved]sed, remove spaces from blank line

What about only this?

s/^[[:space:]]*$//

So, if I create example as you said, three blank lines and second one has N spaces - input example file "test" has 23 bytes, while afterwards the "output" has only 3 bytes - 1byte/line:

$  sed 's/^[[:space:]]*$//' test > output; ls -la test output
-rw-r--r-- 1 username groupname 23 Feb  6 20:43 test
-rw-r--r-- 1 username groupname  3 Feb  6 20:44 output

Logic clearly dictates that the needs of the many outweigh the needs of the few.

Offline

#3 2020-02-06 19:59:49

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,043

Re: [solved]sed, remove spaces from blank line

Thanks, Yes, I get the same result using

sed -e 's/^[[:space:]]*$//' ~/.scripts/tests/space | sed '/^$/N;/^\n$/D' > ~/.scripts/tests/sp

only one line is left which is good.
when however using this strategy on a + 20 lines sed script it fails, I try to edit the result afterwards, see where it gets me.

Offline

#4 2020-02-06 20:02:46

8472
Member
From: Slovakia
Registered: 2010-05-15
Posts: 68

Re: [solved]sed, remove spaces from blank line

If you intend to apply this globally, use G for global:

s/^[[:space:]]*$//g

This should process the whole file with such data.


Logic clearly dictates that the needs of the many outweigh the needs of the few.

Offline

#5 2020-02-06 20:09:04

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,043

Re: [solved]sed, remove spaces from blank line

I do already hmm

edit;

I got it it's ugly though..

sed -e'....
sed lines
...'  ~/.scripts/tests/space |  sed 's/^[[:space:]]*$//g' > ~/.scripts/tests/sp
 sed -i '/^$/N;/^\n$/D'  ~/.scripts/tests/sp

Thanks for the help;)

Last edited by qinohe (2020-02-06 20:18:03)

Offline

Board footer

Powered by FluxBB