LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-11-2014, 05:13 PM   #1
Tchiarot
Member
 
Registered: Feb 2013
Location: Lima, Peru
Distribution: Debian
Posts: 39

Rep: Reputation: Disabled
Question 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?.

Last edited by Tchiarot; 07-11-2014 at 05:27 PM.
 
Old 07-11-2014, 06:15 PM   #2
coralfang
Member
 
Registered: Nov 2010
Location: Bristol, UK
Distribution: Slackware, FreeBSD
Posts: 836
Blog Entries: 3

Rep: Reputation: 297Reputation: 297Reputation: 297
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

Last edited by coralfang; 07-11-2014 at 06:18 PM.
 
1 members found this post helpful.
Old 07-11-2014, 06:27 PM   #3
Tchiarot
Member
 
Registered: Feb 2013
Location: Lima, Peru
Distribution: Debian
Posts: 39

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-11-2014, 07:06 PM   #4
Tchiarot
Member
 
Registered: Feb 2013
Location: Lima, Peru
Distribution: Debian
Posts: 39

Original Poster
Rep: Reputation: Disabled
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?

Last edited by Tchiarot; 07-11-2014 at 07:18 PM.
 
Old 07-11-2014, 07:18 PM   #5
Tchiarot
Member
 
Registered: Feb 2013
Location: Lima, Peru
Distribution: Debian
Posts: 39

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-13-2014, 07:47 PM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
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

Last edited by danielbmartin; 07-14-2014 at 06:07 AM. Reason: Tighten the code, slightly
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Delete Unix/Linux file at a specific time in future. abhi7514 Linux - Newbie 4 09-15-2012 08:41 PM
Find folders with ONLY one specific file in and delete file and folder - How ? gedi1 Linux - Newbie 5 10-21-2009 08:09 PM
Delete specific lines of a file beginning with a certain letter docaia Programming 4 08-24-2008 11:04 PM
Keep specific file types, delete the rest ? jchambers Programming 5 11-26-2007 06:25 PM
how to insert newlines into a dd-converted ascii file bryan.out.there Linux - General 2 09-13-2006 11:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration