Monday, March 31, 2008

Convert DOS EOL (End of Line) to Linux EOL

This is useful when you do not have dos2unix command.
You can determine whether a file is having DOS End of Line.

$ od -c dos_file.txt | grep "\\\r" | wc -l
If you see any output other than 0, then this file
is having \r\n as new line (i.e DOS EOL).

1. Suppose you want to convert EOL of dos_file.txt . Open this
file using vim as:

$ vim dos_file.txt

2. If ^M (Ctrl-M) character is at end of every line, then this file
is in DOS format. We have to remove this ^M from the end
of line. Therefore, go to Edit mode by pressing Esc (Escape
button).

3. Type
:%s/^M$//g

And viola!, you will find that all ^M has gone from end of line.

4. Now, save the file and exit.

PS. In order to type ^M on screen, you should press Ctrl-v Ctrl-m
Otherwise this method will not work.

Alternatively, you can use sed to remove ^M from end of line, given
as following:

$ sed 's/^M$//g' dos_file.txt > temp
$ mv temp dos_file.txt

Swapping Two Consecutive Lines in Vim

1. Go to edit mode by pressing Esc (Escape Button).

2. Go to the line which you want to swap with the line following.
Example:
1 Singh Jat
2 Mitesh

3. Press dd, then press p. You will find the two line are swapped.
Example:
1 Mitesh
2 Singh Jat

Swaping Two Consecutive Characters in Vim

1. Go to edit mode by pressing Esc (Escape Button).

2. Go to the character which you want to swap with the character following.
Example: Mitseh

3. Press x, then press p. You will find the characters are swapped.
Example: Mitesh

Remembrall: Esc-x-p Transpose (X-Pose) characters