Linux command: grep & pipe — understanding grep and pipe command in linux

Olarik Surinta
olarik
Published in
11 min readFeb 12, 2019

grep

  • คำสั่ง grep มาจากคำว่า global regular expression print ใช้สำหรับการประมวลผลข้อความ และแสดงผลลัพธ์ของการประมวลผลออกจากจอภาพ โดยแสดงผลลัพธ์ที่ตรง (match) กับรูปแบบที่ได้ค้นหา (specified pattern) เช่น ค้นหาข้อความจากไฟล์ข้อมูล และค้นหาข้อมูลจากผลลัพธ์ที่ส่งต่อมาจากคำสั่งอื่น
  • ดังนั้น คำสั่ง grep จึงสามารถนำไปใช้งานร่วมกับคำสั่งอื่น หรือเรียกว่า pipe (|) ทำให้การค้นหาข้อมูลมีประสิทธิภาพมากยิ่งขึ้น

การค้นหาข้อมูลด้วยคำสั่ง grep

ตัวอย่างการค้นหาข้อมูลด้วยคำสั่ง grep สามารถทำได้ดังต่ออย่างต่อไปนี้

การแสดงข้อมูลที่อยู่ในไฟล์

  • หากต้องการแสดงข้อมูลทั้งหมดที่อยู่ในไฟล์สามารถทำได้โดยคำสั่งต่อไปนี้
$ grep '' IoTtweetRPi/IoTtweet.py 
import requests
import time
def WriteDashboard(_userid,_key,_slot0,_slot1,_slot2,_slot3,_tw,_twpb): #Input data
userid = _userid
key = _key
slot0 = _slot0
slot1 = _slot1
slot2 = _slot2
slot3 = _slot3
tw = _tw
twpb = _twpb
#Merge data
payload = {'userid':userid, 'key':key, 'slot0':slot0,'slot1':slot1,'slot2':slot2,'slot3':slot3,'tw':tw,'twpb':twpb}
#Send to IoTtweet
r = requests.get('http://api.iottweet.com',params=payload)
response = r.text

#Display response from IoTtweet server
return response
def getversion():
#Get version of IoTtweet.py
return '0.1'
  • จากตัวอย่างข้างต้นคำสั่ง grep '' IoTtweetRPi/IoTtweet.py จะมีผลลัพธ์ที่เหมือนกับคำสั่ง cat IoTtweetRPi/IoTtweet.py

การค้นหาข้อมูลจากไฟล์

  • ตัวอย่างนี้แสดงวิธีการค้นหาข้อมูลจากไฟล์ โดยไฟล์ที่ต้องการค้นหานั้นสามารถเป็นได้ทั้ง เท็กไฟล์ (txt) หรือไฟล์โปรแกรมต่าง ๆ เช่น html, py และ c
  • เริ่มต้นด้วยการแสดงโครงสร้างของไฟล์และไดเรกทอรีด้วยคำสั่ง tree
$ tree
.
├── bk
│ └── IoTtweetRPi
│ ├── IoTtweet.py
│ ├── README.md
│ ├── simple_read_cpu_temp.py
│ └── WriteDashboard.py
├── encode-decode.py
└── IoTtweetRPi
├── IoTtweet.py
├── README.md
├── simple_read_cpu_temp.py
└── WriteDashboard.py
3 directories, 9 files
  • จากตัวอย่างข้างต้น ได้เลือกทดสอบค้นหาข้อมูลที่อยู่ในไฟล์ IoTtweetRPi/IoTtweet.py โดยใช้คำสั่ง cat เพื่อแสดงข้อมูลที่อยู่ในไฟล์ ดังตัวอย่างต่อไปนี้
$ cat IoTtweetRPi/IoTtweet.py 
import requests
import time
def WriteDashboard(_userid,_key,_slot0,_slot1,_slot2,_slot3,_tw,_twpb): #Input data
userid = _userid
key = _key
slot0 = _slot0
slot1 = _slot1
slot2 = _slot2
slot3 = _slot3
tw = _tw
twpb = _twpb
#Merge data
payload = {'userid':userid, 'key':key, 'slot0':slot0,'slot1':slot1,'slot2':slot2,'slot3':slot3,'tw':tw,'twpb':twpb}
#Send to IoTtweet
r = requests.get('http://api.iottweet.com',params=payload)
response = r.text

#Display response from IoTtweet server
return response
def getversion():
#Get version of IoTtweet.py
return '0.1'
  • จากตัวอย่างข้างต้น เป็นการค้นหาข้อมูลจากไฟล์ของโปรแกรมไพธอน (Python) ซึ่งการสร้างฟังก์ชัน (Function) ในโปรแกรมไพธอนนั้น จะใช้คำสั่ง def หรือย่อมาจากคำว่า definition ดังนั้น สามารถใช้คำสั่งในการค้นหาฟังก์ชัน หรือ def จากไฟล์โปรแกรมไพธอนดังนี้
