Home

Thursday, December 5, 2013

Penggunaan Command Sed dan Awk

Berikut ini beberapa contoh dari penggunaan perintah Sed dan Awk :

File latihan.txt :
---------------------
John Daggett, 341 King Road, Plymouth MA
Alice Ford, 22 East Broadway, Richmond VA
Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
Terry Kalkas, 402 Lans Road, Beaver Falls PA
Eric Adams, 20 Post Road, Sudbury MA
Hubert Sims, 328A Brook Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 73 6th Street, Boston MA


Mengganti kata list menjadi Daftar di file latihan.txt
sed 's/list/Daftar/' latihan.txt >latihan.txt

Menggabung beberapa perintah 
---------------------------------------------------
sed 's/ List/, Daftar/; s/ list/, daftar/' latihan.txt

sed -e 's/ MA/, Massachusetts/' -e 's/ PA/,
Pennsylvania/' list

sed '
s/ MA/, Massachusetts/
s/ PA/, Pennsylvania/
s/ CA/, California/' list


$ cat test1
first:second
one:two

sed 's/\(.*\):\(.*\)/\2:\1/' test1


Perintah Sed dan awk
---------------------------------
$ cat nameState
s/ CA/, California/
s/ MA/, Massachusetts/
s/ OK/, Oklahoma/
s/ PA/, Pennsylvania/
s/ VA/, Virginia/

sed -f nameState latihan.txt | awk -F, '{ print $4 }'

sed -f namestate latihan.txt | awk -F, '{ print $4 ", " $0 }'


Menggunakan perintah Awk
-----------------------------
awk '{ print $1 }' latihan.txt

awk '/MA/' latihan.txt

awk '/MA/ { print $1 }' latihan.txt

awk -F, '/MA/ { print $1 }' latihan.txt

awk -F, '{ print $1; print $2; print $3 }' latihan.txt


Data Sample   BBS-list
-------------------------------
aardvark 555-5553 1200/300 B
alpo-net 555-3412 2400/1200/300 A
barfly 555-7685 1200/300 A
bites 555-1675 2400/1200/300 A
camelot 555-0542 300 C
core 555-2912 1200/300 C
fooey 555-1234 2400/1200/300 B
foot 555-6699 1200/300 B
macfoo 555-6480 1200/300 A
sdace 555-3430 2400/1200/300 A
sabafoo 555-2127 1200/300 C


Data Sample  inventory-shipped
------------------------------------
Jan 13 25 15 115
Feb 15 32 24 226
Mar 15 24 34 228
Apr 31 52 63 420
May 16 34 29 208
Jun 31 42 75 492
Jul 24 34 67 436
Aug 15 34 47 316
Sep 13 55 37 277
Oct 29 54 68 525
Nov 20 87 82 577
Dec 17 35 61 401
Jan 21 36 64 620
Feb 26 58 80 652
Mar 24 75 70 495
Apr 21 70 74 514


awk '/foo/ { print $0 }' BBS-list

awk '/12/ { print $0 }
/21/ { print $0 }' BBS-list inventory-shipped


ls -l | awk '$5 == "Nov" { sum += $4 }
END { print sum }'

ls -l | awk '$5 == "May" { sum += $4 } END { print sum }'

awk '{ print $(2*2) }' BBS-list

awk '{ $3 = $2 - 10; print $2, $3 }' inventory-shipped


awk '{ $2 = $2 - 10; print $0 }' inventory-shipped

awk '{ $6 = ($5 + $4 + $3 + $2) ; print $6 }' inventory-shipped


echo aaaabcd | awk '{ sub(/a+/, "<A>"); print }'


awk 'BEGIN { RS = "/" } { print $0 }' BBS-list

awk '{ print $0 }' RS="/" BBS-list

awk '/foo/ { print $1, $NF }' BBS-list

awk 'BEGIN {
ORS = "\nOUCH!\n"; OFS = "+"
msg = "Dont Panic!"
printf "%s\n", msg
}'


awk '{ printf "%-10s %s\n", $1, $2 }' BBS-list

awk '{ printf "%-10s %d %s\n", $1, $2,$3 }' BBS-list

awk '{ print $2 > "phone-list"
print $1 > "name-list" }' BBS-list

awk '{ printf "%-10s ", $1, $2 }
{ printf "%-10s %s\n", $3, $4 }' BBS-list



Ida.Bagus@NTS-N9ABM1 ~/shellscripts/latihan_awk
$ awk '{ printf "%-10s %s %s\n", $1, $2,$3 }' BBS-list
aardvark   555-5553 1200/300
alpo-net   555-3412 2400/1200/300
barfly     555-7685 1200/300
bites      555-1675 2400/1200/300
camelot    555-0542 300
core       555-2912 1200/300
fooey      555-1234 2400/1200/300
foot       555-6699 1200/300
macfoo     555-6480 1200/300
sdace      555-3430 2400/1200/300
sabafoo    555-2127 1200/300

