Linux grep hais kom ua piv txwv

Pin
Send
Share
Send

Qee zaum cov neeg siv tau ntsib nrog qhov xav tau los tshawb txog qee yam ntaub ntawv sab hauv ntawm txhua cov ntaub ntawv. Feem ntau, cov ntaub ntawv teeb tsa lossis lwm cov ntaub ntawv voluminous muaj cov kab ntau, yog li nws tsis tuaj yeem nrhiav tau cov ntaub ntawv tsim nyog. Tom qab ntawd ib qho ntawm cov lus txib nyob hauv Linux kev khiav hauj lwm rau kev cawm, uas yuav cia koj mus nrhiav cov kab lus raws li ob peb feeb.

Siv grep hais kom ua rau Linux

Raws li rau qhov sib txawv ntawm Linux kev faib tawm, qhov no lawv tsis ua lub luag haujlwm, txij li pab pawg koj txaus siab grep Los ntawm lub neej ntawd, nws muaj nyob rau hauv cov rooj sib txoos feem ntau thiab tau thov kom zoo ib yam. Niaj hnub no peb xav sib tham tsis yog tsuas yog ua grep, tab sis kuj tseem yuav txheeb xyuas cov kev sib cav lub ntsiab uas tuaj yeem yooj yim rau kev tshawb nrhiav.

Saib ntxiv mus: Nrhiav cov ntaub ntawv hauv Linux

Npaj ua haujlwm

Tag nrho cov kev nqis tes ua ntxiv yuav raug ua los ntawm cov txheej txheem txheem, nws tseem tso cai rau koj qhib cov ntaub ntawv tsuas yog los ntawm kev qhia meej txoj kev puv rau lawv, lossis yog "Chaw Nres Tsheb" tua tawm ntawm cov ntawv tsim nyog tsim nyog. Koj tuaj yeem pom cov niam txiv daim nplaub tshev ntawm cov ntawv pov tseg thiab taw rau nws hauv cov console zoo li no:

  1. Khiav cov ntaub ntawv tus thawj tswj thiab rau ntawm lub nplaub tshev yam.
  2. Txoj cai-nias rau cov ntaub ntawv xav tau thiab xaiv "Khoom".
  3. Nyob hauv tab "Pib" nyeem kab "Cov niam txiv ntawv tais ceev tseg".
  4. Tam sim no khiav "Chaw Nres Tsheb" cov qauv yooj yim, piv txwv li, los ntawm cov ntawv qhia zaub mov lossis los ntawm nias cov sib xyaw tseem ceeb Ctrl + kev + T.
  5. Ntawm no, mus rau cov ntawv qhia chaw los ntawm cov lus txibcd / tsev / neeg siv / nplaub tshevqhov twg neeg siv - Lub npe username, thiab ntawv tais ceev tseg - lub npe ntawm daim nplaub tshev.

Koom tes nrog pab pawgmiv + ntawv npeyog tias koj xav mus saib cov ncauj lus tag nrho. Yog xav paub cov lus qhia ntxaws ntxaws ntawm kev ua haujlwm nrog pab pawg no, saib peb lwm tsab xov xwm ntawm qhov txuas hauv qab no.

Nyeem Ntau: Cat Command Piv Txwv rau Linux

Los ntawm kev ua rau saum toj no, koj tuaj yeem siv grep, nyob rau hauv cov ntaub ntawv tsim nyog, tsis muaj kev qhia meej tag nrho txoj kev rau cov ntawv.

Txheeb Xyuas Ntsiab Lus Tshawb Nrhiav