$ grep 'def' IoTtweetRPi/IoTtweet.py 
def WriteDashboard(_userid,_key,_slot0,_slot1,_slot2,_slot3,_tw,_twpb):
def getversion():
  • จากคำสั่งโปรแกรมจะค้นหาคำว่า def และแสดงออกมาทางจอภาพ โดยผลลัพธ์ที่ได้คือมีจำนวนทั้งสิ้น 2 ฟังก์ชัน ประกอบด้วยฟังก์ชัน WriteDashboard และ getversion

การค้นหาข้อมูลจากไฟล์ และแสดงหมายเลขของบรรทัดที่ค้นเจอ

  • สามารถทำได้ด้วยการเพิ่มตัวเลือก -n หรือ --line-number คำสั่งดังต่อไปนี้
$ grep -n def IoTtweetRPi/IoTtweet.py 
4:def WriteDashboard(_userid,_key,_slot0,_slot1,_slot2,_slot3,_tw,_twpb):
26:def getversion():
  • จากผลลัพธ์ข้างต้น แสดงให้เห็นว่าสามารถค้นพบคำว่า def ที่บรรทัดที่ 4 และ 26

แสดงจำนวนที่พบ (match)

  • คำสั่ง grep สามารถแสดงจำนวนของคำที่พบ โดยใช้ตัวเลือก -c หรือ --count ดังคำสั่งต่อไปนี้
$ grep -c def IoTtweetRPi/IoTtweet.py
2
  • จากตัวอย่าง ระบบจะแสดงหมายเลข 2 นั่นแสดงว่าค้นพบคำว่า def จำนวน 2 ตำแหน่ง

การค้นหาข้อมูลจากไฟล์มากกว่า 1 ไฟล์

  • คำสั่ง grep นอกจากจะค้นหาจากไฟล์ที่ระบุ ยังสามารถค้นหาจากไฟล์มากกว่า 1 ไฟล์ได้ในคราวเดียวกัน แสดงดังตัวอย่างต่อไปนี้
