AWK / SED Introduction (Revision 140822-r01Ki)

Return to Lab 02

  1. Before doing this tutorial make sure you are in ~/[KELAS]/[NPM]/Lab02 directory by doing the command:
    cd ~/CLASS/NPM/Lab02

    Change CLASS with your class and NPM with your student ID.

SED Tutorial

  1. Do this command :
    vi cobased
  2. Fill out the cobased file with the below content:
    Saya adalah suatu tanda ? dalam hati
    Saya sering melewati # yang penuh * bersinar
    Saya tidak suka di () dalam [] seram
    Saya sering berjalan ' atau " dan
    Ya begitulah hidup............Sendiri... Sendiri... Sendiri.....
  3. Do this command in the vi command mode (you are entering command mode by pressing [ESC] and there will be a - on the bottom left of the vi)
    $ sed -i 's/\?/tanya/g' cobased
  4. The above command will change ? character to be "tanya" word. Now your job is to change these things in the file:
    # becoming pagar
    () becoming kurung
    [] becoming kotak
    " becoming berdua
    ' becoming sendiri
    * becoming bintang

    Remarks :for (') sign, you can use \x27.

AWK Tutorial

  1. Do this command:
    vi cobaawk
  2. Fill out the cobaawk file with:
    ls -l | awk '{
       FNAME=$9                                     # To get data in 9th field from ls -l (filename)
       FNAME2=FNAME                                 # FNAME2 for old name and FNAME for new name
       gsub (/\"/,"\\\"",FNAME2)                    # To handle problem with " and change it with \" 
       NCHG += gsub(/\x22/,"-",FNAME)               # To replace character " with -
       NCHG += gsub (/--+/,"-",FNAME)              # To replace more than 1 sequencial "-" to one "-" 
       NCHG += gsub (/-\./,".",FNAME)              # To replace character -. to only .
       NCHG += gsub (/^-/,"",FNAME)                # To delete - if in front of file name
       if (NCHG > 0) {                              # if there is change for the filename, filename will be replace with the command below
          system("/bin/mv \""  FNAME2 "\" " FNAME)      # rename oldfile to newfile
  3. Run the file:
    $ chmod 777 cobaawk
    $ touch coba\"awk
    $ ./cobaawk
  4. In the cobaawk program we replace /\x22/ becoming "-", /\x22/ means character no 22 (hexadecimal) in the ascii table which is *"*.
    Your task is to complete the above program that it can replace these caracters below to "-":
    , ? * #

    Ascii table can be seen at
  5. After completing the program, let's try whether you've done it correctly:
    $ touch "coba,2" 
    $ touch "coba?3" 
    $ touch "coba*4" 
    $ touch "coba#5" 
    $ ./cobaawk

Return to Lab 02
Kembali ke Wiki