Count the number of occurrences of a word in a file in vim

May 29, 2013
:%s/./&/gn		characters
	:%s/\i\+/&/gn		words
	:%s/^//n		lines
	:%s/the/&/gn		"the" anywhere
	:%s/\<the\>/&/gn	"the" as a word
Advertisements

How to set up autoindent “gg=G” automatically before any file is opened in Vim

May 7, 2013
  • Vim events : 
  • Vim autocommands : http://vimdoc.sourceforge.net/htmldoc/autocmd.html
  • :autocmd BufRead * :normal gg=G

How to use multiple versions of gcc ?

May 2, 2013

http://stackoverflow.com/questions/7832892/how-to-change-the-default-gcc-compiler-in-ubuntu/7834049#7834049


Redirect the output of gdb to a file

March 29, 2013

set logging file filename

http://www.slac.stanford.edu/comp/unix/package/rtems/doc/html/gdb/gdb.info.Logging_output.html

 


Automatically load bashrc on login

March 29, 2013

You can add the command “source .bashrc” in a file called .bash_profile which can be put in /home/sayghosh. That will automatically load your .bashrc on login and you wont need to load that separately everytime. 


Revising awk

January 15, 2013

Awk basics :

NR: Keeps a current count of the number of input lines.

• NF: Keeps a count of the number of words in an input line. The last field in the input line can be designated by $NF.

• FILENAME: Contains the name of the current input file.

• FS: Contains the field separator character. The default is “white space”, meaning space and tab characters. FS can be reassigned to another character to change the field separator.

• RS: Stores the current “record separator” character. Since, by default, an input line is the input record, the default record separator character is a “newline”.

• OFS: Stores the “output field separator”, which separates the fields when Awk prints them. The default is a “space” character.

• ORS: Stores the “output record separator”, which separates the output lines when Awk prints them. The default is a “newline” character.

• OFMT: Stores the format for numeric output. The default format is “%.6g”, which will be explained when “printf” is discussed

Awk examples :

  1.  Write a command to find the sum of bytes (size of file) of all files in a
    directory.
    ls -l | awk ‘BEGIN {sum=0} {sum = sum + $5} END {print sum}’

References :

  1. http://oreilly.com/catalog/unixnut3/chapter/ch11.html
  2. http://www.chemie.fu-berlin.de/chemnet/use/info/gawk/gawk_17.html

http://www.catonmat.net/blog/awk-one-liners-explained-part-one/


Options in the search and replace command in vi editor

June 5, 2012

1. What is the difference between s/old/new/ and s/old/new/g ?

The g option at the end of the line will replace all occurrences of “old” in a particular line with “new”.

If the g option is not used only the first occurrence of “old” in a particular line will be replaced with “new”.

2. What is the use of the “c” option in the use case  s/old/new/gc ?

This option will ask for an affirmation or negation before every replacement is made.

3. What does this mean :g/some text/s/search text/replacement text/ ?
“for all lines containing `some text’, search for `search text’ and replace any instances with `replacement text.'”

4. What does this mean :g!/some text/s/search text/replacement text/ ?
“for all lines not containing `some text’, search for `search text’ and replace any instances with `replacement text.'”
It is the same as :v/some text/s/search text/replacement text/

Reference :

http://sunsite.uakom.sk/sunworldonline/swol-10-1997/swol-10-unix101.html