$ grep -n 'IoT' IoTtweetRPi/*
IoTtweetRPi/IoTtweet.py:19: #Send to IoTtweet
IoTtweetRPi/IoTtweet.py:23: #Display response from IoTtweet server
IoTtweetRPi/IoTtweet.py:27: #Get version of IoTtweet.py
IoTtweetRPi/README.md:1:# IoTtweetRPi
IoTtweetRPi/README.md:2:Simple code send data to https://www.IoTtweet.com (Free IoT Dashboard)
IoTtweetRPi/README.md:4:How to use IoTtweet dashboard</br>
IoTtweetRPi/README.md:7:[![Rpi3 with IoTtweet.png](https://s28.postimg.org/4j3z3fnr1/Rpi3_with_Io_Ttweet.png)](https://postimg.org/image/a7a9ubs3d/)
IoTtweetRPi/simple_read_cpu_temp.py:1:from IoTtweet import *
IoTtweetRPi/simple_read_cpu_temp.py:3:#Get version of IoTtweet.py
IoTtweetRPi/simple_read_cpu_temp.py:7:#An IoTtweet account userid (6 digits)
IoTtweetRPi/simple_read_cpu_temp.py:8:userid = 'YOUR USERID AN IoTtweet account'
IoTtweetRPi/simple_read_cpu_temp.py:10:#An IoTtweet registered iot device key (My IoT garage)
IoTtweetRPi/simple_read_cpu_temp.py:11:key = 'YOUR IoT device key'
IoTtweetRPi/simple_read_cpu_temp.py:25: #Send data to IoTtweet dashboard.
IoTtweetRPi/simple_read_cpu_temp.py:28: #Print response JSON from IoTtweet
IoTtweetRPi/WriteDashboard.py:1:from IoTtweet import *
IoTtweetRPi/WriteDashboard.py:3:#Get version of IoTtweet.py
IoTtweetRPi/WriteDashboard.py:7:#An IoTtweet account userid (6 digits)
IoTtweetRPi/WriteDashboard.py:8:userid = 'YOUR USERID AN IoTtweet account'
IoTtweetRPi/WriteDashboard.py:10:#An IoTtweet registered iot device key (My IoT garage)
IoTtweetRPi/WriteDashboard.py:11:key = 'YOUR IoT device key'
IoTtweetRPi/WriteDashboard.py:21:#Send data to IoTtweet dashboard.
IoTtweetRPi/WriteDashboard.py:24:#Print response JSON from IoTtweet
  • จากคำสั่ง grep -n 'IoT' IoTtweetRPi/* หมายถึงการค้นหาคำว่า IoT จากไฟล์ทั้งหมดที่อยู่ในไดเรกทอรี IottweetRPi

Pipe ( | )

  • pipe ( | ) เป็นคำสั่งในลินุกซ์ที่อนุญาตให้ใช้คำสั่งลินุกซ์ร่วมกันมากกว่า 1 คำสั่ง ตัวอย่างเช่น
$ cat IoTtweetRPi/IoTtweet.py | grep 'def'

จากตัวอย่างข้างต้น สามารถอธิบายได้ดังนี้

  • cat IoTtweetRPi/IoTtweet.py คือคำสั่งที่แสดงข้อมูลทั้งหมดที่อยู่ในไฟล์ IoTtweetRPi/IoTtweet.py
  • เครื่องหมาย | หรือ pipe ทำหน้าที่เชื่อมหรือส่งต่อผลลัพธ์ที่ได้จากคำสั่งแรก และส่งไปเป็นอินพุท (input) ของคำสั่งต่อไป
  • grep 'def' เป็นคำสั่งในการค้นหาคำว่า def โดยค้นหาจากผลลัพธ์ที่ได้จากคำสั่งแรก ซึ่งก็คือ cat IoTtweetRPi/IoTtweet.py

ผลลัพธ์ที่ได้แสดงดังต่อไปนี้

$ cat IoTtweetRPi/IoTtweet.py | grep 'def'
def WriteDashboard(_userid,_key,_slot0,_slot1,_slot2,_slot3,_tw,_twpb):
def getversion():
  • หากต้องการที่จะเรียงลำดับ (sort) ผลลัพธ์ที่ได้ สามารถทำได้โดยใช้คำสั่งดังต่อไปนี้
$ cat IoTtweetRPi/IoTtweet.py | grep 'def' | sort
def getversion():
def WriteDashboard(_userid,_key,_slot0,_slot1,_slot2,_slot3,_tw,_twpb):
  • จากตัวอย่างแสดงให้เห็นว่าผลลัพธ์ที่ได้จากคำสั่งที่ 1 จะถูกต้องส่งไปยังคำสั่งที่สองด้วยเครื่องหมาย | และคำสั่งที่สองก็จะถูกส่งต่อไปยังคำสั่งถัดไป

การใช้คำสั่งร่วมกันระหว่าง grep และ sort

  • grep เป็นคำสั่งที่ใช้สำหรับการค้นหาข้อมูล (match) โดยสามารถค้นหาจากเนื้อหาที่อยู่ในไฟล์ข้อมูล หรือผลลัพธ์ (output) ที่ถูกส่งมาจากคำสั่งอื่น ๆ และคำสั่ง sort เป็นคำสั่งที่ใช้สำหรับการจัดเรียงข้อมูล ตัวอย่างดังต่อไปนี้แสดงให้เห็นถึงการนำคำสั่ง grep และ sort มาใช้งานร่วมกันโดยใช้เครื่องหมาย pipe (|) เป็นตัวเชื่อมระหว่างคำสั่งแรกและคำสั่งที่สอง
  • เริ่มต้นด้วยการดาวน์โหลดข้อมูลดอกไม้ iris flower dataset จากเว็บไซต์ http://ias.it.msu.ac.th/sample-data/iris.data ด้วยคำสั่ง wget แสดงดังต่อไปนี้
$ wget http://ias.it.msu.ac.th/sample-data/iris.data
--2018-09-10 14:33:35-- http://ias.it.msu.ac.th/sample-data/iris.data
Resolving ias.it.msu.ac.th (ias.it.msu.ac.th)... 202.28.34.214
Connecting to ias.it.msu.ac.th (ias.it.msu.ac.th)|202.28.34.214|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4549 (4.4K)
Saving to: ‘iris.data’
iris.data 100%[===============================================================================================>] 4.44K --.-KB/s in 0s 2018-09-10 14:33:35 (69.0 MB/s) - ‘iris.data’ saved [4549/4549]
  • โดย wget เป็นคำสั่งสำหรับการดาวน์โหลดข้อมูลจากอินเทอร์เน็ต จากคำสั่งข้างต้น ระบบจึงดาวน์โหลดไฟล์ชื่อ iris.data จากเว็บไซต์ลงมาเก็บไว้ที่ไดเรกทอรี
  • เมื่อดาวน์โหลดเสร็จสามารถใช้คำสั่ง tree หรือ ls เพื่อตรวจสอบข้อมูล แสดงดังต่อไปนี้ คำสั่ง tree
$ tree
.
├── html
│ └── index.html
├── iris.data
├── test
└── tmp1
├── index.htm
└── index.html
3 directories, 4 files

หรือคำสั่ง ls

$ ls
html iris.data test tmp1
  • จากการตรวจสอบพบว่าไฟล์ iris.data ถูกดาวน์โหลดเก็บไว้ในไดเรกทอรีเป็นที่เรียบร้อย จากนั้นสามารถตรวจสอบข้อมูลที่อยู่ในไฟล์ด้วยคำสั่งดังต่อไปนี้
$ grep -n '' iris.data
...
...
137:6.3 3.4 5.6 2.4 Iris-virginica
138:6.4 3.1 5.5 1.8 Iris-virginica
139:6.0 3.0 4.8 1.8 Iris-virginica
140:6.9 3.1 5.4 2.1 Iris-virginica
141:6.7 3.1 5.6 2.4 Iris-virginica
142:6.9 3.1 5.1 2.3 Iris-virginica
143:5.8 2.7 5.1 1.9 Iris-virginica
144:6.8 3.2 5.9 2.3 Iris-virginica
145:6.7 3.3 5.7 2.5 Iris-virginica
146:6.7 3.0 5.2 2.3 Iris-virginica
147:6.3 2.5 5.0 1.9 Iris-virginica
148:6.5 3.0 5.2 2.0 Iris-virginica
149:6.2 3.4 5.4 2.3 Iris-virginica
150:5.9 3.0 5.1 1.8 Iris-virginica

หรือคำสั่ง

$ cat -n iris.data
...
...
137 6.3 3.4 5.6 2.4 Iris-virginica
138 6.4 3.1 5.5 1.8 Iris-virginica
139 6.0 3.0 4.8 1.8 Iris-virginica
140 6.9 3.1 5.4 2.1 Iris-virginica
141 6.7 3.1 5.6 2.4 Iris-virginica
142 6.9 3.1 5.1 2.3 Iris-virginica
143 5.8 2.7 5.1 1.9 Iris-virginica
144 6.8 3.2 5.9 2.3 Iris-virginica
145 6.7 3.3 5.7 2.5 Iris-virginica
146 6.7 3.0 5.2 2.3 Iris-virginica
147 6.3 2.5 5.0 1.9 Iris-virginica
148 6.5 3.0 5.2 2.0 Iris-virginica
149 6.2 3.4 5.4 2.3 Iris-virginica
150 5.9 3.0 5.1 1.8 Iris-virginica
  • จากคำสั่ง grep และ cat แสดงให้เห็นว่าไฟล์ iris.data มีข้อมูลทั้งหมด 150 แถว
  • นอกจากทั้ง 2 คำสั่ง ยังสามารถใช้คำสั่ง wc เพื่อแสดงจำนวนบรรทัดข้อมูลที่อยู่ในไฟล์ได้ ดังตัวอย่างต่อไปนี้
$ wc -l iris.data 
150 iris.data
  • ข้อมูลทั้งหมดที่อยู่ในไฟล์ iris.data มีทั้งสิ้น 150 แถว โดยแบ่งออกเป็นข้อมูลของ Iris-setosa, Iris-versicolor และ Iris-virginica อย่างละ 50 แถว ในตัวอย่างต่อไปนี้จะแสดงข้อมูลเฉพาะของ Iris-setosa เท่านั้น สามารถทำได้โดยใช้คำสั่งที่แตกต่างกันดังนี้
$ grep Iris-setosa iris.data 
$ grep -i iris-setosa iris.data
$ cat iris.data | grep -i iris-setosa
  • จากทั้ง 3 ตัวอย่างข้างต้น ให้ผลลัพธ์ที่ไม่แตกต่างกันดังต่อไปนี้
5.1 3.5 1.4 0.2 Iris-setosa
4.9 3.0 1.4 0.2 Iris-setosa
4.7 3.2 1.3 0.2 Iris-setosa
4.6 3.1 1.5 0.2 Iris-setosa
5.0 3.6 1.4 0.2 Iris-setosa
5.4 3.9 1.7 0.4 Iris-setosa
4.6 3.4 1.4 0.3 Iris-setosa
5.0 3.4 1.5 0.2 Iris-setosa
4.4 2.9 1.4 0.2 Iris-setosa
4.9 3.1 1.5 0.1 Iris-setosa
5.4 3.7 1.5 0.2 Iris-setosa
4.8 3.4 1.6 0.2 Iris-setosa
4.8 3.0 1.4 0.1 Iris-setosa
4.3 3.0 1.1 0.1 Iris-setosa
5.8 4.0 1.2 0.2 Iris-setosa
5.7 4.4 1.5 0.4 Iris-setosa
5.4 3.9 1.3 0.4 Iris-setosa
5.1 3.5 1.4 0.3 Iris-setosa
5.7 3.8 1.7 0.3 Iris-setosa
5.1 3.8 1.5 0.3 Iris-setosa
5.4 3.4 1.7 0.2 Iris-setosa
5.1 3.7 1.5 0.4 Iris-setosa
4.6 3.6 1.0 0.2 Iris-setosa
5.1 3.3 1.7 0.5 Iris-setosa
4.8 3.4 1.9 0.2 Iris-setosa
5.0 3.0 1.6 0.2 Iris-setosa
5.0 3.4 1.6 0.4 Iris-setosa
5.2 3.5 1.5 0.2 Iris-setosa
5.2 3.4 1.4 0.2 Iris-setosa
4.7 3.2 1.6 0.2 Iris-setosa
4.8 3.1 1.6 0.2 Iris-setosa
5.4 3.4 1.5 0.4 Iris-setosa
5.2 4.1 1.5 0.1 Iris-setosa
5.5 4.2 1.4 0.2 Iris-setosa
4.9 3.1 1.5 0.1 Iris-setosa
5.0 3.2 1.2 0.2 Iris-setosa
5.5 3.5 1.3 0.2 Iris-setosa
4.9 3.1 1.5 0.1 Iris-setosa
4.4 3.0 1.3 0.2 Iris-setosa
5.1 3.4 1.5 0.2 Iris-setosa
5.0 3.5 1.3 0.3 Iris-setosa
4.5 2.3 1.3 0.3 Iris-setosa
4.4 3.2 1.3 0.2 Iris-setosa
5.0 3.5 1.6 0.6 Iris-setosa
5.1 3.8 1.9 0.4 Iris-setosa
4.8 3.0 1.4 0.3 Iris-setosa
5.1 3.8 1.6 0.2 Iris-setosa
4.6 3.2 1.4 0.2 Iris-setosa
5.3 3.7 1.5 0.2 Iris-setosa
5.0 3.3 1.4 0.2 Iris-setosa
  • จากนั้นสามารถส่งผลลัพธ์ข้างต้นไปยังคำสั่ง sort เพื่อจัดเรียง ตัวอย่างแสดงดังต่อไปนี้
$ grep  Iris-setosa iris.data | sort
4.3 3.0 1.1 0.1 Iris-setosa
4.4 2.9 1.4 0.2 Iris-setosa
4.4 3.0 1.3 0.2 Iris-setosa
4.4 3.2 1.3 0.2 Iris-setosa
4.5 2.3 1.3 0.3 Iris-setosa
4.6 3.1 1.5 0.2 Iris-setosa
4.6 3.2 1.4 0.2 Iris-setosa
4.6 3.4 1.4 0.3 Iris-setosa
4.6 3.6 1.0 0.2 Iris-setosa
4.7 3.2 1.3 0.2 Iris-setosa
4.7 3.2 1.6 0.2 Iris-setosa
4.8 3.0 1.4 0.1 Iris-setosa
4.8 3.0 1.4 0.3 Iris-setosa
4.8 3.1 1.6 0.2 Iris-setosa
4.8 3.4 1.6 0.2 Iris-setosa
4.8 3.4 1.9 0.2 Iris-setosa
4.9 3.0 1.4 0.2 Iris-setosa
4.9 3.1 1.5 0.1 Iris-setosa
4.9 3.1 1.5 0.1 Iris-setosa
4.9 3.1 1.5 0.1 Iris-setosa
5.0 3.0 1.6 0.2 Iris-setosa
5.0 3.2 1.2 0.2 Iris-setosa
5.0 3.3 1.4 0.2 Iris-setosa
5.0 3.4 1.5 0.2 Iris-setosa
5.0 3.4 1.6 0.4 Iris-setosa
5.0 3.5 1.3 0.3 Iris-setosa
5.0 3.5 1.6 0.6 Iris-setosa
5.0 3.6 1.4 0.2 Iris-setosa
5.1 3.3 1.7 0.5 Iris-setosa
5.1 3.4 1.5 0.2 Iris-setosa
5.1 3.5 1.4 0.2 Iris-setosa
5.1 3.5 1.4 0.3 Iris-setosa
5.1 3.7 1.5 0.4 Iris-setosa
5.1 3.8 1.5 0.3 Iris-setosa
5.1 3.8 1.6 0.2 Iris-setosa
5.1 3.8 1.9 0.4 Iris-setosa
5.2 3.4 1.4 0.2 Iris-setosa
5.2 3.5 1.5 0.2 Iris-setosa
5.2 4.1 1.5 0.1 Iris-setosa
5.3 3.7 1.5 0.2 Iris-setosa
5.4 3.4 1.5 0.4 Iris-setosa
5.4 3.4 1.7 0.2 Iris-setosa
5.4 3.7 1.5 0.2 Iris-setosa
5.4 3.9 1.3 0.4 Iris-setosa
5.4 3.9 1.7 0.4 Iris-setosa
5.5 3.5 1.3 0.2 Iris-setosa
5.5 4.2 1.4 0.2 Iris-setosa
5.7 3.8 1.7 0.3 Iris-setosa
5.7 4.4 1.5 0.4 Iris-setosa
5.8 4.0 1.2 0.2 Iris-setosa
  • จากตัวอย่างข้างต้น สามารถจัดเรียงผลลัพธ์โดยเรียงจากน้อยไปหามากด้วยคำสั่ง sort หรือหากต้องการจัดเรียงจากมากไปหาน้อยสามรถใช้ตัวเลือก -r แสดงดังตัวอย่างต่อไปนี้
$ grep  Iris-setosa iris.data | sort -r
5.8 4.0 1.2 0.2 Iris-setosa
5.7 4.4 1.5 0.4 Iris-setosa
5.7 3.8 1.7 0.3 Iris-setosa
5.5 4.2 1.4 0.2 Iris-setosa
5.5 3.5 1.3 0.2 Iris-setosa
5.4 3.9 1.7 0.4 Iris-setosa
5.4 3.9 1.3 0.4 Iris-setosa
5.4 3.7 1.5 0.2 Iris-setosa
5.4 3.4 1.7 0.2 Iris-setosa
5.4 3.4 1.5 0.4 Iris-setosa
5.3 3.7 1.5 0.2 Iris-setosa
5.2 4.1 1.5 0.1 Iris-setosa
5.2 3.5 1.5 0.2 Iris-setosa
5.2 3.4 1.4 0.2 Iris-setosa
5.1 3.8 1.9 0.4 Iris-setosa
5.1 3.8 1.6 0.2 Iris-setosa
5.1 3.8 1.5 0.3 Iris-setosa
5.1 3.7 1.5 0.4 Iris-setosa
5.1 3.5 1.4 0.3 Iris-setosa
5.1 3.5 1.4 0.2 Iris-setosa
5.1 3.4 1.5 0.2 Iris-setosa
5.1 3.3 1.7 0.5 Iris-setosa
5.0 3.6 1.4 0.2 Iris-setosa
5.0 3.5 1.6 0.6 Iris-setosa
5.0 3.5 1.3 0.3 Iris-setosa
5.0 3.4 1.6 0.4 Iris-setosa
5.0 3.4 1.5 0.2 Iris-setosa
5.0 3.3 1.4 0.2 Iris-setosa
5.0 3.2 1.2 0.2 Iris-setosa
5.0 3.0 1.6 0.2 Iris-setosa
4.9 3.1 1.5 0.1 Iris-setosa
4.9 3.1 1.5 0.1 Iris-setosa
4.9 3.1 1.5 0.1 Iris-setosa
4.9 3.0 1.4 0.2 Iris-setosa
4.8 3.4 1.9 0.2 Iris-setosa
4.8 3.4 1.6 0.2 Iris-setosa
4.8 3.1 1.6 0.2 Iris-setosa
4.8 3.0 1.4 0.3 Iris-setosa
4.8 3.0 1.4 0.1 Iris-setosa
4.7 3.2 1.6 0.2 Iris-setosa
4.7 3.2 1.3 0.2 Iris-setosa
4.6 3.6 1.0 0.2 Iris-setosa
4.6 3.4 1.4 0.3 Iris-setosa
4.6 3.2 1.4 0.2 Iris-setosa
4.6 3.1 1.5 0.2 Iris-setosa
4.5 2.3 1.3 0.3 Iris-setosa
4.4 3.2 1.3 0.2 Iris-setosa
4.4 3.0 1.3 0.2 Iris-setosa
4.4 2.9 1.4 0.2 Iris-setosa
4.3 3.0 1.1 0.1 Iris-setosa
  • คำสั่ง sort ยังสามารถจัดเรียงโดยเรียงจากลำดับของคอลัมน์ โดยใช้ตัวเลือก -k ดังตัวอย่างต่อไปนี้
$ grep  Iris-setosa iris.data | sort -k 2
4.5 2.3 1.3 0.3 Iris-setosa
4.4 2.9 1.4 0.2 Iris-setosa
4.3 3.0 1.1 0.1 Iris-setosa
4.4 3.0 1.3 0.2 Iris-setosa
4.8 3.0 1.4 0.1 Iris-setosa
4.9 3.0 1.4 0.2 Iris-setosa
4.8 3.0 1.4 0.3 Iris-setosa
5.0 3.0 1.6 0.2 Iris-setosa
4.9 3.1 1.5 0.1 Iris-setosa
4.9 3.1 1.5 0.1 Iris-setosa
4.9 3.1 1.5 0.1 Iris-setosa
4.6 3.1 1.5 0.2 Iris-setosa
4.8 3.1 1.6 0.2 Iris-setosa
5.0 3.2 1.2 0.2 Iris-setosa
4.4 3.2 1.3 0.2 Iris-setosa
4.7 3.2 1.3 0.2 Iris-setosa
4.6 3.2 1.4 0.2 Iris-setosa
4.7 3.2 1.6 0.2 Iris-setosa
5.0 3.3 1.4 0.2 Iris-setosa
5.1 3.3 1.7 0.5 Iris-setosa
5.2 3.4 1.4 0.2 Iris-setosa
4.6 3.4 1.4 0.3 Iris-setosa
5.0 3.4 1.5 0.2 Iris-setosa
5.1 3.4 1.5 0.2 Iris-setosa
5.4 3.4 1.5 0.4 Iris-setosa
4.8 3.4 1.6 0.2 Iris-setosa
5.0 3.4 1.6 0.4 Iris-setosa
5.4 3.4 1.7 0.2 Iris-setosa
4.8 3.4 1.9 0.2 Iris-setosa
5.5 3.5 1.3 0.2 Iris-setosa
5.0 3.5 1.3 0.3 Iris-setosa
5.1 3.5 1.4 0.2 Iris-setosa
5.1 3.5 1.4 0.3 Iris-setosa
5.2 3.5 1.5 0.2 Iris-setosa
5.0 3.5 1.6 0.6 Iris-setosa
4.6 3.6 1.0 0.2 Iris-setosa
5.0 3.6 1.4 0.2 Iris-setosa
5.3 3.7 1.5 0.2 Iris-setosa
5.4 3.7 1.5 0.2 Iris-setosa
5.1 3.7 1.5 0.4 Iris-setosa
5.1 3.8 1.5 0.3 Iris-setosa
5.1 3.8 1.6 0.2 Iris-setosa
5.7 3.8 1.7 0.3 Iris-setosa
5.1 3.8 1.9 0.4 Iris-setosa
5.4 3.9 1.3 0.4 Iris-setosa
5.4 3.9 1.7 0.4 Iris-setosa
5.8 4.0 1.2 0.2 Iris-setosa
5.2 4.1 1.5 0.1 Iris-setosa
5.5 4.2 1.4 0.2 Iris-setosa
5.7 4.4 1.5 0.4 Iris-setosa
  • จากตัวอย่างข้างต้น กำหนดให้ตัวเลือก -k 2 หมายถึง ให้จัดเรียงตามคอลัมน์ที่ 2 ซึ่งผลลัพธ์คือการจัดเรียงจากคอมลัมน์สอง โดยเรียงจากน้อยไปมาก
  • ผลลัพธ์ที่แสดงออกมาทางหน้าจอ แสดงทั้งสิ้น 50 แถว ในกรณีนี้สามารถที่จะกำหนดจำนวนผลลัพธ์ที่จะแสดงออกจากหน้าจอได้ เช่น กำหนดให้แสดงจำนวน 10 แถว เป็นต้น สามารถทำได้ด้วยคำสั่ง head แสดงดังต่อไปนี้
$ grep  Iris-setosa iris.data | sort -k 2 | head -10
4.5 2.3 1.3 0.3 Iris-setosa
4.4 2.9 1.4 0.2 Iris-setosa
4.3 3.0 1.1 0.1 Iris-setosa
4.4 3.0 1.3 0.2 Iris-setosa
4.8 3.0 1.4 0.1 Iris-setosa
4.9 3.0 1.4 0.2 Iris-setosa
4.8 3.0 1.4 0.3 Iris-setosa
5.0 3.0 1.6 0.2 Iris-setosa
4.9 3.1 1.5 0.1 Iris-setosa
4.9 3.1 1.5 0.1 Iris-setosa
  • หากต้องการให้ระบบแสดงเฉพาะคอลัมน์ที่ 1 ถึง 4 สามารถทำได้โดยใช้คำสั่ง awk เข้ามาช่วยในการแสดงผล ดังต่อไปนี้
$ grep  Iris-setosa iris.data | sort -k 2 | head -10 | awk '{ print $1, $2, $3, $4 }'
4.5 2.3 1.3 0.3
4.4 2.9 1.4 0.2
4.3 3.0 1.1 0.1
4.4 3.0 1.3 0.2
4.8 3.0 1.4 0.1
4.9 3.0 1.4 0.2
4.8 3.0 1.4 0.3
5.0 3.0 1.6 0.2
4.9 3.1 1.5 0.1
4.9 3.1 1.5 0.1
  • หากต้องการที่จะนำผลลัพธ์เก็บไปใช้งานต่อในครั้งต่อไป สามารถทำได้โดยบันทึกผลลัพธ์เก็บไว้ในไฟล์ โดยใช้เครื่องหมาย > สามารถทำได้ดังนี้
$ grep  Iris-setosa iris.data | sort -k 2 | head -10 | awk '{ print $1, $2, $3, $4 }' > sample.data
  • จากนั้นตรวจสอบไฟล์ชื่อ sample.data ว่าปรากฎอยู่ในระบบหรือไม่ ด้วยคำสั่ง tree
$ tree
.
├── html
│ └── index.html
├── iris.data
├── sample.data
├── test
└── tmp1
├── index.htm
└── index.html
3 directories, 5 files
  • หากต้องการตรวจสอบข้อมูลที่อยู่ในไฟล์ sample.data สามารถทำได้โดยใช้คำสั่ง cat แสดงดังต่อไปนี้
$ cat sample.data 
4.5 2.3 1.3 0.3
4.4 2.9 1.4 0.2
4.3 3.0 1.1 0.1
4.4 3.0 1.3 0.2
4.8 3.0 1.4 0.1
4.9 3.0 1.4 0.2
4.8 3.0 1.4 0.3
5.0 3.0 1.6 0.2
4.9 3.1 1.5 0.1
4.9 3.1 1.5 0.1
  • ในลินุกซ์สามารถเขียนเชลล์สคริปต์ (shell script) หรือคำสั่งเพื่อให้ระบบทำงานตามที่ต้องการ ตัวอย่างต่อไปนี้แสดงให้เห็นถึงการใช้คำสั่ง awk เพื่อคำนวณหาผลรวมทั้งในแนวคอลัมน์ และแนวแถว สามารถทำได้ดังต่อไปนี้
  • คำสั่งเพื่อให้คำนวณในแนวคอลัมน์
$ cat sample.data | awk '{ c1+= $1;} END {printf "%.2f\n", c1}'
46.90
  • จากตัวอย่าง ผลลัพธ์ที่ได้คือ 46.90 คือผลรวมของคอลัมน์ที่ 1
  • หากต้องการแสดงผลลัพธ์ตั้งแต่คอลัมน์ 1–4 สามารถทำได้ดังตัวอย่างต่อไปนี้
$ cat sample.data | awk '{ c1+= $1; c2+= $2; c3+= $3; c4+= $4} END {printf "%.2f, %.2f, %.2f, %.2f\n", c1,c2,c3,c4}'
46.90, 29.40, 13.90, 1.80
  • จากผลลัพธ์ แสดงผลรวมของคอลัมน์ตั้งแต่คอลัมน์ที่ 1–4 โดยตัวแปล c1, c2, c3 และ c4 ใช้แทน คอลัมน์ที่ 1 ถึงคอลัมน์ที่ 4 ตามลำดับ
  • คำสั่งเพื่อให้คำนวณในแนวแถว
$ cat sample.data | awk '{ for(i=1; i<=NF;i++) l+=$i; print l; l=0 }'
8.4
8.9
8.5
8.9
9.3
9.5
9.5
9.8
9.6
9.6
  • จากตัวอย่างข้างต้น แสดงให้เห็นถึงการคำนวณเพื่อหาผลรวมของแต่ละแถว โดยที่ตัวแปร NF หมายถึง number of fields หมายถึงจำนวนของคอลัมน์ในแต่ละแถว เช่น 4.5 2.3 1.3 0.3 หมายถึงมีจำนวน 4 field หรือ 4 column ดังนั้น (i=1; i<=NF; i++) จึงหมายถึง หาผลรวมทั้งแต่ i=1 ถึง i=4 ดังนั้นผลรวมของแถวแรกจึงเท่ากับ 8.4
  • หากต้องการหาผลรวมของตัวเลขทั้งหมดที่อยู่ในไฟล์ sample.data สามารถทำได้ดังตัวอย่างต่อไปนี้
$ cat sample.data | awk '{ for(i=1; i<=NF;i++) l+=$i; print l; l=0}' | awk '{ c += $1; } END {printf "%.2f\n", c}'
92.00
Photo by Anthony Martino on Unsplash

--

--