Thursday, 23 April 2015

paste

Insert lines from files using Linux paste command:
------------------------------------------------------------------------

Input files:

$ cat contestant.txt
Christopher
Williams
Darwin
Ajay
Brain
Amay
Jiten
Lila

$ cat leader.txt
Mr B
Mrs C
Mrs A

Output required:

For every single line of 'leader.txt'; insert 3 lines from file 'contestant.txt'; so that the output looks like this:

Mr B
Christopher
Williams
Darwin
Mrs C
Ajay
Brain
Amay
Mrs A
Jiten
Lila


The step by step solution using Linux/UNIX paste command

$ cat contestant.txt | paste - - -
Output:
Christopher     Williams        Darwin
Ajay    Brain   Amay
Jiten   Lila

$ cat contestant.txt | paste - - - | paste leader.txt -
Output:
Mr B    Christopher     Williams        Darwin
Mrs C   Ajay    Brain   Amay
Mrs A   Jiten   Lila

$ cat contestant.txt | paste - - - |paste leader.txt - |tr "\t" "\n"
Output:
Mr B
Christopher
Williams
Darwin
Mrs C
Ajay
Brain
Amay
Mrs A
Jiten

Lila

Wednesday, 15 April 2015

du,df

Directory size excluding sub-directories - Linux:
---------------------------------------------------------------------

Directory '/home/user/work/demo/' contains a few regular files and two directories say "part2"(size=41236 KB) and "libs"(size=20620 KB).

$ du ~/work/demo/
41236   /home/user/work/demo/part2
20620   /home/user/work/demo/libs
87640   /home/user/work/demo/

From Linux/UNIX DU(1) command man page:

-s, --summarize
display only a total for each argument.

So, the following command is going to display the total size of the directory '/home/user/work/demo/'

$ du -s ~/work/demo/
87640   /home/user/work/demo/

Now, if you need to find the size of the '/home/user/work/demo/' directory excluding the size of the sub-directories, there is a command line option with DU(1):

-S, --separate-dirs
do not include size of sub-directories

So

$ du -S ~/work/demo/
41236   /home/user/work/demo/part2
20620   /home/user/work/demo/libs
25784   /home/user/work/demo/

Now,

$ du -S --max-depth=0 ~/work/demo/
25784   /home/user/work/demo/

or

$ du -S ~/work/demo/ | awk 'END {print}'

25784   /home/user/work/demo/

awk

please refer my another blog : Top examples of awk command in unix