Mari Belajar Linux: Pemprosesan Teks & Editor (Bhg. 1)

Arahan-arahan yang akan dikongsikan pada posting kali ini adalah berkaitan dengan pemprosesan teks.  Arahan-arahan ini membolehkan kita membina, mengedit dan memanipulasi kandungan teks di dalam fail Linux.

1. Arahan cat (concatenate)

Arahan ini membolehkan kita melihat kandungan teks yang terdapat di dalam fail. Arahan yang digunakan adalah: cat <nama fail teks>

Contoh, melihat kandungan fail di /etc/hosts dan /etc/network/interfaces:

Fail /etc/hosts:

nazri@LAMP:~$ cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 LAMP
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Fail /etc/network/interfaces:

nazri@LAMP:~$ cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet static
 address 192.168.0.103
 netmask 255.255.255.0
 gateway 192.168.0.254

Nota:

  • /etc/hosts – fail bagi memetakan atau “map” nama host kepada alamat IP
  • /etc/network/interfaces – fail konfigurasi NIC atau Network Interface Card

Arahan ini juga boleh digabungkan dengan arahan more jika output adalah terlalu panjang.  Contoh: cat /etc/passwd | more

root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
uucp:x:10:10:uucp:/var/spool/uucp:/bin/sh
proxy:x:13:13:proxy:/bin:/bin/sh
www-data:x:33:33:www-data:/var/www:/bin/sh
backup:x:34:34:backup:/var/backups:/bin/sh
list:x:38:38:Mailing List Manager:/var/list:/bin/sh
irc:x:39:39:ircd:/var/run/ircd:/bin/sh
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/bin/sh
nobody:x:65534:65534:nobody:/nonexistent:/bin/sh
libuuid:x:100:101::/var/lib/libuuid:/bin/sh
syslog:x:101:103::/home/syslog:/bin/false
mysql:x:102:105:MySQL Server,,,:/nonexistent:/bin/false
messagebus:x:103:106::/var/run/dbus:/bin/false
landscape:x:104:109::/var/lib/landscape:/bin/false
--More--

Antara option yang menarik adalah cat -n seperti contoh berikut: cat -n /etc/passwd | more

 1 root:x:0:0:root:/root:/bin/bash
 2 daemon:x:1:1:daemon:/usr/sbin:/bin/sh
 3 bin:x:2:2:bin:/bin:/bin/sh
 4 sys:x:3:3:sys:/dev:/bin/sh
 5 sync:x:4:65534:sync:/bin:/bin/sync
 6 games:x:5:60:games:/usr/games:/bin/sh
 7 man:x:6:12:man:/var/cache/man:/bin/sh
 8 lp:x:7:7:lp:/var/spool/lpd:/bin/sh
 9 mail:x:8:8:mail:/var/mail:/bin/sh
 10 news:x:9:9:news:/var/spool/news:/bin/sh
 11 uucp:x:10:10:uucp:/var/spool/uucp:/bin/sh
 12 proxy:x:13:13:proxy:/bin:/bin/sh
 13 www-data:x:33:33:www-data:/var/www:/bin/sh
 14 backup:x:34:34:backup:/var/backups:/bin/sh
 15 list:x:38:38:Mailing List Manager:/var/list:/bin/sh
 16 irc:x:39:39:ircd:/var/run/ircd:/bin/sh
 17 gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/bin/sh
 18 nobody:x:65534:65534:nobody:/nonexistent:/bin/sh
 19 libuuid:x:100:101::/var/lib/libuuid:/bin/sh
 20 syslog:x:101:103::/home/syslog:/bin/false
 21 mysql:x:102:105:MySQL Server,,,:/nonexistent:/bin/false
 22 messagebus:x:103:106::/var/run/dbus:/bin/false
 23 landscape:x:104:109::/var/lib/landscape:/bin/false
--More--

2. Arahan echo

Arahan ini digunakan untuk memaparkan satu baris teks.  Contoh:

nazri@LAMP:~$ echo "satu baris teks"
satu baris teks

Option lain yang sering digunakan adalah -e di mana format berikut boleh dipaparkan:

  • \\ backslash
  • \a alert (BEL)
  • \b backspace
  • \c produce no further output
  • \e escape
  • \f form feed
  • \n new line
  • \r carriage return
  • \t horizontal tab
  • \v vertical tab

