Showing posts with label การจำนวน user samba. Show all posts
Showing posts with label การจำนวน user samba. Show all posts

Tuesday, February 7, 2017

การใช้คำสั่ง cut และ grep ในการค้นหาข้อมูล

1. คำสั่ง cut
cut -d":" -f 1 /etc/passwd
root
daemon
bin
sys
sync
games

cut -d":" -f 1,7 /etc/passwd
root:/bin/bash
daemon:/bin/sh
bin:/bin/sh
sys:/bin/sh
sync:/bin/sync
games:/bin/sh

อ่านเพิ่มเติม
command cut

2. คำสั่ง grep
    2.1 grep -n   แสดงบรรทัดของสิ่งที่ค้นหา เช่น

#grep -n root passwd
1:root:x:0:0:root:/root:/bin/bash
12:operator:x:11:0:operator:/root:/sbin/nologin

    2.2 grep -v ไม่แสดงบรรทัดที่มีคำไม่ต้องการ เช่น ไม่แสดงคำว่า root
#grep -v root /etc/passwd

    2.3 grep -E ให้หาคำที่ขึ้นต้นด้วยอะไรสักอย่าง เช่น
#grep -E ^s /etc/passwd

^ใส่ก่อนตัวอักษรหรือตัวเลข ตัวแรก

อ่านเพิ่มเติม
using grep find pattern in file

** กรณีศึกษา **
ตัวอย่างการนับจำนวน user samba
1. ต้องรู้ว่า number ของ user group เช่น group: usershare = 1000 เช็คได้จาก /etc/group
2. ใช้คำสั่งต่อไปนี้

grep -n 1000 /etc/passwd > user01.txt
cut -d":" -f 2 user01.txt > user02.txt

แล้วจะได้รายชื่อ samba ที่อยู่ในกลุ่ม usershare