Saturday, June 29, 2013

My AWK Code for Parsing Physic Exercise

Bismillahirrahmanirrahim.

I have a project for translating an exercise in Physic into known variables. I use awk only for do that. This is the exercise example in Indonesian:

Dari salah satu bagian gedung yang tingginya 20 m, dua buah batu dijatuhkan secara berurutan. Massa kedua batu masing-masing 1/2 kg dan 5 kg. Bila percepatan gravitasi bumi di tempat itu g = 10 m/s2, tentukan waktu jatuh untuk kedua batu itu (Abaikan gesekan udara). 

I should translate it into known variables (diketahui -Indonesian) like this:

Tinggi = 20 m
Massa 1 = 1/2 kg
Massa 2 = 5 kg
Gravitasi = 10 m/s2

Actually, I have tried awk and stuck on this code for getting numbers.

{
  for(i=1; i<=NF; i++){   
    if($i ~ /^[[:digit:]]+/) 
      print $i     
  }
}

And this second code for getting units (like m, kg, m/s2).

{  
  for(i=1; i<=NF; i++){      
  if(($i ~ /^m\/s2/) || ($i ~ /^kg$/) || ($i ~ /^m$/))    
      print $i       
  }
}

And I have tried to join those two codes into one.

BEGIN { FS = "[, ]+" }      

#getting units

{  
  for(i=1; i<=NF; i++){      
  if(($i ~ /^m\/s2/) || ($i ~ /^kg$/) || ($i ~ /^m$/))    
      print $i         
  }
}

#getting numbers

{
  for(i=1; i<=NF; i++){    
    if($i ~ /^[[:digit:]]+/) 
      print $i      
  }
}

Result
master@master:~/Dokumen/Pelajaran/Semester 4/Pak Anom$ awk -f plasma.awk soal1 
m
20
kg
m/s2
1/2
5
10
master@master:~/Dokumen/Pelajaran/Semester 4/Pak Anom$ 

But all fail. What makes me fail? Because I don't understand awk syntax and logic. After I asked Stackoverflow (you can see my question at http://stackoverflow.com/questions/17312343/parsing-physic-exercise-in-awk), two or five minutes later I get the answer. So quick. The best code was this:

{
    for(i=1;i<=NF;i++) {
        gsub(/[,.]/,"",$(i+1))
        if($i~/^[[:digit:]]/ && $(i+1)=="m") {
            print "Height = "$i,$(i+1)
        }
        else if($i~/^[[:digit:]]/ && $(i+1)=="kg") {
            print "Mass "++x" = "$i,$(i+1)
        }
        else if($i~/^[[:digit:]]/ && $(i+1)=="m/s2") {
            print "Gravity = "$i,$(i+1)
        }
    }
}

Result
Height = 20 m
Mass 1 = 1/2 kg
Mass 2 = 5 kg
Gravity = 10 m/s2

Short Analysis 
My first code works in my thought baseline. I should scan all field (read: column) and save every founded pattern into variable then print the variable content. But the problem, main problem is I don't understand how to use variable in awk.


for(i=1; i<=NF; i++)


This code, awk for() looping, is same with C for() looping. The main difference is NF variable. This is built-in variable in awk used for Number of Field (read: number of column). So, with this for() looping I scan my whole exercise. I use i variable for counting field by field.


if($i ~ /^[[:digit:]]+/)


This code, if() statement, used for searching pattern. Basically, this if() statement does saving any matching pattern with regex I specified, into $i variable. Remember, $i variable. It is different with just i. My regex for this is /^[[:digit:]]+/ that means:

  • ^ = must at first place, avoid the pattern match in after or in middle. Must in the front. So, every pattern should be at first appear in the word. Example: /^anu/ is match with anu1 and anu3, not match with banu, 8anu, or every pattern not placing anu at first. That is ^ (carat). 
  • [[:digit:]] = POSIX style regex for every numbers. It limits pattern for only number, no alphabet or strange character can enter. 
  • + = one or many. It causes [[:digit:]] regex can used for 20. Without +, it only can match single number like 1, 3, 5, and so on. And + causes [[:digit:]] regex never match empty character, it must mach at least 1 character. So, with this +, I can scan number at least 0 until infinite. 
My second code was same with the first. The only one difference was

if(($i ~ /^m\/s2/) || ($i ~ /^kg$/) || ($i ~ /^m$/))  

What is the meaning of that code? Actually simple. We can divide that into 3 parts. First:


($i ~ /^m\/s2/)


Second:


($i ~ /^kg$/)


Third:


($i ~ /^m$/)