Contoh:

nazri@LAMP:~$ echo -e " baris 1 \n baris 2 \n baris 3 \n"
 baris 1
 baris 2
 baris 3

Setiap arahan UNIX/Linux boleh disimpan / redirect ke dalam fail dengan menggunakan aksara simbol lebih besar “>“.  Contoh:

nazri@LAMP:~$ echo -e " baris 1 \n baris 2 \n baris 3 \n" > baris.txt

nazri@LAMP:~$ ls
apache2 ayam.txt backup-apache2 baris.txt hosts

nazri@LAMP:~$ cat baris.txt
 baris 1
 baris 2
 baris 3

Jika arahan:

echo -e " baris 1 \n baris 2 \n baris 3 \n" > baris.txt

.. dilaksanakan sekali lagi, fail “baris.txt” akan di overwrite.  Bagaimanapun, jika simbol >> digunakan, fail ini akan  append di bawahnya.  Contoh:

 

nazri@LAMP:~$ echo -e " baris 1 \n baris 2 \n baris 3 \n" > baris.txt
nazri@LAMP:~$ echo -e " baris 4 \n baris 5 \n baris 6 \n" >> baris.txt
nazri@LAMP:~$ cat baris.txt
 baris 1
 baris 2
 baris 3

 baris 4
 baris 5
 baris 6

3. Arahan grep

Arahan grep digunakan untuk memaparkan pattern atau corak tertentu terhadap sesuatu teks.  Penggunaan adalah seperti berikut: grep <pattern> <nama fail>

Contoh, cari “nazri” dalam fail /etc/passwd:

nazri@LAMP:~$ grep nazri /etc/passwd
nazri:x:1000:1000:Md Nazri Ahmad,,,:/home/nazri:/bin/bash

Contoh ke-2, cari “baris 5” dalam fail baris.txt:

nazri@LAMP:~$ grep "baris 5" baris.txt
 baris 5

Contoh ke-3, cari “nazri” dalam fail-fail yang terdapat dalam direktori /etc:

