Faka okunye esikhundleni sombhalo ngaphakathi kwamafayela ngomyalo owodwa nje: perl

Ngezikhathi ezithile sifuna ukufaka umbhalo ngaphakathi kwefayela, ngokwesibonelo, change in Idokhumenti.txt konke okushiwo "umndeni wami" ngathi. Ukufeza lokhu singasebenzisa isihleli sethu sombhalo esithandayo (i-Kate, Gedit, SublimeText, njll) kepha kuhle ngaso sonke isikhathi ukwazi okwehlukile, ezinye izindlela zokuthola umphumela ofanayo 🙂

Kwenzeka lokho nge izinkulumo ezivamile lokhu kungafinyelelwa, umyalo we-sed umane nje muhle kakhulu, unokuhluka okungapheli nezinketho kepha ... kulabo abafuna nje ukufaka umbhalo kusuka ku- okungenani indlela eyinkimbinkimbi engenzeka, kubo ukuthi ngiletha okuhlukile okulula kakhulu:

Ukulandela isibonelo, sinefayela Idokhumenti.txt etholakala $ HOME / Document.txt ngombhalo olandelayo:

(...) umkhaya wami Kufana nabo bonke, njengeyakho noma eyomakhelwane wakho. Empeleni umkhaya wami Sizama ukuba ngabantu abanokwethenjelwa futhi emehlweni omphakathi, silungile kwezepolitiki nakwezenhlalo. Kodwa-ke, endlini yami kukhona imvu emnyama, futhi leyo yindima yami LOL!. Bengihlale ngingulowo ongesabi ukusho lokho akucabangayo (noma ngabe lokho akulona iqiniso emehlweni omphakathi), yize empeleni umndeni wami ukhathele ukungcola umphakathi wanamuhla.

Ngomyalo olandelayo uzoshintsha «umkhaya wami"ngo"nathi«:

perl -pi -e "s[mi familia][nosotros]g" $HOME/Documento.txt

Kulula kanjalo 🙂

Ngakho-ke umbhalo uzoba:

(...) nathi Kufana nabo bonke, njengeyakho noma eyomakhelwane wakho. Empeleni nathi Sizama ukuba ngabantu abanokwethenjelwa futhi emehlweni omphakathi, silungile kwezepolitiki nakwezenhlalo. Kodwa-ke, endlini yami kukhona imvu emnyama, futhi leyo yindima yami LOL!. Bengihlale ngingulowo ongesabi ukusho lokho akucabangayo (noma ngabe lokho akulona iqiniso emehlweni omphakathi), yize empeleni umndeni wami ukhathele ukungcola umphakathi wanamuhla.

I-syntax yile:

perl -pi -e "s[lo-que-quiero-cambiar][lo-nuevo-a-poner]g" archivo-en-el-cual-reemplazar

Umyalo ungabonakala ngathi uyinkimbinkimbi, ake siwuhlukanise kancane bese siwuchaza kancane ngokulula 😉

  • i-perl : Esizokusebenzisa, perl
  • -pi : Pí, kanjani π (inani elilinganiselwa ku-3.14)
  • -e : Abakwa-E bakucabange ngo- «e ukwenza» ^ - ^
  • Ngemuva kwalokho kuzilinganiso » sivula nge s futhi sivale nge g: "sg" … Cabanga ngo-S Wokuqala no-G weGame Over 😀
  • Jika phakathi kwe- sg kufanele sibeke amasethi amabili wabakaki: [] y [], ngokuba ngaleyo ndlela: "S [] [] g"
  • Ngaphakathi kubakaki bokuqala njengoba ngishilo ngenhla, umbhalo esifuna ukuwususa bese siwufaka omusha uzohamba, ngenkathi kusethi yesibili yabakaki umbhalo omusha ozowabeka uzohamba, ulandela isibonelo: "S [umndeni wami] [we] g"

Kulula akunjalo?

Incazelo empeleni ibanzi kakhulu kunalokho okudingekayo kumyalo, kulula ukuyiqonda 🙂

UPerl ungaphezulu kakhulu kunendlela yokushintsha umbhalo, wulimi lonke 😉

Uma ufuna ukufunda kabanzi ngakho, faka iphakheji Perl-doc bese ubheka i-whooodo ukuthi ivumela ini, kumane nje kuyindawo ehlukile.

Ngendlela futhi ukuvala, ucabanga ukuthi ufuna ukuthola wonke amafayela we-.txt onawo ku- $ HOME / Documents / nakuwo ngamunye esikhundleni se- "umndeni wami" ufake u- "us", ungajoyina thola + perl ukufeza lokhu:

find $HOME/Documentos/ -name *.txt -exec perl -p -i -e "s[mi familia][nosotros]g" {} \;

Noma usebenzisa i-wildcard:

