26 February 2010

การเลือกแสดงใน Ubuntu

การเลือกแสดงที่จะนำเสนอคือการใช้
-grep
-cut
-awk

โดยตัวอย่างที่ใช้คือการแสดง ip ของ interface ทำได้โดย

root@ubuntu:/# ifconfig
eth3 Link encap:Ethernet HWaddr 00:0c:29:7f:94:d1
inet addr:192.168.200.130 Bcast:192.168.200.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe7f:94d1/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:263 errors:0 dropped:0 overruns:0 frame:0
TX packets:252 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:25424 (25.4 KB) TX bytes:30155 (30.1 KB)
Interrupt:17 Base address:0x24a4

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:8 errors:0 dropped:0 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:556 (556.0 B) TX bytes:556 (556.0 B)

จะทำการแสดงค่าปกติ แล้วจากนั้นจะทำการเลือกบรรทัดที่ต้องการโดยใช้ grep

root@ubuntu:/# ifconfig eth3 | grep "inet addr"
inet addr:192.168.200.130 Bcast:192.168.200.255 Mask:255.255.255.0

จากข้างบนคือผมเลือกข้อมูลของ eth3 แล้วเลือกบรรทัดที่มีคำว่า inet addr ออกมา

root@ubuntu:/# ifconfig eth3 | grep "inet addr" | cut -d: -f2
192.168.200.130 Bcast

ที่นี้ผมได้ใช้คำสั่ง cut โดย -d: คือการใช้ : เป็นอักษระแบ่งข้อความ โดยดูที่ผมมาร์กสีไว้ให้จากตอนใช้
ifconfig eth3 | grep "inet addr" นะครับโดยถ้าใช้
-f1 คือข้อมูลตรงสีแดง
-f2 คือข้อมูลตรงสีส้ม
-f3 คือข้อมูลตรงสีเหลือง
-f4 คือข้อมูลตรงสีเขียว
โดนแต่ละตัวจะมีเครื่องหมาย : เป็นตัวขั้นครับ

root@ubuntu:/# ifconfig eth3 | grep "inet addr" | cut -d: -f2 | awk '{print $1}'
192.168.200.130

จากนั้นเราก็ใช้ awk '{print $1}' ออกมา โดยถ้าหากเลือก เป็น awk '{print $2}' ก็จะได้คำว่า Bcast ออกมาครับ

0 comments:

Post a Comment