[sed] Replace a tab character using sed
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
$ 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.
[unix] Display the number of processors in the command line
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!
[unix] Compare the contents of two directories in the command line
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.
[vim] Save and use vim sessions
Save your vim session while working on a lot of files opened in different tabs or screens by using
:mksession saved_session.vimin 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.vimor if you want to start vim with the saved session, use this
$ vim -S saved_session.vimin 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.

leave a comment