Bạn có chắc chắn muốn xóa bài viết này không ?
Bạn có chắc chắn muốn xóa bình luận này không ?
How to become a lazy dev with Linux cut command
Tiêu đề bài viết không ngoài gì khác ngoài mục đích câu view, nội dung không có gì cao siêu cả.
What is the cut command in UNIX?
cut - là câu lệnh thường được dùng để xử lý text, bạn có thể sử dụng lệnh này để trích xuất một phần text từ một file bằng cách chọn columns.
Option | Meaning |
---|---|
-b BYTE-LIST--bytes=BYTE-LIST | Print only the bytes in positions listed in BYTE-LIST. Tabs and backspaces are treated like any other character; they take up 1 byte. |
-c CHARACTER-LIST--characters=CHARACTER-LIST | Print only characters in positions listed in CHARACTER-LIST. The same as '-b' for now, but internationalization will change that. Tabs and backspaces are treated like any other character; they take up 1 character |
-f FIELD-LIST--fields=FIELD-LIST | Print only the fields listed in FIELD-LIST. Fields are separated by a TAB character by default. |
-d INPUT_DELIM_BYTE--delimiter=INPUT_DELIM_BYTE | For '-f', fields are separated in the input by the first character in INPUT_DELIM_BYTE (default is TAB). |
-n | Do not split multi-byte characters (no-op for now). |
-s--only-delimited | For '-f', do not print lines that do not contain the field separator character. |
--output-delimiter=OUTPUT_DELIM_STRING | For '-f', output fields are separated by OUTPUT_DELIM_STRING The default is to use the input delimiter. |
Chúng ta có file test.txt với nội dung như sau:
➜ ~ cat test.txt
Hello World!
My name is Tobi.
➜ ~ cut -c2 test.txt
e
y
Với câu lệnh trên, nó sẽ hiện thị tất cả kí tự ở cột thứ 2.
Ta cũng có thể chọn một khoảng columns với câu lệnh sau:
➜ ~ cut -c1-5 test.txt
Hello
My na
➜ ~ cut -c2- test.txt
ello World!
y name is Tobi.
➜ ~ cut -c-8 test.txt
Hello Wo
My name
How to select a specific field from a File
Chúng ta có thể sử dụng tham số -f, -d để xử lý những yêu cầu đặc biệt hơn. Option -d dùng để xác định kí tự nào sẽ được dùng làm kí tự phân cách để chia đoạn text đó ra tương tự như dùng method .split trong ruby. Option -f xác định field nào chúng ta muốn lấy.
Với file passwd sau:
➜ ~ cat /etc/passwd
nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false
root:*:0:0:System Administrator:/var/root:/bin/sh
daemon:*:1:1:System Services:/var/root:/usr/bin/false
_uucp:*:4:4:Unix to Unix Copy Protocol:/var/spool/uucp:/usr/sbin/uucico
_taskgated:*:13:13:Task Gate Daemon:/var/empty:/usr/bin/false
_networkd:*:24:24:Network Services:/var/networkd:/usr/bin/false
_installassistant:*:25:25:Install Assistant:/var/empty:/usr/bin/false
➜ ~ cut -d':' -f1 /etc/passwd
nobody
root
daemon
_uucp
_taskgated
_networkd
_installassistant
Phân tách theo dấu hai chấm và lấy trường đầu tiên để hiện các username, tương tự ta có thể đổi thành f2 f3.. fn để thấy các thông tin về User ID, Group ID,..
Ngoài ra chúng ta có thể lấy nhiều fields cùng một lúc.
➜ ~ cut -d':' -f1,5 /etc/passwd
nobody:Unprivileged User
root:System Administrator
daemon:System Services
_uucp:Unix to Unix Copy Protocol
_taskgated:Task Gate Daemon
_networkd:Network Services
_installassistant:Install Assistant
How to select fields only when a line contains the delimiter
Thêm -s để chỉ hiện thị những dòng có chứa kí tự phân cách.
Với tham số --complement và —output mình không thể test được trên MacOS nên sẽ dừng lại ở đây.
To be a lazy dev
Để truy cập một pod trước đó mình sẽ làm như sau:
➜ ~ kubectl get pods --namespace development
NAME READY STATUS RESTARTS AGE
service_one-5749cd88cb 1/1 Running 0 18m
service_two-5pqdl 1/1 Running 0 1h
service_three-zttgk 1/1 Running 0 1h
Sau đó copy tên của service_one để chạy cmd tiếp theo:
➜ ~ kubectl exec -it service_one-5749cd88cb --namespace development -- /bin/bash
Sau khi lặp đi lặp lại thao tác copy paste một cảm giác lười biến xuất hiện và chúng ta có file console
#!/bin/bash
name=$1
command=/bin/bash
# Get lastest line then take the first word.
pod=$(./kubectl get pods -l name="${name}" --no-headers | tail -n 1 | cut -d ' ' -f 1)
if [ -z $pod ]; then
exit 255
fi
./kubectl exec ${pod} -it -- ${command}
Sau đó mình chỉ cần gõ như sau để truy cập:
➜ ~ ./console service_one
"Bài viết này sẽ giúp tui tăng lương thế nào?" * by Mr Quan Cam.
Bài viết thể hiện độ lười của bản thân trong công việc, không mong sếp tặng thêm coins để lại tốn công chuyển vào ví lạnh.




