LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Delete specific newlines on a file. (https://www.linuxquestions.org/questions/programming-9/delete-specific-newlines-on-a-file-4175510818/)

Tchiarot 07-11-2014 05:13 PM

Delete specific newlines on a file.
 
Hi Friends,

I have a procedure for delete some specific newline on a file.
For example with the file ex01:

Code:

Chiarot:teddy$cat ex01
Pencil1,101,102,103
Desktop2,201,104,106
nil,202,203,204,205,206,207
nilB,208,209,210,211,150,151,152,153,154,155,156
nilC,208,209,210,211,150,151,152,153,154,155,156
nilD,208,209,210,211,150,151,152,153,154,155,156

I should obtain:

Code:

sed
Chiarot:tmp$cat ex01
Pencil1,101,102,103
Desktop2,201,104,106
nil1,202,203,204,205,206,207,208,209,210,211,150,151,152,153,154,155,156

Now I using a sed and grep:

Code:

Chiarot:teddy$cat pr.sh
#!/bin/bash
egrep -v "nil" /tmp/ex01 > /tmp/ex01A
egrep -R "nil" /tmp/ex01 | sed -e ':a;N;$!ba;s/\n//g' -e 's/nil[B|C|D]//g' > /tmp/ex01B
cat /tmp/ex01A /tmp/ex01B > /tmp/ex01Final
rm /tmp/ex01A; rm /tmp/ex01B

It works.. but I'm trying to use the sed for made the change only in the file and be more efficient with temp files.

Some ideas to reduce this code?.

coralfang 07-11-2014 06:15 PM

You could store the temporary parts in variables, something like this:
Code:

#!/bin/bash
TMPVAR1=$(egrep -v "nil" /tmp/ex01)
TMPVAR2=$(egrep -R "nil" /tmp/ex01 | sed -e ':a;N;$!ba;s/\n//g' -e 's/nil[B|C|D]//g')
echo -e "$TMPVAR1\n$TMPVAR2" > /tmp/ex01Final

EDIT:
or even better..

Code:

#!/bin/bash
echo -e "$(egrep -v 'nil' /tmp/ex01)\n$(egrep -R 'nil' /tmp/ex01 | sed -e ':a;N;$!ba;s/\n//g' -e 's/nil[B|C|D]//g')" > /tmp/ex01Final


Tchiarot 07-11-2014 06:27 PM

That's a good idea, thanks coralfang.

It works very well, but there is no way to use only sed for replace nilB, nilC and nilD for nothing and paste this lines only with sed into the file?.
It could work even better than now.

Tchiarot 07-11-2014 07:06 PM

Finally find a even better solution:

Code:

sed -e '/^nil/{:a;N;$!ba;s/\n//g}' -e 's/nil[B|C|D]//g' /tmp/ex01 > /tmp/ex01Final
Can I execute it in the same file?, whithout use redirect?

Tchiarot 07-11-2014 07:18 PM

This is the final expression:

Code:

sed -i '/^nil/{:a;N;$!ba;s/\n//g;s/nil[B|C|D]//g}' /tmp/ex01
or:

Code:

sed -e '/^nil/{:a;N;$!ba;s/\n//g;s/nil[B|C|D]//g}' /tmp/ex01 > /tmp/ex01Final
Thx a lot for help.

danielbmartin 07-13-2014 07:47 PM

With this InFile ...
Code:

Chiarot:teddy$cat ex01
Pencil1,101,102,103
Desktop2,201,104,106
nil,202,203,204,205,206,207
nilB,208,209,210,211,150,151,152,153,154,155,156
nilC,208,209,210,211,150,151,152,153,154,155,156
nilD,208,209,210,211,150,151,152,153,154,155,156

... this awk ...
Code:

awk -F, '{if (substr($0,1,3)!="nil") print
  else for (j=2;j<=NF;j++) if (!index(ns,$j)) ns=ns","$j}
  END{print "NIL1" ns}' $InFile >$OutFile

... produced this OutFile ...
Code:

Chiarot:teddy$cat ex01
Pencil1,101,102,103
Desktop2,201,104,106
NIL1,202,203,204,205,206,207,208,209,210,211,150,151,152,153,154,155,156

Daniel B. Martin


All times are GMT -5. The time now is 08:19 PM.