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

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


Substitute (Search and replace) command in the vi editor

June 5, 2012

1. How to replace an occurrence of “old_string” by “new_string” in a file

{code} :s/old_string/new_string/g{/code}

2. How to replace an occurrence of “old_string” by “new_string” in a file withinthe range of lines 1 to 10

:1,10s/old_string/new_string/g
3. How to replace an occurrence of “old_string” by “new_string” in a file withinthe range of lines 1 to the current line ?

The current line (where the cursor is located) can be specified as a single dot (.).

:1,.s/old_string/new_string/g

4. How to replace an occurrence of “old_string” by “new_string” in a file withinthe range of lines from the current line to the last line?

The last line can be specified as a dollar sign ($).

:.,$s/old_string/new_string/g

5. How do you search the whole file ?

:%s/old_string/new_string/g or
:1,$s/old_string/new_string/g
6. How can you execute a search and replace on the current line and the next five lines ?

The beginning or ending line for a range may be given as a positive or negative number of lines offset from the current line.

:.,+5s/old_string/new_string/g

7. How can you execute a search and replace from five lines above the current line through the current line ?

:-5,.s/old_string/new_string/g

8. How can you perform a search and replace from five lines above the current line through five lines below the current line ?

:-5,+5s/old_string/new_string/g

 


Useful Screen Commands

May 22, 2012

screen -list : display a list of all the screens
screen -r : reconnect to a screen
screen -x : reattach to a screen that is already attached
C-c – Create a new screen window
C-n – shift to the next screen
C-p – shift to the previous screen

C-a C-w – see the list of windows connected to this screen

Click to access screen.cheat.sheet.pdf


Redirection and File Descriptors

April 25, 2012

Standard File Descriptors in Unix :

  • Stderr : 2
  • Stdin : 0
  • Stdout : 1

Redirection Examples :

Example 1 :

bash$ echo “Hello” > a.txt

is the same as

bash$ echo “Hello” 1> a.txt

Example 2 :

bash$ cat < a.txt means use the contents of the file when cat reads from stdin which is the same as doing
bash$ cat 0< a.txt

In all the cases the redirection occurs before the echo or the cat command is executed.

Redirections operations all happen from left to right and pipe has precedence over redirection.