grep เพื่อละเว้นรูปแบบ


12

ฉันกำลังดึง URL จากเว็บไซต์โดยใช้ cURL ดังต่อไปนี้

curl www.somesite.com | grep "<a href=.*title=" > new.txt

ไฟล์ new.txt ของฉันมีดังต่อไปนี้

<a href="http://website1.com" title="something">
<a href="http://website1.com" information="something" title="something">
<a href="http://website2.com" title="some_other_thing">
<a href="http://website2.com" information="something" title="something">
<a href="http://websitenotneeded.com" title="something NOTNEEDED">

อย่างไรก็ตามฉันต้องดึงข้อมูลด้านล่างเท่านั้น

<a href="http://website1.com" title="something">
<a href="http://website2.com" information="something" title="something">

ฉันพยายามที่จะไม่สนใจ<a hrefที่มีข้อมูลในพวกเขาและท้ายที่สุดกับชื่อNOTNEEDED

ฉันจะแก้ไขคำสั่ง grep ได้อย่างไร


ผลลัพธ์ที่คุณแสดงที่นี่ถูกต้องหรือไม่ ข้อความที่อธิบายไม่สมเหตุสมผลตามตัวอย่างนี้
slm

1
คุณไม่ต้องการcurl www.somesite.com | grep "<a href=.*title=" | grep -v NOTNEEDED > new.txtหรือ
terdon

@terdon นั่นคือสิ่งที่ฉันกำลังมองหา ฉันยอมรับได้ว่าเป็นคำตอบถ้าคุณโพสต์ไว้
Ramesh

Ramesh เป็นคำตอบโดยพื้นฐานของ @ slm ฉันเพิ่งแก้ไขเพื่อให้คุณสามารถยอมรับได้
terdon

โอ้ใช่ฉันไม่ได้ตระหนักถึงท่อที่มีประสิทธิภาพนี้ ฉันยอมรับมันเป็นคำตอบ ขอบคุณ!
Ramesh

คำตอบ:


17

ฉันไม่ได้ทำตามตัวอย่างของคุณ + คำอธิบายอย่างสมบูรณ์ แต่ดูเหมือนว่าสิ่งที่คุณต้องการคือ:

$ grep -v "<a href=.*title=.*NOTNEEDED" sample.txt 
<a href="http://website1.com" title="something">
<a href="http://website1.com" information="something" title="something">
<a href="http://website2.com" title="some_other_thing">
<a href="http://website2.com" information="something" title="something">

ดังนั้นสำหรับตัวอย่างของคุณ:

$ curl www.example.com | grep -v "<a href=.*title=" | grep -v NOTNEEDED > new.txt

ฉันมีชั้นเรียนในส่วน <a href โดยพื้นฐานแล้วฉันไม่ต้องการสิ่งนั้นในผลลัพธ์ของฉัน
Ramesh

9

grepหน้าคนพูดว่า:

-v, --invert-match
    Invert the sense of matching, to select non-matching lines. (-v is specified by POSIX .) 

คุณสามารถใช้นิพจน์ปกติสำหรับผู้เรียกหลายคน:

grep -v 'red\|green\|blue'

หรือ

grep -v red | grep -v green | grep -v blue
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.