Showing posts with label Vim. Show all posts
Showing posts with label Vim. Show all posts

11/08/2019

[Linux] Text handling for counting and replacing words

Here is a list of handy commands for VI and SED to count and replace text in a file. For both editors the escape character is /:

VI:

- count total words:

 :%s/\i\+/&/gn    


- count occurrences of particular word:

 :%s/WORD/&/gn    



SED:

- copy all text from marker until end of file:

 sed -n -n '/MARKER/,$p' in >> out  


- replace all occurrences of SOURCE to TARGET with optional characters before SOURCE:

 sed -E 's/(OPTIONAL)?SOURCE/\1TARGET/g' in >> out  

17/01/2015

[Vim] Remove empty lines from file

Once you learn the magic :q! combination to close Vim discarding changes, you find out that it is a quite powerful tool.

To remove empty lines from a file you might try with some specific commands such as:

:g/^$/d

Where g tells vim to execute a command only on the lines that match a regular expression, ^$ is said regular expression to match empty (blank) lines, and d is the delete command.

Bonus: if when you open the file you see a lot of ^M characters, it means you're editing it in Unix format but the file was created in DOS format. You can either try by telling vim to treat it as a DOS format before running the previous instruction:

:set ff=dos

or by converting the file to Unix format beforehand with the dos2unix command:

dos2unix -n in out