and those 3 parts connected by || operator. Those code mean: m/s2 OR kg OR m is match and save it on $i variable. Why we must write m\/s2 and not directly m/s2? Because awk differ pattern and internal awk code. Actually / (slash) is internal awk code, used for limiting regex. Any code snipped between two slashes (//), is pattern. So, when we want to search slash (/), we should inform awk that this slash is not internal awk code. How? Just use awk escape character named backslash (\) persistent in front of slash (/).

And what is $ (dollar) sign? It is opponent of ^ sign. If ^ is in front, then $ is in rear. If there is /anu$/ regex pattern, then it is match with lanu, 8anu, banu, any word contains anu pattern in rear. This /anu$/ is not match with anu9, anuyu, anum, any word contains character after anu.

My third code, the joined code, contains my first and second code. But there is difference.


BEGIN { FS = "[, ]+" }


What is that? This code section contains FS variable which is built-in awk variable for Field Separator customization. I put comma (,) and space ( ) as field separator. So, if there is m, or kg, or m/s, on my exercise, awk will never consider comma. In other word, I specify field separator so awk just take any unit but not the comma. So, later I can get output kg only not kg, (notice the comma).


Thursday, June 6, 2013

A Little Tips for Handling Virus in Windows

Bismillahirrahmanirrahim,

I have experienced virus attack many times in my computer. At that day, I was using Windows XP. Beside my antivirus, I have some tricks if my flash disk or my hard disk entered by virus.

1. Burn Linux ISO to CD (but now you can burn it to USB flash disk).
2. Run Linux as Live CD and read the partition.
3. Search for the virus name, or any .lnk file if the virus is shortcut virus type, or any autorun.inf file created by virus, or any .pif or .com file as common original virus file, and sort them all.
4. Delete all viruses by Ctrl+A and Shift+Del, but you should be careful of this.

It is my trick for handling Windows virus. There is many other tricks but at this time, I give you this only. Remember, all risk are yours. Handle with care. Keep calm and learn.

A Collection of Linux Terminal (Command Line) Resources

Bismillahirrahmanirrahim,

Actually Terminal (you can call it shell, command line, or command prompt) is not for newbie. But every newbie needs to learn Linux Terminal Commands and it is fun. I have a web collection of amazing tutorial in this case. You can start learning command line by following these websites. Or, like what I do when I need a website, you can mirror it with HTTrack Website Copier. Keep calm and learn Terminal.

1. The 5-Minute Essential Shell Tutorial



Comment: this tutorial for me is the easiest Terminal tutorial on the net. You should come here first.

2. Ubuntu Using The Terminal

https://help.ubuntu.com/community/UsingTheTerminal



3. Ubuntu Command Line Howto

https://help.ubuntu.com/community/CommandlineHowto



4. Ubuntu Advanced Command Line Howto

https://help.ubuntu.com/community/AdvancedCommandlineHowto




5. IBM Bash by example, Part 1

http://www.ibm.com/developerworks/linux/library/l-bash/index.html

6. Bash Guide for Beginners

http://tille.garrels.be/training/bash/

7. An A-Z Index of the Bash command line for Linux

http://ss64.com/bash/

8. The Bash-Hackers Wiki

http://wiki.bash-hackers.org/start

9. Bash Reference Manual

http://www.gnu.org/software/bash/manual/bashref.html

10. Bashshell Exercise


http://bashshell.net/category/exercises/

11. Learning The Shell

http://linuxcommand.org/lc3_learning_the_shell.php


List of Linux Command Resources Collector

Above are the webs of tutorial. Now, if you want another collection posts like this post, you can see at these pages:



Some Books in Linux Command Line Learning

Often you will find more comfort to read real books over the digital books. You can purchase one of them by searching on Google or Amazon. But there are some examples of good books.































Wednesday, June 5, 2013

Linux Screenshots Collection Part 1

Bismillahirrahmanirrahim,

In case I don't know yet how to fill this new blog, it is better if I upload my Linux screenshots collection. I will make all in separate posts. I think you will like it :) Most of these I got from Google Plus.

This is Ubuntu with Cinnamon Desktop



This is elementaryOS with Pantheon Desktop





Truly, I love Pantheon desktop. I see many screenshots of Pantheon and all greats. I like this smooth desktop system and I like the innovation from the devs.

This is Linux Mint with Cinnamon Desktop





Actually I love Cinnamon innovation. More if it can be translucent like that :) And it is good if Cinnamon tweaked into Mac OS style with dock at bottom.

This is Ubuntu 12.04 with Some Tweaks


I got this here and tou can get the video tutorial for it: http://doitwithgnulinux.blogspot.com. Truly I love this desktop at first glance, directly. Simple, clean, and the Conky at bottom is cool.

This is unknown Linux using Emerald Window Manager



I got it from http://www.webupd8.org/2013/05/how-to-install-emerald-in-ubuntu-1304.html. Same, I love this at first glance directly. I love the translucency at the window border. Yes, only the window border not whole. And I think you will like nostalgia with Emerald :)