Ida.Bagus@NTS-N9ABM1 ~/shellscripts/latihan_awk
$ awk '{ printf "%-10s %s %s %s\n", $1, $2,$3,$4 }' BBS-list
aardvark   555-5553 1200/300 B
alpo-net   555-3412 2400/1200/300 A
barfly     555-7685 1200/300 A
bites      555-1675 2400/1200/300 A
camelot    555-0542 300 C
core       555-2912 1200/300 C
fooey      555-1234 2400/1200/300 B
foot       555-6699 1200/300 B
macfoo     555-6480 1200/300 A
sdace      555-3430 2400/1200/300 A
sabafoo    555-2127 1200/300 C

Ida.Bagus@NTS-N9ABM1 ~/shellscripts/latihan_awk
$ awk '{ printf "%-10s %s %-10s %s\n", $1, $2,$3,$4 }' BBS-list
aardvark   555-5553 1200/300   B
alpo-net   555-3412 2400/1200/300 A
barfly     555-7685 1200/300   A
bites      555-1675 2400/1200/300 A
camelot    555-0542 300        C
core       555-2912 1200/300   C
fooey      555-1234 2400/1200/300 B
foot       555-6699 1200/300   B
macfoo     555-6480 1200/300   A
sdace      555-3430 2400/1200/300 A
sabafoo    555-2127 1200/300   C

Ida.Bagus@NTS-N9ABM1 ~/shellscripts/latihan_awk
$ awk '{ printf "%-10s %s %-20s %s\n", $1, $2,$3,$4 }' BBS-list
aardvark   555-5553 1200/300             B
alpo-net   555-3412 2400/1200/300        A
barfly     555-7685 1200/300             A
bites      555-1675 2400/1200/300        A
camelot    555-0542 300                  C
core       555-2912 1200/300             C
fooey      555-1234 2400/1200/300        B
foot       555-6699 1200/300             B
macfoo     555-6480 1200/300             A
sdace      555-3430 2400/1200/300        A
sabafoo    555-2127 1200/300             C

Ida.Bagus@NTS-N9ABM1 ~/shellscripts/latihan_awk
$ awk '{ printf "%-10s  %-20s  %-20s %s\n", $1, $2,$3,$4 }' BBS-list
aardvark    555-5553              1200/300             B
alpo-net    555-3412              2400/1200/300        A
barfly      555-7685              1200/300             A
bites       555-1675              2400/1200/300        A
camelot     555-0542              300                  C
core        555-2912              1200/300             C
fooey       555-1234              2400/1200/300        B
foot        555-6699              1200/300             B
macfoo      555-6480              1200/300             A
sdace       555-3430              2400/1200/300        A
sabafoo     555-2127              1200/300             C


Ida.Bagus@NTS-N9ABM1 ~/shellscripts/latihan_awk
$ awk '{ printf "%-10s  %-10s  %-20s %s\n", $1, $2,$3,$4 }' BBS-list
aardvark    555-5553    1200/300             B
alpo-net    555-3412    2400/1200/300        A
barfly      555-7685    1200/300             A
bites       555-1675    2400/1200/300        A
camelot     555-0542    300                  C
core        555-2912    1200/300             C
fooey       555-1234    2400/1200/300        B
foot        555-6699    1200/300             B
macfoo      555-6480    1200/300             A
sdace       555-3430    2400/1200/300        A
sabafoo     555-2127    1200/300             C


awk 'BEGIN { printf "%-10s %s\n", "Name", "Number"
printf "%-10s %s\n", "----", "------" }
{ printf "%-10s %s\n", $1, $2 }' BBS-list

Ida.Bagus@NTS-N9ABM1 ~/shellscripts/latihan_awk
$ awk 'BEGIN { printf "%-10s %s\n", "Name", "Number"
> printf "%-10s %s\n", "----", "------" }
> { printf "%-10s %s\n", $1, $2 }' BBS-list
Name       Number
----       ------
aardvark   555-5553
alpo-net   555-3412
barfly     555-7685
bites      555-1675
camelot    555-0542
core       555-2912
fooey      555-1234
foot       555-6699
macfoo     555-6480
sdace      555-3430
sabafoo    555-2127



File Email
-------------------
Jane Doe
123 Main Street
Anywhere, SE 12345-6789
John Smith
456 Tree-lined Avenue
Smallville, MW 98765-4321

Program untuk mengkonversi email
-------------------------------------
# addrs.awk --- simple mailing list program
# Records are separated by blank lines.
# Each line is one field.
BEGIN { RS = "" ; FS = "\n" }
{
print "Name is:", $1
print "Address is:", $2
print "City and State are:", $3
print ""
}


Execute Program
--------------------
awk -f addrs.awk addresses

No comments:

Post a Comment