Useful Unix/Linux Commands for a DBA

Moderator: NorbertKrupa

Post Reply
User avatar
JimKnicely
Site Admin
Site Admin
Posts: 1815
Joined: Sat Jan 21, 2012 4:58 am
Contact:

Useful Unix/Linux Commands for a DBA

Post by JimKnicely » Wed Aug 01, 2012 4:49 pm

1) PASTE - The Paste utility is a very basic tool for creating either rows or columns of data that are combined from two separate files.

Example:

Code: Select all

$ cat > file1.txt
id
1
2
3
4
5

$ cat > file2.txt
value
foo
bar
baz
egg
sax

$ paste ./file{1,2}.txt
id   value
1    foo
2    bar
3    baz
4    egg
5    sax
2) JOIN - We can use the JOIN command to join lines from two files on a common field

Example:

Code: Select all

$ cat file1.txt
Name:
John
Anna
Josh
Dick
Hope

$ cat file2.txt
TEL:
555-456123           
555-987654
555-668844
555-997733
555-147852

$ cat file1.txt | nl    # number rows example
     1    Name:
     2    John
     3    Anna
     4    Josh
     5    Dick
     6    Hope

$ cat file1.txt | nl > file_A
$ cat file2.txt | nl > file_B

$ join -1 1 -2 1 file_{A,B} # join by first field from "left" file [-1 1] and by first field from "right" file [-2 1]
1  Name: TEL:
2  John  555-456123
3  Anna  555-987654
4  Josh  555-668844
5  Dick  555-997733
6  Hope  555-147852
3) We can use the the cut and awk commands to limit the columns displayed in an output file:
  • cut [-d --- specifies delimiter, default tab; -f --- fields to output ]
    awk [-F --- specifies delimiter, default white space(tab, space); $# --- fields to output ]
Examples:

Code: Select all

$ echo -e "1|2|3|4|5|6\na|b|c|d|e|f"
1|2|3|4|5|6
a|b|c|d|e|f

$ echo -e "1|2|3|4|5|6\na|b|c|d|e|f" | cut -d"|" -f3-5
3|4|5
c|d|e

$ echo -e "1|2|3|4|5|6\na|b|c|d|e|f" | awk -F"|" '{print $3"-"$4"-"$5}'
3-4-5
c-d-e
Have fun!
Jim Knicely

Image

Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.

Post Reply

Return to “Vertica and the Operating System”