Friday, January 29, 2016

Linux: tar Extract Files

Extract or Unpack a TarBall File

To unpack or extract a tar file, type:tar -xvf file.tar
To save disk space and bandwidth over the network all files are saved using compression program such as gzip or bzip2. To extract / unpack a .tar.gz (gzip) file, enter (note -z option): tar -xzvf file.tar.gz
To extract / unpack a .tar.bz2 (bzip2) file, enter (note -j option):tar -xjvf file.tar.bz
Where,
  • -x : Extract a tar ball.
  • -v : Verbose output or show progress while extracting files.
  • -f : Specify an archive or a tarball filename.
  • -j : Decompress and extract the contents of the compressed archive created by bzip2 program (tar.bz2 extension).
  • -z : Decompress and extract the contents of the compressed archive created by gzip program (tar.gz extension).

How Do I Extract A Single File Called foo.txt?

To extract a single file called foo.txt, enter:tar -xvf file.tar foo.txttar -xzvf file.tar.gz foo.txttar -xjvf file.tar.bz2 foo.txt 
You can also specify path such as etc/resolv.conf, enter:tar -xvf file.tar etc/resolv.conftar -xzvf file.tar.gz etc/resolv.conftar -xjvf file.tar.bz2 etc/resolv.conf 

How Do I Extract a Single Directory Called etc?

To extract a single directory called etc, enter:tar -xvf file.tar etctar -xzvf file.tar.gz etctar -xjvf file.tar.bz2 etc

Thursday, January 21, 2016

Creating TextTables and PrettyTables using Python

There are some light and useful python packages for this purpose:
>>> from tabulate import tabulate
>>> print tabulate([['Alice', 24], ['Bob', 19]], headers=['Name', 'Age'])
Name      Age
------  -----
Alice      24
Bob        19
tabulate has many options to specify headers and table format.
>>> print tabulate([['Alice', 24], ['Bob', 19]], headers=['Name', 'Age'], tablefmt='orgtbl')
| Name   |   Age |
|--------+-------|
| Alice  |    24 |
| Bob    |    19 |
>>> from prettytable import PrettyTable
>>> t = PrettyTable(['Name', 'Age'])
>>> t.add_row(['Alice', 24])
>>> t.add_row(['Bob', 19])
>>> print t
+-------+-----+
|  Name | Age |
+-------+-----+
| Alice |  24 |
|  Bob  |  19 |
+-------+-----+
PrettyTable has options to read data from csv, html, sql database. Also you are able to select subset of data, sort table and change table styles.
>>> from texttable import Texttable
>>> t = Texttable()
>>> t.add_rows([['Name', 'Age'], ['Alice', 24], ['Bob', 19]])
>>> print t.draw()
+-------+-----+
| Name  | Age |
+=======+=====+
| Alice | 24  |
+-------+-----+
| Bob   | 19  |
+-------+-----+
with texttable you can control horisontal/verical align, border style and data types.
Other options:
  • terminaltables Easily draw tables in terminal/console applications from a list of lists of strings. Supports multi-line rows.
  • asciitable Asciitable can read and write a wide range of ASCII table formats via built-in Extension Reader Classes.


Make sure to install packages using pip.

Python - Print list of temperatures between Celsius and Fahrenheit

# convert.py
#   This program will convert a finite set of temperatures from Celsius to Fahrenheit
#   starting from 0 to 90 increments of 10 deg C.
#   The data is organized and printed using the Texttable module.

from texttable import Texttable

def main():
    cList = []
    fList = []
    for i in range(0,100,10):
        fahrenheit = 9/5 * i + 32
        cList.append(i)
        fList.append(fahrenheit)
       
    t = Texttable()
    for x in range(len(cList)):
        t.add_rows([['Temperature Reading Index', 'Celsius','Fahrenheit'], [x, cList[x],fList[x]]])
    print(t.draw())

if __name__ == "__main__":
    main()