nazri@LAMP:~$ grep nazri /etc/*
grep: /etc/acpi: Is a directory
grep: /etc/alternatives: Is a directory
grep: /etc/apache2: Is a directory
grep: /etc/apm: Is a directory
grep: /etc/apparmor: Is a directory
grep: /etc/apparmor.d: Is a directory
grep: /etc/apport: Is a directory
grep: /etc/apt: Is a directory
grep: /etc/at.deny: Permission denied
grep: /etc/bash_completion.d: Is a directory
grep: /etc/blkid.tab: No such file or directory
grep: /etc/byobu: Is a directory
grep: /etc/ca-certificates: Is a directory
grep: /etc/calendar: Is a directory
grep: /etc/chatscripts: Is a directory
grep: /etc/console-setup: Is a directory
grep: /etc/cron.d: Is a directory
grep: /etc/cron.daily: Is a directory
grep: /etc/cron.hourly: Is a directory
grep: /etc/cron.monthly: Is a directory
grep: /etc/cron.weekly: Is a directory
grep: /etc/dbus-1: Is a directory
grep: /etc/default: Is a directory
grep: /etc/depmod.d: Is a directory
grep: /etc/dhcp: Is a directory
grep: /etc/dpkg: Is a directory
grep: /etc/fonts: Is a directory
grep: /etc/fstab.d: Is a directory
grep: /etc/fuse.conf: Permission denied
grep: /etc/groff: Is a directory
/etc/group:adm:x:4:nazri
/etc/group:cdrom:x:24:nazri
/etc/group:sudo:x:27:nazri
/etc/group:dip:x:30:nazri
/etc/group:plugdev:x:46:nazri
/etc/group:nazri:x:1000:
/etc/group:lpadmin:x:113:nazri
/etc/group:sambashare:x:114:nazri
grep: /etc/group-: Permission denied
grep: /etc/grub.d: Is a directory
grep: /etc/gshadow: Permission denied
grep: /etc/gshadow-: Permission denied
grep: /etc/ifplugd: Is a directory
grep: /etc/init: Is a directory
grep: /etc/init.d: Is a directory
grep: /etc/initramfs-tools: Is a directory
grep: /etc/insserv: Is a directory
grep: /etc/insserv.conf.d: Is a directory
grep: /etc/iproute2: Is a directory
grep: /etc/iscsi: Is a directory
grep: /etc/kbd: Is a directory
grep: /etc/kernel: Is a directory
grep: /etc/landscape: Is a directory
grep: /etc/ldap: Is a directory
grep: /etc/ld.so.conf.d: Is a directory
grep: /etc/libnl-3: Is a directory
grep: /etc/logcheck: Is a directory
grep: /etc/logrotate.d: Is a directory
grep: /etc/modprobe.d: Is a directory
grep: /etc/mysql: Is a directory
grep: /etc/network: Is a directory
grep: /etc/newt: Is a directory
grep: /etc/opt: Is a directory
grep: /etc/pam.d: Is a directory
/etc/passwd:nazri:x:1000:1000:Md Nazri Ahmad,,,:/home/nazri:/bin/bash
grep: /etc/passwd-: Permission denied
grep: /etc/perl: Is a directory
grep: /etc/php5: Is a directory
grep: /etc/pm: Is a directory
grep: /etc/polkit-1: Is a directory
grep: /etc/ppp: Is a directory
grep: /etc/profile.d: Is a directory
grep: /etc/python: Is a directory
grep: /etc/python2.7: Is a directory
grep: /etc/python3: Is a directory
grep: /etc/python3.3: Is a directory
grep: /etc/rc0.d: Is a directory
grep: /etc/rc1.d: Is a directory
grep: /etc/rc2.d: Is a directory
grep: /etc/rc3.d: Is a directory
grep: /etc/rc4.d: Is a directory
grep: /etc/rc5.d: Is a directory
grep: /etc/rc6.d: Is a directory
grep: /etc/rcS.d: Is a directory
grep: /etc/resolvconf: Is a directory
grep: /etc/rsyslog.d: Is a directory
grep: /etc/security: Is a directory
grep: /etc/selinux: Is a directory
grep: /etc/sgml: Is a directory
grep: /etc/shadow: Permission denied
grep: /etc/shadow-: Permission denied
grep: /etc/skel: Is a directory
grep: /etc/ssh: Is a directory
grep: /etc/ssl: Is a directory
/etc/subgid:nazri:170000:10000
grep: /etc/subgid-: Permission denied
/etc/subuid:nazri:170000:10000
grep: /etc/subuid-: Permission denied
grep: /etc/sudoers: Permission denied
grep: /etc/sudoers.d: Is a directory
grep: /etc/sysctl.d: Is a directory
grep: /etc/systemd: Is a directory
grep: /etc/terminfo: Is a directory
grep: /etc/udev: Is a directory
grep: /etc/ufw: Is a directory
grep: /etc/update-manager: Is a directory
grep: /etc/update-motd.d: Is a directory
grep: /etc/update-notifier: Is a directory
grep: /etc/vim: Is a directory
grep: /etc/w3m: Is a directory
grep: /etc/wpa_supplicant: Is a directory
grep: /etc/X11: Is a directory
grep: /etc/xml: Is a directory

4. Arahan wc

Digunakan untuk memaparkan bilangan baris, perkataan dan saiz fail dalam byte. Contoh:

nazri@LAMP:~$ wc /etc/passwd
26 34 1109 /etc/passwd

Bagi memaparkan bilangan baris:

nazri@LAMP:~$ wc -l /etc/passwd
26 /etc/passwd

Memaparkan bilangan perkataan:

nazri@LAMP:~$ wc -w /etc/passwd
34 /etc/passwd

Memaparkan saiz fail:

nazri@LAMP:~$ wc -c /etc/passwd
1109 /etc/passwd

 5. Arahan sort

Arahan sort digunakan untuk menyusun kandungan sesuatu fail.  Bagi memahami arahan ini, mari kita bina satu fail dengan arahan echo:

nazri@LAMP:~$ echo -e "1\n10\n3\n2\n5\n9\n8\n" > sort.txt
nazri@LAMP:~$ cat sort.txt
1
10
3
2
5
9
8

Oleh itu, fail ini boleh disusun dengan arahan seperti berikut:

nazri@LAMP:~$ sort sort.txt
1
10
2
3
5
8
9
nazri@LAMP:~$ sort -g sort.txt
1
2
3
5
8
9
10