Python khớp chuỗi chính xác trong tệp

Làm cách nào để tìm đối sánh chính xác cho chuỗi hoặc mẫu từ một tệp trong Linux hoặc Unix. Cách tìm kiếm mẫu chính xác. Làm thế nào để grep khớp chính xác với các ví dụ?

Trong hướng dẫn này, tôi sẽ chia sẻ nhiều lệnh có thể được sử dụng để grep chính xác từ hoặc chuỗi hoặc mẫu từ một tệp. Tôi đã cố gắng đề cập đến một số tình huống phổ biến, hãy cho tôi biết nếu bạn gặp phải bất kỳ vấn đề nào hoặc có yêu cầu bổ sung và tôi có thể cố gắng giúp bạn qua phần bình luận

Dưới đây là tệp mẫu của tôi để minh họa tất cả các ví dụ và tình huống từ hướng dẫn này

# cat /tmp/somefile
first line ABCD some text
second line abcd some text
third line12abcde some text
fourth line 12.abcd.32 some text
fifth line s(abcd)e some text
sixth line 1234abcd some text
seventh line 1abcd234 some text
eighth line 234abcd1 some text
abcd some text
abcd

 

grep khớp chính xác với -w

Bây giờ với grep, chúng ta có một đối số (

# grep -w abcd /tmp/somefile
second line abcd some text
fourth line 12.abcd.32 some text
fifth line s(abcd)e some text
abcd some text
abcd
1) được sử dụng để grep đối sánh chính xác toàn bộ từ trong một tệp

# grep -w abcd /tmp/somefile
second line abcd some text
fourth line 12.abcd.32 some text
fifth line s(abcd)e some text
abcd some text
abcd

Như bạn quan sát, nó đã lọc đầu ra bằng cách loại bỏ kết quả khớp không liên quan mặc dù grep không thành công 100%. Từ trang người đàn ông của grep

-w, --word-regexp
     Select  only  those  lines  containing  matches  that  form  whole  words.  The test is that the matching
     substring must either be at the beginning of the line, or preceded by a non-word  constituent  character.
     Similarly,  it  must  be  either  at the end of the line or followed by a non-word constituent character.
     Word-constituent characters are letters, digits, and the underscore.

 

CŨNG ĐỌC. 10 lệnh cơ bản và mạnh mẽ để kiểm tra loại hệ thống tệp trong Linux/Unix

Phương pháp 1. grep cho ký tự đầu tiên và cuối cùng

Chúng ta có thể grep một đối sánh chính xác bằng cách đặt một đối sánh regex của bắt đầu (

# grep -w abcd /tmp/somefile
second line abcd some text
fourth line 12.abcd.32 some text
fifth line s(abcd)e some text
abcd some text
abcd
2) và kết thúc (
# grep -w abcd /tmp/somefile
second line abcd some text
fourth line 12.abcd.32 some text
fifth line s(abcd)e some text
abcd some text
abcd
3) char. Vì chúng tôi đang lên kế hoạch grep cho "
# grep -w abcd /tmp/somefile
second line abcd some text
fourth line 12.abcd.32 some text
fifth line s(abcd)e some text
abcd some text
abcd
4", lệnh của chúng tôi sẽ là