Ua ntej hloov mus rau kev txiav txim siab ntawm txhua qhov kev sib cav, nws yog ib qho tseem ceeb kom nco ntsoov tshawb nrhiav cov ncauj lus tsis tu ncua. Nws yuav muaj txiaj ntsig zoo nyob rau hauv cov sijhawm ntawd thaum koj xav nrhiav qhov yooj yim kom phim los ntawm tus nqi thiab tso saib txhua kab uas cuam tshuam ntawm lub vijtsam.

  1. Ntawm tus txheej txheem hais kom ua, nkag musgrep cov lus simqhov twg lo lus - cov ntaub ntawv nrhiav, thiab noob qes - cov ntaub ntawv npe. Thaum tshawb sab nraud daim nplaub tshev, qhia kom meej tag nrho txoj hauv kev piv txwv/ home / user / folder / filenameCov. Tom qab nkag mus rau cov lus txib, nias tus yuam sij Sau.
  2. Nws tshua tsuas yog yuav ua kom paub koj tus kheej nrog cov kev xaiv muaj. Tag nrho cov kab yuav tau tshwm rau ntawm lub vijtsam, thiab tus nqi tseem ceeb yuav tau muab liab.
  3. Nws yog ib qho tseem ceeb kom muaj kev cuam tshuam cov ntaub ntawv ib yam nkaus, txij li Linux encoding tsis ua qhov zoo rau kev tshawb nrhiav yam tsis muaj cov cim loj lossis me. Yog tias koj xav hla txoj kev txhais ntawm kev sau npe, nkag musgrep -i "lo lus" testfile.
  4. Raws li koj tuaj yeem pom, hauv daim duab thaij tom ntej, cov txiaj ntsig tau hloov thiab lwm cov kab tshiab tau ntxiv.

Txoj hlua Nrhiav Kev Nrhiav

Qee lub sij hawm cov neeg siv yuav tsum tau nrhiav tsis tau tsuas yog sib phim ntawm cov kab, tab sis kuj pom cov ntaub ntawv uas tuaj tom qab lawv, piv txwv li, thaum qhia txog qhov yuam kev tshwj xeeb. Tom qab ntawv txoj kev txiav txim siab zoo yuav raug siv cov cwj pwm. Ntaus hauv lub consolegrep -A3 "lo lus" testfilekom suav nrog hauv qhov tshwm sim thiab ntxiv rau peb kab tom qab ntais ntawv. Koj tuaj yeem sau-4, tom qab ntawd plaub kab yuav raug ntes, tsis muaj kev txwv.

Yog hais tias hloov-Qhov-koj siv qhov kev sib cav-B + kab suav, raws li qhov tshwm sim, cov ntaub ntawv nyob ua ntej nkag mus kis.

Sib cav-C, nyob rau hauv lem, captures cov kab ib ncig ntawm lub ntsiab lus tseem ceeb.

Hauv qab no koj tuaj yeem pom cov piv txwv ntawm kev muab haujlwm ntawm cov lus sib cav tshwj xeeb. Thov nco ntsoov tias koj yuav tsum yog kis txooj xwm thiab siv ob kab cim sau.

grep -B3 "lo lus" testfile
grep -C3 "lo lus" testfile

Nrhiav cov ntsiab lus tseem ceeb thaum pib thiab xaus ntawm kab

Qhov xav tau los txhais cov ntsiab lus tseem ceeb thaum pib lossis tom kawg ntawm txoj kab feem ntau tshwm sim thaum ua haujlwm nrog cov ntaub ntawv teeb tsa, qhov twg txhua kab yog lub luag haujlwm rau ib qho ntsuas. Txhawm rau pom qhov tseeb nkag thaum pib, nws yog qhov tsim nyog sau npegrep "^ lo lus" testfileCov. Kos npe ^ tsuas yog lub luag haujlwm rau kev siv qhov kev xaiv no.

Kev tshawb rau cov ntsiab lus tom kawg ntawm txoj kab ua raws li kwv yees li qhov qub, tsuas yog hauv cov cim qhia koj yuav tsum tau ntxiv tus cwj pwm $, thiab cov lus txib yuav zoo li no:grep "lo lus $" xeem lus.

Tshawb nrhiav naj npawb

Thaum tshawb rau cov txiaj ntsig yam xav tau, tus neeg siv yuav tsis ib txwm muaj ntaub ntawv hais txog cov lus tseeb tam sim no hauv kab. Tom qab ntawd cov txheej txheem tshawb nrhiav tuaj yeem ua tiav los ntawm cov lej, uas qee zaum ua tau yooj yim dua cov haujlwm. Nws tsuas yog tsim nyog los siv pawg hauv nug hauv daim ntawvgrep "[0-7]" cov ntawv pov thawjqhov twg «[0-7]» - ntau ntawm qhov tseem ceeb, thiab noob qes - lub npe ntawm cov ntaub ntawv luam theej duab.

