Introduction to LinuxUnix by ozb45831

VIEWS: 6 PAGES: 37

									Introduction to Linux/Unix
• Login

• Moving through the file folder structure
  – Permissions

• The vi editor
  – Creating a new file
  – Basic commands
     • Deleting line, Sorting columns etc…

• AWK
  – Set up, writing, executing
 Login
• Couple of ways to login to the cluster
  – Linux workstation
  – PuTTY
  – Samba server
File Folder Structure
                        Linux folder structure
                        is similar to Windows
                        but uses a command
                        line using terminal
                        windows
• You can use the Graphical User Interface (GUI)
  with Linux but most common approach is through
  terminal window

• To open a terminal in Linux right click and select
  open terminal
   – If working in PuTTY you will need to start a new
     session for each terminal window

• There are two commands that tell you where you
  are at in the file structure
   – pwd and ls
   – pwd will tell you the path of the current directory
   – ls will show you what folders are in the current
     directory
• After login the default directory is open

• Most programs need to be run in a working
  directory
                    Basic Commands
• For complete list see “Linux in a Nutshell”
   –   pwd (identifies current directory)
   –   cd (change directory) and cd .. (up one directory),
   –   cd ../.. (up two directories)
   –    ls (looks in current directory) ls –l (looks in current directory
       and lists all permissions)
   –   mkdir (make a new directory)
   –   rm remove/delete file
   –   rm –R remove/delete folder
   –   mv and mv –R moves files and folders
   –   cp and cp –R copies files and folders
   –   ps and ps –a (looks for all processes running)
   –   kill (will kill a process running)
   –   man command (gives information about a command)
        • man wc
            Basic Commands
• Example
  – Copy awk script from Chem991A folder to FAST-
    NMR Directory
               Pipes and Redirection
• | (pipe)
   – Passes output of one Linux command to input of
     second command
   – ls |wc
      • Passes output of ls command to wc to get file count of
        directory
      • Not limited to just one pipe can string more pipes together
• >, < redirection
   – > filename (instead of display on a screen send
     information to a file)
      • ls > file_list
   – program < filename (passes content of a file to a
     program for execution)
      • xplor < psf.inp
        Background calculations
• For long calculations don’t want the process
  associated with the window
  – Calculations can be stopped or slow down your CPU
  – Window must remain open and active during
    calculation
  – Output displays on window can be lost, lock window
    or crash CPU
• Background
  – xplor < psf.inp > psf.out &&
• Open window
  – xplor < psf.inp
The vi editor
• The vi editor the basic text editor in linux

• Opens any text based file including awk scripts

• Command to open vi
  – vi filename
The vi editor
• To remove one line of text
  – dd
• To search document
  –/
The vi editor cont.
• To start vi just type vi
   – Will create a new file
• To open a file with vi
   – vi filename
• To close vi
   – :q
   – :wq filename
   – :q!
   – :wq! (will save to current filename)
The vi editor cont.
• Pattern matching
  – / searches file for pattern
     • Example /NH
  – /[string] searches for all occurrences of a
    character within brackets
     • Example /[23]
  – \< and \> Searches for specific parts of a word or
    string
     • Example /\<short
     • Example/ridge>
The vi editor cont.
• Sorting files
   – sort –rn $column filename > newfilename
      • Example Sort pks.asg file by CA and Cai-1


• Very useful when working on peak
  assignments for protein structure
AWK
   Awk
• There are three primary steps in an awk script
  – Deciding what to do
  – Script writing
  – Running the script
Awk –Script Decision
• Deciding what to write can be a challenging
  first step

• Many times the output from a program is
  used as the input of a second program
                           BEGIN




              logic statements (if, and, or, not)
              arithmetic
              looping
INPUT         table arrays                          OUTPUT
              printing




                            END



    awk –f awk_script.awk infilename > outfilename
         BEGIN


• Often the first few lines   # This script politely introduces itself
  of a script are not used    BEGIN {
  by the program but          print “Hello, world”
                              }
  simply states what the
  program does


