*NIX Tricks

[sed] Replace a tab character using sed

Posted in sed by kousik on May 7, 2012

Here’s how to replace all instances of TAB in a file input_file by, say comma (,), using sed

$ sed 's/<TAB>/,/g' input_file

But what is <TAB> above? On Linux systems you may just type \t (which is the regular expression for TAB) in place of <TAB>. However,  on some other systems (e.g., OSX with FreeBSD) it does not work. In cases where it doesn’t work, invoke <TAB> by hitting Control+v followed by the TAB key. This may alternatively be achieved by hitting Control+v followd by Control+i, as well.

References: ATOzTOA, Stack Overflow.

[perl] Search and replace text in multiple files from the command line

Posted in cli, linux, osx, perl, unix by kousik on January 28, 2010

$ perl -pi -w -e 's/foo/bar/g;' *.txt

-e means execute the following line of code.
-i means edit in-place
-w write warnings
-p loop

It will search for “foo” in files with extension .txt and replace it with “bar”.

Credit: here.

Tagged with: , , , ,

[unix] Display the number of processors in the command line

Posted in linux, osx, unix by kousik on January 16, 2010

The information about the CPU  is stored in /proc/cpuinfo. The following command will show you the number of processors by reading that file:

$ grep processor /proc/cpuinfo | wc -l

A simple
$ grep -c processor /proc/cpuinfo
should also be enough!

Tagged with: , , , , , , , , ,

[unix] Compare the contents of two directories in the command line

Posted in cli, unix by kousik on January 11, 2010

The first thing that comes to mind when you try to do any comparison, e.g. between two files or directories, is diff. Here is how it is used to compare two directories, dir-1 and dir-2:
$ diff /path_to_dir-1/ /path_to_dir-2/

But if the files are very big in size, diff may take quite a while.

Another useful utility is comm which is relatively fast for comparison of two files. It prints out the unique lines of file-1 in column 1, that of file-2 in column 2 and the common lines in column 3. The column 1, 2 and 3 may be suppressed by using the flags -1, -2 and -3, respectively. So, for example, if we want to see which files are common in dir-1 and dir-2, we may use comm with the “-1” and “-2” flags on the output of the ls command on the directories:
$ comm -1 -2 <(ls /path_to_dir-1/) <(ls /path_to_dir-2/)

Note: Of course, you can do a diff (or better vimdiff) of the outputs of ls as above, but comm still wins over diff/vimdiff because of the option to selectively output common or unique lines. Also notice that these forms are different from the diff mentioned at the top which does the comparison byte-by-byte.

Tagged with: , , , , , , ,

[vim] Save and use vim sessions

Posted in linux, osx, vim by kousik on November 2, 2009

Save your vim session while working on a lot of files opened in different tabs or screens by using

:mksession saved_session.vim

in one of the vim windows. Note that it does not have to have “.vim” extension.

After that you can do pretty much anything with your computer (close all windows, reboot, etc.) as long as you do not deleted the file saved above. You can restore the session while you are in vim by using:

:source saved_session.vim

or if you want to start vim with the saved session, use this

$ vim -S saved_session.vim

in the command line.

Application: (Directly copied from here)

The obvious way to use sessions is when working on different projects.
Suppose you store you session files in the directory "~/.vim".  You are
currently working on the "secret" project and have to switch to the "boring"
project:

	:wall
	:mksession! ~/.vim/secret.vim
	:source ~/.vim/boring.vim

This first uses ":wall" to write all modified files.  Then the current session
is saved, using ":mksession!".  This overwrites the previous session.  The
next time you load the secret session you can continue where you were at this
point.  And finally you load the new "boring" session.

Further reading: here and here.

Follow

Get every new post delivered to your Inbox.