Qee lub sij hawm cov neeg siv raug teeb meem nrog rau kev xav tau los nrhiav tej ntaub ntawv hauv cov ntaub ntawv. Feem ntau, cov ntaub ntawv cov ntaub ntawv los sis lwm yam ntaub ntawv muaj ntau yam muaj cov kab ntau, yog li nws tsis tuaj yeem nrhiav tau cov ntaub ntawv tsim nyog. Ces ib qho ntawm cov lus tsa built-in rau Linux operating system tuaj rau tus cawm, uas yuav cia koj mus nrhiav cov hlua hauv ib txhia vib nas this xwb.
Siv cov lus hais ua grep hauv Linux.
Raws li qhov sib txawv ntawm Linux distributions, nyob rau hauv cov ntaub ntawv no lawv tsis ua si tej yam hauj lwm, txij thaum lub txib koj xav grep Yog vim, nws muaj nyob rau hauv feem ntau sibtham thiab yog siv raws nraim tib yam. Niaj hnub no peb xav tham txog tsis yog qhov ua xwb grep, tab sis kuj yog los rhuav tshem lub ntsiab lus tseem ceeb uas tuaj yeem yooj yim rau txoj kev tshawb nrhiav.
Saib kuj: Peb tab tom nrhiav cov ntaub ntawv hauv Linux
Npaj ua haujlwm
Tag nrho cov kev ua ntxiv yuav raug ua ntawm ib tus qauv console, nws tseem tso cai rau koj qhib cov ntaub ntawv tsuas yog los ntawm kev hais meej tag nrho txoj kev rau lawv los yog "Terminal" launched los ntawm qhov yuav tsum tau muaj cov npe. Koj tuaj yeem paub cov niam txiv ntawm cov ntaub ntawv thiab mus rau hauv qhov console zoo li no:
- Tua tawm tus thawj tswj xyuas cov ntaub ntawv thiab xa mus rau cov ntawv nplaub tshev.
- Txoj cai-nias rau ntawm cov ntaub ntawv thiab xaiv "Cov khoom".
- Hauv tab "Yooj Yim" nyeem kab "Parent folder".
- Tam sim no khiav "Terminal" yooj yim txoj kev, piv txwv li, los ntawm daim ntawv qhia zaub mov los yog nias lub khawm tseem ceeb Ctrl + Alt + T.
- Ntawm no mus rau ntawm cov npe ntawm cov lus txib
cd / tsev / neeg / khoob
qhov twg tus neeg siv - username, thiab nplaub tshev - nplaub tshev lub npe.
Koom ua kemiv + cov ntaub ntawv npe
yog tias koj xav pom cov ntawv qhia tag nrho. Cov lus qhia ntxaws ntxiv txog kev yuav ua hauj lwm nrog pab neeg no tuaj yeem pom muaj nyob rau hauv peb lwm tsab xov xwm ntawm qhov txuas hauv qab no.
Nyeem ntxiv: Piv txwv ntawm qhov hais kom ua miv hauv Linux
Ua raws li cov kauj ruam saum toj no, koj tuaj yeem siv tau grep, nyob rau hauv qhov yuav tsum tau muaj cov npe, tsis hais meej txog tag nrho txoj kev rau cov ntaub ntawv.
Txuj ci txheej txheem
Ua ntej txoj kev txiav txim siab ntawm tag nrho cov lus sib cav, nws yog ib qho tseem ceeb uas yuav tsum nco ntsoov cov kev tshawb fawb niaj zaus los ntawm cov ntsiab lus. Nws yuav pab tau rau cov sij hawm thaum koj xav nrhiav ib qho yooj yim match los ntawm tus nqi thiab tso saib tag nrho cov laj kab.
- Ntawm qhov hais kom sai, sau
grep lo lus testfile
qhov twg lo lus - cov ntaub ntawv yuav tsum tau, thiab testfile - cov ntaub ntawv npe. Thaum ua ib qho kev tshawb nrhiav sab nraud daim nplaub tshev, qhia meej txog txoj kev taug qab raws qhov piv txwv./ tsev / neeg / khawm / filename
. Tom qab nkag mus rau qhov kev hais kom ua, nyem qhov tseem ceeb Sau. - Nws tshua tsuas yog kom tau cov neeg paub nrog cov kev xaiv muaj. Tag nrho cov kab muaj nyob rau ntawm qhov screen, thiab qhov tseem ceeb yog qhov tseem ceeb hauv cov xim liab.
- Nws yog ib qho tseem ceeb rau kev suav rau hauv cov ntaub ntawv ntawm cov tsiaj ntawv, txij thaum Linux encoding yog tsis optimized rau searching tsis tau noj rau hauv cov account loj los yog me me. Yog tias koj xav siv qhov kev txhais ntawm ib lub npe, sau
grep -i "lo lus" testfile
. - Raws li koj tau pom, nyob rau hauv qhov screenshot tom ntej, qhov tshwm sim tau hloov lawm thiab ib qho ntxiv tshiab kab ntxiv.
Tshawb nrhiav nrog txoj hlua
Qee lub sij hawm cov neeg siv yuav tsum nrhiav tsis tau ib qho kev sib tw nkaus xwb, tab sis kuj yuav tau xyuas cov ntaub ntawv uas tuaj tom qab lawv, piv txwv li, thaum ceeb toom qee qhov yuam kev. Tom qab ntawd qhov kev daws teeb meem yog los mus siv cov cwj pwm. Nkag mus rau hauv lub consolegrep - A3 "lo lus" testfile
kom suav nrog peb cov kab hauv qab no tom qab cov menyuam ncaws pob. Koj tuaj yeem sau tau-A4
, ces plaub kab yuav raug, tsis muaj kev txwv.
Yog hais tias xwb-A
koj thov qhov kev sib cav-B + pes tsawg tus kab
, yog li ntawd, cov ntaub ntawv mus txog qhov pib ntawm kev nkag teb chaws yuav muab tso tawm kom pom.
Sib cav-C
nyob rau hauv lem, captures kab ib ncig ntawm ib lo lus tseem ceeb.
Hauv qab no koj tuaj yeem pom cov piv txwv ntawm txoj haujlwm ntawm cov lus sib tham. Thov nco ntsoov tias nws yog ib qhov tsim nyog yuav tau coj mus rau hauv cov ntaub ntawv nyiaj thiab muab ob chav quotes.
grep -B3 "lo lus" testfile
grep-C3 "lo lus" testfile
Tshawb nrhiav cov ntsiab lus thaum pib thiab qhov kawg ntawm cov kab
Qhov yuav tsum tau txhais ib lo lus tseem ceeb, uas yog thaum pib los yog thaum kawg ntawm ib txoj kab, feem ntau tshwm sim thaum ua hauj lwm nrog cov ntaub ntawv configuration, qhov twg txhua kab yog lub luag haujlwm rau ib qho parameter. Yuav kom pom qhov pib nkag thaum pib, koj yuav tsum sau npegrep "^ lus" testfile
. Kos npe ^ lub luag hauj lwm rau kev siv qhov kev xaiv no.
Tshawb nrhiav rau cov ntsiab lus ntawm qhov kawg ntawm cov kab li tshwm sim nyob rau tib txoj ntsiab cai, tsuas yog hauv cov quotes koj yuav tsum ntxiv cov ua cim $, thiab pab neeg no yuav tau txais daim ntawv foos no:grep "lo lus" testfile
.
Nrhiav rau cov zauv
Thaum tshawb nrhiav qhov tseem ceeb xav tau, tus neeg siv tsis tas yuav muaj ntaub ntawv hais txog lo lus qhia tam sim no hauv txoj hlua. Tom qab ntawd txoj kev tshawb no yuav ua tau los ntawm cov zauv, uas tej zaum kuj ua rau sim ua haujlwm. Nws tsuas yog tsim nyog siv cov lus txib hauv nqe lus nug hauv daim ntawvgrep "[0-7]" testfile
qhov twg «[0-7]» - ntau ntawm qhov tseem ceeb, thiab testfile - ua ntaub ntawv rau luam theej duab.
Kev soj ntsuam ntawm cov ntaub ntawv teev keeb kwm
Kev xa tag nrho cov khoom hauv tib phau ntawv hu ua recursive. Tus neeg siv yuav tsum tau thov tsuas yog ib qho kev sib cav, uas txheeb xyuas tag nrho cov ntaub ntawv hauv daim nplaub tshev thiab qhia txog cov kab tsim nyog thiab lawv qhov chaw. Koj yuav tsum nkag musgrep -r "lo lus" / tsev / neeg / khoob
qhov twg / tsev / neeg / khoob - kev mus rau cov npe rau kev luam.
Qhov chaw uas cov ntaub ntawv tseg cia yuav tshwm nyob rau hauv xiav, thiab yog tias koj xav tau cov kab tsis muaj cov ntaub ntawv no, muab lwm cov lus sib tham ua kom tiavgrep -h -r "lo lus" + txoj kab ke
.
Txhais lus lo lus
Thaum pib ntawm tsab xov xwm peb twb tau tham txog kev tshawb nrhiav li ib txwm. Txawm li cas los xij, nrog rau hom no ntxiv ua ke yuav tshwm sim hauv cov txiaj ntsig. Piv txwv, koj pom lo lus Tus neeg siv, tab sis qhov hais kom ua kuj pom cov neeg siv123, Lo lus zaisTus neeg siv thiab lwm qhov khub, yog tias muaj. Txhawm kom zam qhov no, muab qhov kev sib cav-w
(grep -w "lo lus" + cov ntaub ntawv npe lossis qhov chaw
).
Qhov kev xaiv no yog tseg txawm tias koj yuav tsum tau nrhiav ob peb lo lus tseem ceeb ntawm ib zaug. Hauv qhov no, sauegrep -w 'word1 | word2' testifile
. Thov nco ntsoov tias hauv qhov no grep ib tsab ntawv ntxiv e, thiab quotes yog ib.
Nrhiav cov hlua tsis muaj ib lo lus.
Lub chaw tso dej muaj nqis yuav tsis tuaj yeem nrhiav cov lus hauv cov ntaub ntawv, tab sis kuj mus tso cov kab nyob rau hauv uas tsis muaj tus neeg siv txhais. Ces ua ntej nkag mus rau tus nqi tseem ceeb thiab cov ntaub ntawv ntxiv-v
. Tsaug rau nws, thaum koj qhib qhov kev hais kom ua, koj yuav pom xwb cov ntaub ntawv tseem ceeb.
Syntax grep sau ob peb nqe lus ntxiv, uas tau tham luv luv:
- Kuv
- qhia cov npe ntawm cov ntaub ntawv uas sib npaug rau cov ntsiab lus nrhiav;-S
- lov tes taw cov ntawv ceeb toom txog cov teeb meem pom;-n
- tso saib tus xov tooj hauv cov ntaub ntawv;-b
- Qhia tus lej tooj ua ntej kab.
Tsis muaj dab tsi thaiv koj los ntawm kev thov ntau nqe lus rau ib qho kev tshawb nrhiav, cia li nkag mus rau lawv sib cais los ntawm qhov chaw, tsis nco qab mus siv rooj plaub.
Niaj hnub no peb tau disassembled pawg neeg hauv kev nthuav dav grepmuaj nyob ntawm Linux faib. Nws yog ib qho ntawm cov qauv thiab nquag siv. Koj tuaj yeem nyeem txog lwm yam cuab yeej ntiag tug thiab lawv cov syntax nyob rau hauv peb cov khoom sib cais ntawm qhov txuas nram qab no.
Saib kuj: Nquag Siv Commands hauv Linux Terminal