• To comment out a line
  of text from a script add
  “#” before text
                                          BEGIN {
         BEGIN                                        CAmax[0]= "65.52"
                                                      CAmin[0]= "43.00"
                                                      CBmax[0]= "38.70"
                                                      CBmin[0]= "0.00"
• The first part of the                   {
                                                      Res[0]="A"

  script also sets the
                                          PkID=$1
  variables for the data                  NH=$2
  and defines fields                      N15=$3
                                          CAiatom=$4
                                          CBiatom=$5
                                          CAatom=$6
                                          CBatom=$7
                                          COi=$8
  PkID    NH     N15      CA      CB          CAi      CBi      COi       ResID
  1.00    9.35   126.75   53.19   40.06       63.53    69.87    172.90
  2.00    9.10   126.69   59.42   31.90       52.92    43.03    174.94
  3.00    9.73   126.68   60.73   38.11       54.64    31.38    171.92
  4.00    7.80   126.57   57.28   33.99       56.10    30.75    172.60
  5.00    8.84   126.52   58.35   28.58       53.25    40.03    173.12
  6.00    8.14   125.85   65.85   31.89       53.03    41.07    173.12
  7.00    9.01   125.35   62.57   42.15       52.70    41.84    171.99
  8.00    8.15   125.24   54.86   40.69       55.79    30.35    171.17
     Functions

• The various functions of AWK performs the
  tasks you want
• The primary functions can be grouped into 5
  categories
  – Logic statements
  – Arithmetic
  – Looping
  – Arrays
  – Printing
     Functions

• Logic statements
  – if (a general call that tests a parameter within the input
    file)
  – Probably most important logic command
  – General call structure is
     • if (statement to test) {action}
     • Example: if ($1 == “HAPPY”)
         – Reads “if column 1 equals HAPPY”
         – If this is true then we can do something to that column
  – Opposite of if is else
     • Used to perform an action when the if statement is not true
     • else {action}
  – Example script “happysad.awk”
     • awk –f happysad.awk happysad_in.txt > hs_out.txt
     Functions
• Logic statements
  – not (!) this is a general statement to be used as saying
    “not that”
     • Example: if ($1 !=“HAPPY”) sadhappy.awk
     • awk –f sadhappy.awk sadhappy_in.txt > sh_out.txt

  – and (&&) will perform a function only if both
    conditions are met
     • Example: if ($1 > $2 && $1 > $3)
     • awk –f and.awk and_in.txt > and_out.txt

  – or (||) will perform a function multiple conditions are
    met
     • Example: if ($1 > $2 || $1 < $3)
     • awk –f or.awk and_in.txt > or_out.txt
     Functions

• Arithmetic statements
  – addition (+) can add any number or columns
    defined by logic statements or on its own
     • Example: y = x + 1
     • awk –f add.awk
     • awk –f add.awk addmore_in.txt > addmore_out.txt


  – subtraction (-) can subtract any number or
    columns defined by logic statements or on its own
     • Example: y = x -1
                       Formatting outputs
  • Many programs have very specific file formats
    they use to work properly
       – VMD
                               BAD          GOOD



Non-sense points in
XYZ due to poor
formatting, improper
number of spaces
between columns
              Formatting outputs
• The printf statement has a basic structure
  – printf (“format-expression”, arguments)

  – format-expression is a string of one or many
    formatting specifiers generally preceded by the % sign
  – “%-width.precision format-specifier”
     • (-) is the justification command and will left justify an output
     • width would be how many characters to print before a
       decimal (.)
     • precision is how my characters to print after a decimal
     • format-specifier is a command that tells awk what to print

  – printf (“%s\n”, $1)
      • prints the output string (%s) from column $1 and then prints
        a new line (\n)
            Formatting outputs
• The printf statement has a basic structure
  – printf (“format-expression” , arguments)

  – arguments is what you want to print
     • Need a specifier for each argument
     • printf (“%s%s\n”, $1,$2)
        – print all the characters in column 1 (%s) and column 2 (%s)
          then print new line (\n)
        – The % is used as a literal meaning and gives no spacing
          information so the stings from column 1&2 will be right next
          to each other
             » $1 = HAPPY and $2 = SAD the output would be HAPPYSAD
             Formatting outputs
• Spacing
  – You have to specify the amount of spacing you want
    between characters
     • printf (“%s    %s\n”, $1,$2)
         – Will print the number of spaces (5) you provide between strings
     • printf (“%s%10s\n”, $1$2)
         – Will print a specified number of spaces (10)



• Tabs
  – Want to replace “spaces” in the file with a single tab
     • printf (“%s\t%s\n”, $1, $2)
         – Will print a tab (\t) between the two columns
             Formatting outputs
• Justification
  – By defult the printf command prints everything
    right justified
     • printf (“|%10s|\n”, “hello”)
        –|    hello|


  – To left justify your output you add the (-) minus
    sign before your format speifier
     • printf (“|%-10s|\n”, “hello”)
        – |hello   |
            Formatting outputs
• Floating point operators
  – Again the % will print everything in a string but
    what happens when you have too many significant
    figures? 10.0000000000000000032?
     • printf (“%s\n”, $1)
        – print the entire string of characters

     • printf (“%.2f\n”, $1)
        – Floating point operator (f) prints only the number of
          significant figures (2) that follow the decimal (.)

     • printf (%.2e\n”, $1)
        – A second floating point operator (e) will convert the number
          into scientific notation with a specified number of sigfigs
             Formatting outputs
• Nesting printf with other commands
  – Often printf statements come at the end of a
    different command
     • if ($1 == $3) {
         printf (“%s\n”, $3)
         }
  – The printf statement needs to be grouped by its
    self within a command using {&}

  – Example HAPPYSAD.awk
                Formatting outputs
• Example HAPPYSAD.awk
  {
  if ($1 == “HAPPY” {
             print “SMILE”
             }
  else {
             print “FROWN”
  }
  }

• This awk read column $1 and if it equaled (==)
  HAPPY it printed the word SMILE
  – Used the basic print command print which
    automatically makes a new line but no other
    formatting
                Formatting outputs
• Example HAPPYSAD.awk with printf
  {
  if ($1 == “HAPPY” {
             printf (“%s\n”, “SMILE”)
             }
  else {
             printf (“%s\n”, “FROWN”)
  }
  }


• If we use printf we have to specifiy the string
  (%s) and the new line (\n) to get the same
  result
  – Might seem like extra work but it is worth it
                Formatting outputs
• Example HAPPYSAD.awk
  {
  if ($1 == “HAPPY” {
             printf (“%s%s\n”, $1,“=SMILE”)
             }
  else {
             printf (“%s%s\n”, $1,“=FROWN”)
  }
  }


• We can add to the original values in $1 using
  the printf
  – This will HAPPY=SIMILE for every time $1 ==
    HAPPY
                Formatting outputs
• Example HAPPYSAD.awk
  {
  if ($1 == “HAPPY” {
             printf (“%s\t%s\t%s\n”, $1,“=“, “SMILE”)
             }
  else {
             printf (“%s\t%s\t%s\n”, $1,“=“, “FROWN”)
  }
  }


• We can then format using the tab (\t) or
  spacing ( )
  – This will HAPPY                 =         SIMILE for every time
    $1 == HAPPY
         AWK – Final Thoughts


• For complete list of all the commands used for
  printf see the sed&awk manual pg164-166

								
To top