Blogs Directory

Friday, October 11, 2013

Splitting a line into multiple lines and vice versa

This is the most common situation that we encounter in our work flows. I work in SOC team and there by need to do lot of text manipulations. Here i learnt that splitting a line into multiple lines and multiple lines into a single line using the method bellow.

Example 1:   (Text)

America
Africa
India
Germany

Use this command in the command mode of vim :%s/\n/ / 

The output would be like this :
 America Africa India Germany

Example 2: (Text)

America Africa India Germany

Use this command in the command mode of vim to split the line into multiple lines :%s/ /^M/g
% -> works on all the lines in the file
s -> substitute command
/ -> command divider
^M -> we get this by pressing <Ctrl>V followed by Enter.
g -> works on multiple occurrence of the pattern in the line.

No comments :

Post a Comment