# grep -E "^abcd$" /tmp/somefile
abcd
  • Nhưng nếu bạn quan sát, lệnh này không bắt được các dòng khác có chứa "
    # grep -w abcd /tmp/somefile
    second line abcd some text
    fourth line 12.abcd.32 some text
    fifth line s(abcd)e some text
    abcd some text
    abcd
    4"
  • Bạn phải hiểu rằng, khi chúng ta định nghĩa regex để tìm ký tự đầu
    # grep -w abcd /tmp/somefile
    second line abcd some text
    fourth line 12.abcd.32 some text
    fifth line s(abcd)e some text
    abcd some text
    abcd
    6 và cuối
    # grep -w abcd /tmp/somefile
    second line abcd some text
    fourth line 12.abcd.32 some text
    fifth line s(abcd)e some text
    abcd some text
    abcd
    7, nó có nghĩa là ký tự đầu tiên và ký tự cuối cùng trên toàn bộ dòng
  • Vì chỉ có một lần xuất hiện như vậy khi dòng bắt đầu bằng "
    # grep -w abcd /tmp/somefile
    second line abcd some text
    fourth line 12.abcd.32 some text
    fifth line s(abcd)e some text
    abcd some text
    abcd
    8" và kết thúc bằng "
    # grep -w abcd /tmp/somefile
    second line abcd some text
    fourth line 12.abcd.32 some text
    fifth line s(abcd)e some text
    abcd some text
    abcd
    9", nên có một đầu ra duy nhất
  • Điều này không hoạt động nếu bạn phải tìm bất cứ thứ gì ở đâu đó ở giữa dòng

Nếu chuỗi của bạn bắt đầu thì bạn chỉ có thể sử dụng

# grep -w abcd /tmp/somefile
second line abcd some text
fourth line 12.abcd.32 some text
fifth line s(abcd)e some text
abcd some text
abcd
6

# grep -w abcd /tmp/somefile
second line abcd some text
fourth line 12.abcd.32 some text
fifth line s(abcd)e some text
abcd some text
abcd
4

 

Phương pháp 2. Khớp văn bản với các ký tự khoảng trắng

Chúng tôi chỉ có thể tìm kiếm đối sánh chính xác với các ký tự khoảng trắng ở đầu hoặc cuối để chúng tôi biết rằng đó là đối sánh chính xác

Quảng cáo

# grep -w abcd /tmp/somefile
second line abcd some text
fourth line 12.abcd.32 some text
fifth line s(abcd)e some text
abcd some text
abcd
5

Điều này mang lại cho chúng tôi đầu ra hoàn hảo của tất cả các dòng có khớp chính xác "

# grep -w abcd /tmp/somefile
second line abcd some text
fourth line 12.abcd.32 some text
fifth line s(abcd)e some text
abcd some text
abcd
4". Hoặc sử dụng "
-w, --word-regexp
     Select  only  those  lines  containing  matches  that  form  whole  words.  The test is that the matching
     substring must either be at the beginning of the line, or preceded by a non-word  constituent  character.
     Similarly,  it  must  be  either  at the end of the line or followed by a non-word constituent character.
     Word-constituent characters are letters, digits, and the underscore.
2" thay vì khoảng trắng
-w, --word-regexp
     Select  only  those  lines  containing  matches  that  form  whole  words.  The test is that the matching
     substring must either be at the beginning of the line, or preceded by a non-word  constituent  character.
     Similarly,  it  must  be  either  at the end of the line or followed by a non-word constituent character.
     Word-constituent characters are letters, digits, and the underscore.
3

# grep -w abcd /tmp/somefile
second line abcd some text
fourth line 12.abcd.32 some text
fifth line s(abcd)e some text
abcd some text
abcd
9

 

CŨNG ĐỌC. 10 ví dụ thực tế để thêm hoặc xóa người dùng khỏi nhóm trong Linux

Phương pháp 3. Nối đầu và cuối từ

Với biểu thức chính quy mở rộng grep, chúng ta có thể khớp phần đầu và phần cuối của từ

# grep -w abcd /tmp/somefile
second line abcd some text
fourth line 12.abcd.32 some text
fifth line s(abcd)e some text
abcd some text
abcd
0

Ở đây

-w, --word-regexp
     Select  only  those  lines  containing  matches  that  form  whole  words.  The test is that the matching
     substring must either be at the beginning of the line, or preceded by a non-word  constituent  character.
     Similarly,  it  must  be  either  at the end of the line or followed by a non-word constituent character.
     Word-constituent characters are letters, digits, and the underscore.
