Sunday, 4 September 2011

QuickBits-8: Remove Duplicate Lines in a File

To Remove duplicate lines in a file and print it
 cat filename | sort | uniq  

sort will sort the lines and
uniq will remove adjacent duplicate lines.

To count the number of repetition of each line
 cat filename | sort | uniq -c

Sunday, 28 August 2011

QuickBits-7: Convert .a file to .so file

.a files are just archive of .o object files. You need to just extract the files from the archive and package them as a shared object (.so).

ar -x myarchive.a


gcc -shared -Wl,-soname,mysharedlib.so *.o -o mysharedlib.so

Sunday, 21 August 2011

QuickBits-6: Search and Kill Process

Many a times we need to find a process and kill it. The best way to Find a process is

Search
 pgrep -l [pattern] 

-l will list the name of the process or else only pid will be displayed. pgrep searches for the match in any part of the name including the arguments to the command.

Example:
 pgrep -l work


output
217 kworker/u:3
218 kworker/u:4
219 kworker/u:5
863 NetworkManager
900 ./a.out work

Search & Destroy
 pkill [pattern]

It will find and kill all the process that matches the Pattern.


Sunday, 14 August 2011

QuickBits-5: Searching for previous commands

How to search and run the same command in bash ?

1. Up / down arrow keys [ good for previous few commands ]

2. Type ctrl+r and type any part of the command including its arguments. Bash will find and run it for you. Keep pressing ctrl+r until u find the right command.

Say you have run the following command sometime back

cd /myfolder/personal/secret/

you can search by typing ctrl+r and typing secret. Bash will find this command for you.

Thursday, 7 July 2011

QuickBits-4: sshfs

If you have ssh access to another PC and you want to access the folder of that PC the best way is to ssh mount that.
 sshfs username@hostname:/folder /mountfolder 


With this command the the /folder on the ssh host is mounted locally to /mountfolder. And you can access it as local folder.

To unmount the mounted folder

 fusermount -u /mountfolder 

Friday, 1 July 2011

QuickBits-3: Vim diff

Most often when you are working in shell you want to find diff between two files and merge them. vimdiff is very good utility.

 vim -d file1 file2 




Keyboard Shortcuts:



ctrl+w ctrl+w - switch windows
do - diff obtain
dp - diff put
[c - previous difference
]c - next difference
:diffupdate - diff update ;)
:syntax off - syntax off
zo - open folded text
zc - close folded text



Tuesday, 7 June 2011

A Boolean Array Puzzle

Input: A array arr[] of two elements having value 0 and 1

Output: Make both elements 0.

Specifications: Following are the specifications to follow.
1) It is guaranteed that one element is 0 but we do not know its position.
2) We can’t say about another element it can be 0 or 1.
3) We can only complement array elements, no other operation like and, or, multi, division, …. etc.
4) We can’t use if, else and loop constructs.
5) Obviously, we can’t directly assign 0 to array elements.




void changeToZero(int a[2])
{
a[ a[1] ] = a[ !a[1] ];
}

int main()
{
int a[] = {1, 0};
changeToZero(a);

printf("arr[0] = %d \n", a[0]);
printf("arr[1] = %d ", a[1]);
getchar();
return 0;
}