Kev txheeb xyuas ntawm txhua cov ntaub ntawv npe

Kev tshuaj xyuas txhua yam khoom nyob hauv ib lub nplaub tshev hu ua recursive. Tus neeg siv yuav tsum tau siv tsuas yog ib qho kev sib cav, uas tshawb xyuas tag nrho cov ntaub ntawv hauv daim nplaub tshev thiab qhia cov kab tsim nyog thiab lawv qhov chaw nyob. Yuav tsum tau nkag musgrep -r "lo lus" / tsev / neeg siv / nplaub tshevqhov twg / tsev / neeg siv / nplaub tshev - txoj hauv kev rau lub npe rau scanning.

Cov chaw cia cov ntaub ntawv yuav tso tawm kom pom xiav, thiab yog tias koj xav tau txais cov kab tsis muaj cov ntaub ntawv no, cob qhia lwm qhov kev sib cav kom cov lus txib hloov tawm zoo li nogrep -h -r "lo lus" + nplaub tshev txoj kev.

Tshawb cov lus tseeb

Thaum pib ntawm tsab xov xwm, peb twb tau hais txog qhov ib txwm tau tshawb cov lus. Txawm li cas los xij, nrog hom no, kev sib txuas ntxiv yuav tshwm nyob rau hauv cov txiaj ntsig. Piv txwv li, koj pom lo lus Tus Neeg Sivtab sis cov lus txib tseem yuav tso Tus Neeg Siv123, Lo lus zaisTus neeg siv thiab lwm qhov sib tw, yog tias muaj. Txhawm rau zam qhov tshwm sim no, cob cov lus sib cav-w(grep -w "lo lus" + cov ntaub ntawv npe lossis chaw nyob).

Qhov kev xaiv no tseem ua tau yog tias nws yog qhov yuav tsum tau tshawb rau ntau lub ntsiab lus ntawm ib lub sijhawm. Hauv qhov no, nkag musegrep -w 'word1 | lus2' pov thawjCov. Thov nco ntsoov tias hauv qhov no grep tsab ntawv ntxiv e, thiab cov lus hais cim yog ib leeg.

Nrhiav rau cov hlua yam tsis muaj ib lo lus tshwj xeeb

Cov nqi hluav taws xob hauv nqe lus nug tsis tuaj yeem tsuas pom cov lus nyob hauv cov ntaub ntawv, tab sis kuj qhia cov kab uas tsis muaj tus neeg siv txhais tus nqi. Tom qab ntawd ua ntej nkag mus rau tus nqi tseem ceeb thiab cov ntaub ntawv ntxiv-vCov. Ua tsaug rau nws, thaum koj qhib ib qho kev txib, koj yuav pom tsuas yog cov ntaub ntawv cuam tshuam.

Syntax grep sib sau ob peb yam sib cav uas tuaj yeem piav qhia luv luv:

  • -I- tsuas yog cov npe ntawm cov ntaub ntawv txuam nrog cov txheej txheem tshawb;
  • -s- lov tes taw cov ntawv qhia ntawm cov uas tsis pom;
  • -n- muab tus lej kab hauv cov ntaub ntawv;
  • -b- Qhia tus lej thaiv kev ua ntej ua kab.

Tsis muaj dab tsi thaiv koj ntawm kev siv ntau cov lus sib cav rau ib qho kev tshawb pom ib zaug, tsuas yog nkag mus rau lawv sib cais los ntawm tej qhov chaw, nco ntsoov tias yog rooj plaub.

Niaj hnub no peb rhuav tshem pab neeg hauv kev nthuav dav grepmuaj nyob ntawm Linux faib khoom. Nws yog ib qho ntawm cov qauv thiab nquag siv. Koj tuaj yeem nyeem txog lwm cov cuab yeej nrov thiab lawv cov lus hauv peb cov khoom siv cais ntawm qhov txuas hauv qab no.

Saib ntxiv: Cov Lus Qhia Uas Siv Ntau Dhau Hauv Linux Terminal

Pin
Send
Share
Send