This is unknown Linux with KDE Desktop


KDE is my favourite desktop. But I don't like this screenshot too much.


This is Linux Mint with MATE Desktop


This is Pinguy OS wih GNOME 3 Desktop


Insya Allah, to be continued...

An Applications Collection for Restoring Deleted File - Free!

Bismillahirrahmanirrahim.

When I lose my data on Windows, or accidentally deleted some files, or I need to help my friend's data lose, I use these softwares. They are all free (some demo) and downloadable from the internet. All have small size and let me show you them. I try to give you all direct link to the EXE files. Today (5 June 2013) I have 13 softwares and someday this list can be increased. Hope this will be useful.

PC Inspector File Recovery

License: Freeware
Size: 5.8 MB
Download: ftp://ftp2.convar.com/pcinspector/pci_filerecovery.exe
My comment: I put this software as the best for me in recovering my files completely on my  hard disk.

Glary Undelete
License: Ad-supported
Size: 1.6 MB
Download: http://www.glarysoft.com/download/gun.exe

Undelete Plus
License: Demo
Size: 2 MB
Download: http://undeleteplus.com/files/undeleteplus_setup_1117.exe

Avira Unerase Personal
Liense: Freeware
Size: 402 KB
Download: unerase_en_h.exe

Recuva
License: Freeware
Size: 2.5 MB
Download: rcsetup146_slim.exe

Restoration
License: Freeware
Size: 163 KB 
Download: Restoration.zip

mmCard Recovery
License: Demo
Size: 2.6 MB
Download: http://www.digitalleo.com/downloads/mmcardrecoverysetup.zip

smart recovery
License: Freeware
Size: 6.1 MB
Download: http://download.pcinspector.de/pci_us_smartrecovery.exe

Smart Data Recovery
License: Ad-supported
Size: 2.2 MB
Download: recoverysetup.exe

Recover My Files
License: Demo
Size: 31.1 MB
Download: RecoverMyFiles-Setup.exe

R-Studio
License: Demo
Size: 31.9 MB
Download: RStudio6.exe

GetDataBack for NTFS
License: Demo
Size: 2.5 MB
Download: gdbnt.zip

GetDataBack for FAT
License: Demo
Size: 2.5 MBDownload: gdb.zip

My trusted source in writing this posting is http://softpedia.com. Thank you for reading!

Wednesday, February 13, 2013

Learning Linux Desktop for Beginner Part 2

Hi, it is part 2 from Learning Linux Desktop for Beginner serials. Now we will go ahead into something different.



2. Try
After reading, you should do trying. Obtain a copy of Ubuntu[1] and install it in your computer. So the steps are:

  1. Instal Ubuntu
  2. Use it for daily usage
  3. Find a problem, or more
  4. Solve it
You can get Ubuntu in your computer by getting the ISO file, then burn it to CD, then boot your computer using it. You can find how to install Ubuntu through Google. Further, I will make this part 2 longer by explaining it, insya Allah


to be continued...


__________________
[1] I say Ubuntu because it is the easier for me to learn Linux desktop, and I am sure you will too. You can obtain the ISO here: http://www.ubuntu.com/download/desktop. I've choosen the easiest way for you :) But if you prefer another Linux distro, it is your rights :)

Tuesday, February 12, 2013

Learning Linux Desktop for Beginner Part 1

Hello, Beginner! This is a serial article in Linux Desktop Learning. I wanna fill up this blog with something like serial I have created in past, titled Belajar Komputer untuk Orang Awam (Indonesian: Learning Computer for Newbie). It is easy for me and for you :) Yes, as you can guess, this serial will show you how to learn Linux Desktop from zero. This serial comes from my own knowledge and experiences. So, you can say this serial is from beginner to beginner. I will fill this serial up with points in every post. It is easier for learning than paragraphs, at least for me. And I promise here my serial will contains screenshots, I'll try to give as much as I can. We can start now.

1. Read
The ultimate way for entering Linux learning is reading. No one you can do without it. So, start with any Linux books. Take and read. You can choose first, magazine. Why? It is colorful, has many pictures, so you will never be bored with. Second, you can choose book. If you have reading hobby, it is not a problem. But if not, you should try to read. Give it a try, you will find it useful. My suggestion in this:


  1. Ubuntu Full Circle Magazine
  2. Linux Journal
  3. InfoLINUX (Indonesian)
  4. PCLinuxOS Magazine

Further informations can you see here: http://en.wikipedia.org/wiki/List_of_computer_magazines#Linux_and_open-source. And as addition, you can: 
  1. read anything what you get
  2. get help from any application menu
  3. get manual inside your application or see online and read it
  4. Google >> linux tutorial filetype:pdf

to be continued...