perl -p -i -e "s[mi familia][nosotros]g" $HOME/Documentos/*.txt

Inkinga ngalokhu okuhlukile kwesibili ukuthi ngeke ithathe indawo yamafayela akumafolda amancane 🙂

Noma kunjalo, ngithemba ukuthi ukuthole kuthakazelisa. Kimi kube wusizo impela, sengivele ngingawuthathela indawo umbhalo ngqo kusuka ku-terminal ... kuhle! 😀


Okuqukethwe yi-athikili kunamathela ezimisweni zethu ze izimiso zokuhlelela. Ukubika iphutha chofoza lapha.

Amazwana ayi-21, shiya okwakho

Shiya umbono wakho

Ikheli lakho le ngeke ishicilelwe. Ezidingekayo ibhalwe nge *

*

*

  1. Ubhekele imininingwane: Miguel Ángel Gatón
  2. Inhloso yedatha: Lawula Ugaxekile, ukuphathwa kwamazwana.
  3. Ukusemthethweni: Imvume yakho
  4. Ukuxhumana kwemininingwane: Imininingwane ngeke idluliselwe kubantu besithathu ngaphandle kwesibopho esisemthethweni.
  5. Isitoreji sedatha: Idatabase ebanjwe yi-Occentus Networks (EU)
  6. Amalungelo: Nganoma yisiphi isikhathi ungakhawulela, uthole futhi ususe imininingwane yakho.

  1.   boom kusho

    Yebo, ngicabanga ukuthi iningi lethu lapha liyawazi umyalo we-sed, osebenzela injongo efanayo, uwenza ube ngcono kakhulu futhi kulula ukuwusebenzisa ...

    1.    KZKG ^ Gaara kusho

      Siyabonga ngokuphawula kwakho,
      Imininingwane emibili nje, kungani usho ukuthi i-sed ikwenza ngcono? Ngabe kulula kakhulu ukusebenzisa nezinhlamvu eziningi kangaka 'eziyinqaba'?

  2.   UFederico A. Valdés Toujague kusho

    Sanibonani nonke !!!. Ngisebenzise umyalo sed, futhi indlela echazwe lapha nge-perl ilula. Ngiyabonga KZKG ^ Gaara !!!.

    1.    KZKG ^ Gaara kusho

      Ngiyabonga ngamazwana 🙂
      Yebo, kimi kubonakala sengathi lokhu kulula kunezinkulumo ezijwayelekile ... ukuthi ngezinhlamvu eziningi 'ezixakile', okuthile okuyinkimbinkimbi ukuqonda nokufunda kungenziwa kwabangewona ama-savvy.

      1.    I-Giskard kusho

        Kukhona i-adage «endala» lapho: «Ngaba nenkinga, nginqume ukusebenzisa Izinkulumo Ezijwayelekile ... manje nginezinkinga EZIMBILI» 😉

        1.    KZKG ^ Gaara kusho

          HAHAHAHAHAHAHA !!!
          Izinkulumo ezijwayelekile zingasindisa izimpilo zethu ngesikhathi esithile ... yebo, kuthatha isikhathi eside ukuthi sifunde ukuzisebenzisa futhi singafi sizama.

          Ukube ngangazi lo myalo eminyakeni edlule, NKULUNKULU !! impilo yami ngabe ilula kanjani 😀

          1.    I-Giskard kusho

            Sula! Iphathwe kahle inamandla amakhulu 🙂 Akukho okungcono ukukhohlisa umbhalo. Kepha kwesinye isikhathi, njengoba usho kuleli cala noPerl, awudingi ukuya kude uma okunye kungenele.
            Bengizosebenzisa iPython noma kunjalo :)

    2.    isihlibhi kusho

      Kepha ngokoma ngibona kulula ngisho ... ngabe kukhona engikwenzayo okungalungile?

      sed -i "uma / yini-engifuna ukuyishintsha / entsha yokufaka /" ifayela elizongena esikhundleni salo

      1.    KZKG ^ Gaara kusho

        heh heh ... futhi uma ngifuna ukufaka esikhundleni se- "http://my.blog.com/content/" ngo "http://my.blog.com/uploads/files/" 😀 ... vele lapho kufanele ubeke \ nezikhala nabanye , futhi kuba yinkimbinkimbi noma cha?

        1.    isihlibhi kusho

          Akuphoqelekile ukusebenzisa / ukwahlukanisa. Bheka nge +

          sed -i «s + http: //my.blog.com/content/+http: //my.blog.com/uploads/files/+» ifayela

          1.    KZKG ^ Gaara kusho

            O_O… WTF!
            Ungivulele indawo yonke yezinketho kimi manje .. O_O

          2.    I-Giskard kusho

            Ngiyazithanda lezi zikhala lapho sonke sifunda okuthile 🙂
            Nami bengingakwazi lokho.

  3.   RAW-Okuyisisekelo kusho

    Ngincamela ukusebenzisa ngqo vi ..

    Kalula njenge:
    :% s / us / umndeni wami / g

    % -> idokhumenti ephelele
    s -> sesha
    g -> konke okwenzekayo

    Ukubingelela ..

    1.    KZKG ^ Gaara kusho

      Gracias!
      Bengingazi ukuthi ngikwenze kanjani nge vi

  4.   umagazine kusho

    Kungezwe kuzintandokazi!

    Gracias!

  5.   I-DuranG kusho

    Ukungena okuhle! Uma ngabe noma ngubani enentshisekelo, okufanayo ngomyalo we-Linux sed: http://www.sysadmit.com/2015/07/linux-reemplazar-texto-en-archivos-con-sed.html

    1.    Ukungabaza kusho

      Futhi kwenzekani uma ngaphakathi kwefayela lokho engifuna ukukufaka esikhundleni kunezingcaphuno?
      Isibonelo buyisela okushiwo umbhalo: »1 ″ ngombhalo:» 2 ″
      ubungaba kanjani umyalo?

  6.   UPercy salgado kusho

    Hola

    Bengifuna ukufaka esikhundleni sentambo ebino @ phakathi futhi ngishintshe umbhalo kuphela ngaya ku-sign ngaphambi kwesign.

    umbhalo ozoshintshwa: IPhile isikhathi eside
    umbhalo omusha: Viva mi patria @percysalgado
    Kubonisiwe: Phila izwe lami

    Ngiyabonga ngosizo lwakho

  7.   engaziwa kusho

    "\" Kufanele usebenzise i-slash ngaphambi kwezinhlamvu usebenzisa kabili @, #, ukuze zihunyushwe njengombhalo.

  8.   Gaba kusho

    Futhi ngikwenza kanjani emawindini ????