Vim Tips Wiki
m (Save each line of a text in separated numbered files moved to Save each line in separate numbered files: Page moved by JohnBot to improve title)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
  +
{{TipImported
{{review}}
 
{{Tip
 
 
|id=1059
 
|id=1059
  +
|previous=1058
|title=Save each line of a text in separated numbered files
 
  +
|next=1061
|created=November 25, 2005 16:26
+
|created=2005
 
|complexity=basic
 
|complexity=basic
|author=Walter L�pez
+
|author=
 
|version=6.0
 
|version=6.0
 
|rating=5/2
 
|rating=5/2
  +
|category1=
|text=
 
  +
|category2=
Create this function
 
 
 
 
function! SaveLines()
 
 
execute line(".") . "w " . line(".") . ".xml"
 
 
endf
 
 
 
 
And call it
 
 
 
 
%call SaveLines()
 
 
 
 
The generated files are:
 
 
1.xml
 
 
2.xml
 
 
...
 
 
 
 
each containing a single line.
 
 
}}
 
}}
  +
A file may consist of lines of text, where each line is some meaningful record of information. This tip shows how to save each line to a separate file (one line per file).
   
  +
The following command saves each line in the current buffer to a separate file:
== Comments ==
 
  +
<pre>
wow. just what we wanted
 
 
:g/^/execute '.w '.line('.').'.txt'
 
  +
</pre>
Dingo
 
, November 25, 2005 23:39
 
----
 
No need for a function...you can just use
 
 
:g/^/exe ".w ".line(".").".txt"
 
 
to get the same sort of effect.
 
 
vim.tip [at] the chases {dot} com
 
, November 26, 2005 5:55
 
----
 
:g/^/exe ".w ".line(".").".txt"
 
   
  +
The first line is saved in file <code>1.txt</code>, and the second line is saved in file <code>2.txt</code>, and so on. If a file exists, it is not overwritten—the command stops with an error message, and does not attempt to write any further files. If wanted, replace <code>.w</code> with <code>.w!</code> to force files to be overwritten.
Explanation:-
 
:g/^/ &#35; for very line
 
exe &#35; Execute the following string as an ex command ie :
 
   
  +
The following command is similar, but it only saves non-empty lines (empty lines are skipped):
.w &#35; . means current line so .w means write current line
 
  +
<pre>
line(".") &#35; returns current line number
 
 
:g/./execute '.w '.line('.').'.txt'
  +
</pre>
   
 
Explanation:
The string generated (for first line is) is
 
:g/^/exe ".w 1.txt"
+
;<code>:g/^/...</code>
  +
:The [[Power of g|global]] command performs a command (shown as <code>...</code> here) on each line that matches the pattern <code>^</code>. Since that pattern matches the start of a line, it matches every line, and whatever command is used instead of <code>...</code> will be performed on each line.
  +
;<code>execute ...</code>
  +
:This evaluates the following expression, then executes the result as a command.
  +
;<code>.w</code>
  +
:The <code>w</code> is the write command, and the <code>.</code> is a [[Ranges|range]] that refers to the current line, so the command writes only the current line.
  +
;<code>line('.').'.txt'</code>
  +
:<code>line('.')</code> gives the current line number, which is concatenated with the string <code>.txt</code>.
   
  +
For the first line, <code>:g/^/</code> executes the command <code>execute '.w 1.txt'</code> which writes the current line (the first line) to file <code>1.txt</code>.
zzapper
 
, November 28, 2005 9:15
 
----
 
Great, without the function is better to do. Thanks!!
 
   
  +
If only some lines are wanted, an appropriate search pattern can be used. For example, the following command operates on all lines that start with "Error". In each case, three lines (the line before "Error", the "Error" line, and the line after) are written to a file named <code>N.txt</code> where <code>N</code> is the number of the line containing "Error".
wjlc--AT--internet.com.uy
 
  +
<pre>
, November 30, 2005 11:01
 
 
:g/^Error/execute '.-1,.+1w '.line('.').'.txt'
----
 
  +
</pre>
vim is better than gmacs
 
   
 
==Comments==
parth007--AT--yahoo.com
 
, December 2, 2005 22:38
 
----
 
<!-- parsed by vimtips.py in 0.576481 seconds-->
 

Latest revision as of 12:33, 15 July 2012

Tip 1059 Printable Monobook Previous Next

created 2005 · complexity basic · version 6.0


A file may consist of lines of text, where each line is some meaningful record of information. This tip shows how to save each line to a separate file (one line per file).

The following command saves each line in the current buffer to a separate file:

:g/^/execute '.w '.line('.').'.txt'

The first line is saved in file 1.txt, and the second line is saved in file 2.txt, and so on. If a file exists, it is not overwritten—the command stops with an error message, and does not attempt to write any further files. If wanted, replace .w with .w! to force files to be overwritten.

The following command is similar, but it only saves non-empty lines (empty lines are skipped):

:g/./execute '.w '.line('.').'.txt'

Explanation:

:g/^/...
The global command performs a command (shown as ... here) on each line that matches the pattern ^. Since that pattern matches the start of a line, it matches every line, and whatever command is used instead of ... will be performed on each line.
execute ...
This evaluates the following expression, then executes the result as a command.
.w
The w is the write command, and the . is a range that refers to the current line, so the command writes only the current line.
line('.').'.txt'
line('.') gives the current line number, which is concatenated with the string .txt.

For the first line, :g/^/ executes the command execute '.w 1.txt' which writes the current line (the first line) to file 1.txt.

If only some lines are wanted, an appropriate search pattern can be used. For example, the following command operates on all lines that start with "Error". In each case, three lines (the line before "Error", the "Error" line, and the line after) are written to a file named N.txt where N is the number of the line containing "Error".

:g/^Error/execute '.-1,.+1w '.line('.').'.txt'

Comments[]