4 có nghĩa là, kết thúc bằng khoảng trắng hoặc ở cuối dòng trong khi
-w, --word-regexp
     Select  only  those  lines  containing  matches  that  form  whole  words.  The test is that the matching
     substring must either be at the beginning of the line, or preceded by a non-word  constituent  character.
     Similarly,  it  must  be  either  at the end of the line or followed by a non-word constituent character.
     Word-constituent characters are letters, digits, and the underscore.
5 được coi là ranh giới từ và nó khớp với chuỗi trống ở cạnh của một từ. Bạn cũng có thể đã sử dụng

# grep -w abcd /tmp/somefile
second line abcd some text
fourth line 12.abcd.32 some text
fifth line s(abcd)e some text
abcd some text
abcd
3

HOẶC sử dụng

-w, --word-regexp
     Select  only  those  lines  containing  matches  that  form  whole  words.  The test is that the matching
     substring must either be at the beginning of the line, or preceded by a non-word  constituent  character.
     Similarly,  it  must  be  either  at the end of the line or followed by a non-word constituent character.
     Word-constituent characters are letters, digits, and the underscore.
6 để khớp với một hoặc nhiều ký tự khoảng trắng trong khi đặt \b ở cuối văn bản

# grep -w abcd /tmp/somefile
second line abcd some text
fourth line 12.abcd.32 some text
fifth line s(abcd)e some text
abcd some text
abcd
5

 

Phương pháp 4. Khớp với các số trong chuỗi

Bây giờ chúng tôi sẽ cố gắng in tất cả các dòng có

-w, --word-regexp
     Select  only  those  lines  containing  matches  that  form  whole  words.  The test is that the matching
     substring must either be at the beginning of the line, or preceded by a non-word  constituent  character.
     Similarly,  it  must  be  either  at the end of the line or followed by a non-word constituent character.
     Word-constituent characters are letters, digits, and the underscore.
7. Ở đây, chuỗi của chúng tôi chứa các số nguyên, bây giờ giả sử chúng tôi không biết các giá trị số nguyên có thể ở đó, tất cả những gì chúng tôi biết là có một số số nguyên khi bắt đầu "
# grep -w abcd /tmp/somefile
second line abcd some text
fourth line 12.abcd.32 some text
fifth line s(abcd)e some text
abcd some text
abcd
4"

# grep -w abcd /tmp/somefile
second line abcd some text
fourth line 12.abcd.32 some text
fifth line s(abcd)e some text
abcd some text
abcd
0

Ở đây,

-w, --word-regexp
     Select  only  those  lines  containing  matches  that  form  whole  words.  The test is that the matching
     substring must either be at the beginning of the line, or preceded by a non-word  constituent  character.
     Similarly,  it  must  be  either  at the end of the line or followed by a non-word constituent character.
     Word-constituent characters are letters, digits, and the underscore.
9 sẽ khớp với một hoặc nhiều số nguyên theo sau là "
# grep -w abcd /tmp/somefile
second line abcd some text
fourth line 12.abcd.32 some text
fifth line s(abcd)e some text
abcd some text
abcd
4" trong khi
# grep -E "^abcd$" /tmp/somefile
abcd
1 sẽ đảm bảo chuỗi nằm ở cuối dòng hoặc có ký tự khoảng trắng ở cuối dòng

CŨNG ĐỌC. Cách bật Wake-on-LAN trong Ubuntu 22. 04?

 

Phần kết luận

Trong hướng dẫn này, tôi đã chỉ cho bạn nhiều ví dụ về grep để khớp chính xác mẫu hoặc chuỗi bằng cách sử dụng biểu thức chính quy. Lệnh chính xác có thể khác nhau tùy theo yêu cầu của bạn, đây là một số trường hợp sử dụng phổ biến mà bạn có thể khớp chính xác grep với một số biểu thức chính quy cơ bản

Cuối cùng, tôi hy vọng hướng dẫn này để tìm kiếm và in kết quả khớp chính xác trong Linux và Unix là hữu ích. Vì vậy, hãy cho tôi biết đề xuất và phản hồi của bạn bằng cách sử dụng phần bình luận