When you are working on the Linux command line and you come across a large file or a large number of files in which you need to replace a certain text with another, finding and pasting over each instance of the text can be a bit time consuming. Ak pracujete na príkazovom riadku Linuxu a narazíte na veľkých súborov alebo veľkého množstva súborov, v ktorých budete musieť nahradiť určitý text iným, vyhľadávanie a vkladanie cez jednotlivé inštancie text môže byť trochu časovo náročné. Well, worry no more. No, nič viac starať. Linux has just the solution for you. Linux má práve pre Vás riešenie. Here's a way to find and replace a string of text in one or more files automatically. Tu je spôsob, ako nájsť a nahradiť reťazec textu v jednom alebo viacerých súborov automaticky.
For the purpose of this exercise we will use a Linux command line tool called “sed”. Na účely tohto cvičenia budeme používať Linux príkazového riadka s názvom "sed". ”sed” is a very powerful and versatile tool, and a lot can be written about its capabilities. "Sed" je veľmi silný a univerzálny nástroj, a mnohé možno napísať o jeho schopnostiach. We are using a very limited aspect of “sed” here. Použili sme veľmi obmedzené aspekt "sed" tu. I would definitely recommend that you read up a little more on “sed” if you find this aspect of it interesting. Já bych rozhodně doporučuji, aby ste si sa trochu viac na "sed" ak zistíte, že tento aspekt je zaujímavé.
We are going to use the following syntax to find and replace a string of text in a file: Budeme používať nasledujúce syntax nájsť a nahradiť textový reťazec v súbore:
# sed -i 's/[orginal_text]/[new_text]/' filename.txt # Sed-i 's / [orginal_text] / [nový_text] /' filename.txt
Say you have a file called “database.txt” with numerous instances of the IP address of your database server in it. Povedzme, že máte súbor s názvom "database.txt" s početným prípadom IP adresu databázového servera v ňom. You have just switched to a new database server and need to update it with the new server's IP address. Ste práve prešli na nový databázový server, a je potrebné aktualizovať s novou adresu IP servera. The old IP address is 192.168.1.16 and the new one is 192.168.1.22. Starých IP adresa je 192.168.1.16 a nový je 192.168.1.22. Here's how you go about it: Tu je návod, ako máš na to:
# cat database.txt # Cat database.txt
LOCAL_DATABASE = 192.168.1.16 LOCAL_DATABASE = 192.168.1.16
LOCAL_DIR = /home/calvin/ LOCAL_DIR = / home / calvin /
PROD_DB = 192.168.1.16 PROD_DB = 192.168.1.16
# sed -i 's/192.168.1.16/192.168.1.22/g' database.txt # Sed-i 's/192.168.1.16/192.168.1.22/g' database.txt
# c at database.txt # C u database.txt
LOCAL_DATABASE = 192.168.1.22 LOCAL_DATABASE = 192.168.1.22
LOCAL_DIR = /home/calvin/ LOCAL_DIR = / home / calvin /
PROD_DB = 192.168.1.22 PROD_DB = 192.168.1.22
Now open the file “database.inc” and check to see if the new IP address has taken place of your old one. Teraz otvorte súbor "database.inc" a skontrolujte, či novú IP adresu došlo vášho starého. Here's the breakup of the above command. Tu je rozpad vyššie uvedeného príkazu. First you call the “sed” command. Prvé vravíte "sed" kontrolovať. Then you pass it the parameter “-s” which stands for “in place of”. Potom môžete prejsť to parameter "-s", ktoré znamená "miesto". Now we use a little bit of regular expressions, commonly known as “regex” for the next bit. Teraz používame trochu regulárnych výrazov, bežne známy ako "regex" pre ďalší kúsok. The “s” in the quoted string stands for “substitute”, and the “g” at the end stands for “global”. "S" v citovaný reťazec je skratka pre "náhradné", a "g" na konci je skratka pre "globálne". Between them they result in a “global substitution of the the string of text you place in between them. Medzi nimi vedú k "globálnej nahradenie reťazca textu, ktorý miesto medzi nimi.
You can optionally skip the “g” at the end. Voliteľne môžete preskočiť "g" na konci. This means that the substitution will not be global, which practically translates to the substitution of only the first instance of the string in a line. To znamená, že náhrada nebude globálne, čo prakticky znamená na nahradenie iba prvý výskyt reťazca v riadku. So if you had a line with multiple instances of the text you are trying to replace, here's what will happen Takže ak by ste mali súlade so viac inštancií textu, ktorý sa snaží nahradiť, tu je to, čo sa stane
# cat database.txt # Cat database.txt
LOCAL_DATABASE = 192.168.1.16 LOCAL_DATABASE = 192.168.1.16
LOCAL_DIR = /home/calvin/ LOCAL_DIR = / home / calvin /
PROD_DB = 192.168.1.16, 192.168.1.16 PROD_DB = 192.168.1.16, 192.168.1.16
# sed -i 's/192.168.1.16/192.168.1.22/' database.txt # Sed-i 's/192.168.1.16/192.168.1.22 /' database.txt
# cat database.txt # Cat database.txt
LOCAL_DATABASE = 192.168.1.22 LOCAL_DATABASE = 192.168.1.22
LOCAL_DIR = /home/calvin/ LOCAL_DIR = / home / calvin /
PROD_DB = 192.168.1.22, 192.168.1.16 PROD_DB = 192.168.1.22, 192.168.1.16
Here comes the real magic. Tu prichádza skutočné kúzlo. Now, say you want to change a string of text not just in a single file, but in the entire directory you are in. There are a number of text files in which you need to find and replace the “wine” with “champagne”. Teraz, že chcete zmeniť textový reťazec, nie len v jednom súbore, ale v celom adresári kde sa nachádzate Existuje niekoľko textových súborov, v ktorých musíte nájsť a nahradiť "víno" s "champagne" .
# find . # Find. -maxdepth 1 -name “*.txt” -type f -exec sed -i 's/wine/champagne/' {} \ maxdepth-1-name "*. txt"-type f-exec sed-i 's / víno / champagne /' () \
We use the find command to get a list of all the text files in the current directory. Používame nájsť príkazu získať zoznam všetkých textových súborov v aktuálnom adresári. That's the “find . To je "nájsť. -maxdepth 1 -name “*.txt” -type f” part. maxdepth-1-name "*. txt"-type f "časť. “find . maxdepth 1″ tell the computer to look in the current directory and go no deeper than the current directory. "Nájsť. Maxdepth 1" povedať počítač pozrieť v aktuálnom adresári, a ísť nie je hlbšie ako v aktuálnom adresári. The '-name ”*.txt”' part tells find to only list files with the extension of “.txt”. '-Name "*. txt"' časť hovorí nájsť len zoznam súborov s príponou ". Txt". Then the “-type f” section specifies that “find” should only pick exactly matching files. Potom "-type f" Oddiel uvádza, že "nájsť" by mal len vybrať presne zodpovedajúce súbory. Finally the “-exec” part tells “find” to execute the command that follows, which, in this case, is the “sed” command to replace the text – “sed -i 's/wine/champagne/' {} \”. Nakoniec "-exec" časť hovorí "nájsť" vykonať príkaz, ktorý nasleduje, ktorá v tomto prípade je "sed" príkaz na nahradenie textu - "sed-i 's / víno / champagne /' () \ ".
I realize that the above command seems complicated. Uvedomujem si, že vyššie uvedenému príkazu sa zdá zložité. However, once you use it a little bit you will realize that it is probably worth noting it down and using it. Avšak, akonáhle ho použiť trochu si uvedomíš, že je pravdepodobne stojí za zmienku, že sa používať. Now try changing a string of text in multiple levels of directories. Teraz skúste použiť textový reťazec s viac úrovní adresárov.























> Then you pass it the parameter “-s” which stands for “in place of”. > Potom prejdete to parameter "-s", ktoré znamená "miesto".
I think it must be “-i” Myslím, že to musí byť "-i"
Now open the file “database.inc” and check to see if the new IP address has taken place of your old one. Teraz otvorte súbor "database.inc" a skontrolujte, či novú IP adresu došlo vášho starého.
database.inc here I think it should be database.txt database.inc tady si myslím, že by malo byť database.txt
Eso es muy facil…. Eso es muy facil ....
pero por ejemplo como harias lo siguiente pero por ejemplo ako harias lo siguiente
tienes una ruta windows en un codigo y quieres pasarla a ruta de tipo linux tienes una ruta okná en un codigo y quieres pasarla ruta de tipo linux
la cadena que buscas es C:\ejemplos\archivos\aqui la Cadena Buscas que es C: \ ejemplos \ archivos \ aqui
Cambiarla a /ejemplos/archivos/aqui Cambiarla / ejemplos / archivos / aqui
para un grupo de archivos que se encuentran en la misma carpeta… para un grupo de archivos que se encuentran en la misma zložka ...