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  

No comments:

Post a Comment

With great power comes great responsibility