The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 01-02-2002
Registered User
 

Join Date: Nov 2001
Location: New Zealand
Posts: 333
Red face Another 'VI' question

I want to know how I can strip and add blanklines from/to a text file with VI.

I have two scenarios that I am currently doing manually - and it's painful.

1) I need to strip any lines that have no characters on them at all..... (:set list shows me $)

and

2) I need to enter a blank line on every second line of a text file


I've tried the ole
:1,$ s/`\n`//g

and many other combinations - but without joy.... and I'm foreign to sed/awk. (although if theres a way I'd be happy to hear it).

Cheers
__________________
Pete
Forum Sponsor
  #2  
Old 01-03-2002
shaik786
Guest
 

Posts: n/a
Code:
1.)      Strip blank lines with the following command:
         grep "[^$]" in_file > out_file

2.)      :1,$s/$/^M
         This command is like this:
                   a)  Type 1,$s/$/ as in the normal way you would.
                   b)  Hold [CTRL+V] and then [CTRL+M]
                   c)  Ofcourse, hit [ENTER] :D
Hope this has helped you.

Rgds
SHAIK

Last edited by shaik786; 01-03-2002 at 04:00 AM.
  #3  
Old 01-03-2002
LivinFree's Avatar
Goober Extraordinaire
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
Wow, of all of the different ways I would have though to do #1, I would not have thought of `grep "[^$]"`... Here's some others that work similarly:
grep -v "^$"
sed /^$/d
egrep "^.+$"
....

And list could go on...

Boy, I LOVE Unix!
  #4  
Old 01-03-2002
Jimbo
Guest
 

Posts: n/a
And here is an awk solution that will strip blank lines, not only null lines but also empty lines containing only white space (spaces and/or tabs). It is selecting all lines where the number of fields (NF) on a line are greater than zero:

awk 'NF>0' in_file > out_file

And one awk solution to strip just the completely null lines:

awk 'length==0' in_file > out_file

And an awk solution to add a blank line after each line:

awk '{print $0;print ""}' in_file > out_file

Or to insert blank lines only where needed, considering that there might already be some blank lines:

awk '{if (NF>0&&prev>0) print "";print $0;prev=NF}' inf > outf

Last edited by Jimbo; 01-04-2002 at 01:23 PM.
  #5  
Old 01-03-2002
Registered User
 

Join Date: Nov 2001
Location: New Zealand
Posts: 333
Thumbs up Brilliant..

Thanks heaps for ALL those suggestions... I didn't think there was a way to do it - and I was right - there's about a million!
__________________
Pete
  #6  
Old 01-03-2002
J.P's Avatar
J.P J.P is offline
Registered User
 

Join Date: Nov 2001
Location: Sweden
Posts: 67
simple Elvis (also Vi?) solution

You can also use this command in Elvis, it should work in Vi too probably

:g/^$/d

This deletes all blank lines

/
JP
__________________
[ Please put signature here ]
  #7  
Old 01-03-2002
Registered User
 

Join Date: Nov 2001
Location: New Zealand
Posts: 333
Yep - tis good for VI as well.
__________________
Pete
Google The UNIX and Linux Forums
Closed Thread

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:48 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0