![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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
Rgds SHAIK Last edited by shaik786; 01-03-2002 at 04:00 AM. |
|
#3
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
Yep - tis good for VI as well.
__________________
Pete |
|||
| Google The UNIX and Linux Forums |