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. Kad strādājat ar Linux komandrindas, un jūs saskaraties ar lielu failu vai lielu skaitu failiem, kuriem jums ir nepieciešams aizstāt noteiktu tekstu ar otru, atrast un aplīmēšana pār katru gadījumu tekstu var mazliet laika. Well, worry no more. Nu, uztraukties ne vairāk. Linux has just the solution for you. Linux ir tikai risinājums. Here's a way to find and replace a string of text in one or more files automatically. Šeit ir veids, kā atrast un nomainīt virkni teksta vienu vai vairākus failus automātiski.
For the purpose of this exercise we will use a Linux command line tool called “sed”. Par Šajā izmantot mēs izmantosim Linux komandrindas rīku, ko sauc "sed". ”sed” is a very powerful and versatile tool, and a lot can be written about its capabilities. "Sed" ir ļoti jaudīgs un universāls līdzeklis, un daudz var rakstīt par savām spējām. We are using a very limited aspect of “sed” here. Mēs izmantojam ļoti ierobežota aspektu "sed" šeit. I would definitely recommend that you read up a little more on “sed” if you find this aspect of it interesting. Es noteikti iesakām jums izlasīt līdz nedaudz vairāk par "SED", ja jums atrast šo aspektu ir interesanti.
We are going to use the following syntax to find and replace a string of text in a file: Mēs gatavojamies lietot šādu sintaksi, lai atrastu un aizstātu string teksta failu:
# sed -i 's/[orginal_text]/[new_text]/' filename.txt # Sed-i 's / [orginal_text] / [new_text] / "filename.txt
Say you have a file called “database.txt” with numerous instances of the IP address of your database server in it. Ka jums ir fails ar nosaukumu "database.txt" ar daudziem gadījumiem, par IP adresi Jūsu datu bāzes servera tajā. You have just switched to a new database server and need to update it with the new server's IP address. Tikko esat pārgājuši uz jauno datu bāzi serveri un jāatjaunina to ar jaunā servera IP adresi. The old IP address is 192.168.1.16 and the new one is 192.168.1.22. Vecā IP adrese ir 192.168.1.16 un jauna ir 192.168.1.22. Here's how you go about it: Lūk, kā jūs iet par 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 pie 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. Tagad atveriet failu "database.inc" un pārbaudiet, vai jaunā IP adrese ir notikusi jūsu veco. Here's the breakup of the above command. Šeit ir sabrukuma iepriekš minēto komandu. First you call the “sed” command. Vispirms jūs zvans "sed" komandu. Then you pass it the parameter “-s” which stands for “in place of”. Tad tu dot parametrs "-s", kas nozīmē "vietā". Now we use a little bit of regular expressions, commonly known as “regex” for the next bit. Tagad mēs izmantojam mazliet regulārus izteicienus, ko parasti sauc par "regex" par nākamo biti. The “s” in the quoted string stands for “substitute”, and the “g” at the end stands for “global”. "S" kotētās virkni apzīmē "aizvietotājs" un "g" pie beigām nozīmē "pasaules". Between them they result in a “global substitution of the the string of text you place in between them. Starp tām, to rezultātā "pasaules aizstāšanas virkni teksta jums vietu starp tiem.
You can optionally skip the “g” at the end. Pēc izvēles var izlaist "g" pie beigām. 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. Tas nozīmē, ka aizstāšana nebūs pasaules, kuras praktiski translates to aizstāšanu tikai pirmās instances string in līniju. So if you had a line with multiple instances of the text you are trying to replace, here's what will happen Tātad, ja Jums bija līnija ar vairākiem gadījumiem, kad teksts, kuru jūs mēģināt aizvietot, lūk, kas notiks
# 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. Šeit nāk īsta maģija. 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”. Tagad, ka vēlaties mainīt virkni teksta ne tikai vienā failā, bet visa direktorija, kurā atrodaties ir teksta faili, kuros vēlaties atrast un aizstāt "vīns" ar "šampanieti numurs" .
# find . # Atrast. -maxdepth 1 -name “*.txt” -type f -exec sed -i 's/wine/champagne/' {} \ -maxdepth 1-name "*. txt"-type f-exec sed-i 's / vīns / šampanietis /' () \
We use the find command to get a list of all the text files in the current directory. Mēs izmantojam atrast komandu, lai iegūtu sarakstu ar visām teksta failus pašreizējā direktorijā. That's the “find . Tas ir "atrast. -maxdepth 1 -name “*.txt” -type f” part. -maxdepth 1-name "*. txt"-type f "daļas. “find . maxdepth 1″ tell the computer to look in the current directory and go no deeper than the current directory. "Atrast. Maxdepth 1" pateikt datora skatīties ar pašreizējā direktorijā un aiziet ne dziļāk nekā pašreizējā direktorijā. The '-name ”*.txt”' part tells find to only list files with the extension of “.txt”. '-Name "*. txt" "daļa stāsta atrast tikai sarakstu failus ar pagarinājumu." Txt ". Then the “-type f” section specifies that “find” should only pick exactly matching files. Tad "tipa f" nodaļā noteikts, ka "Meklēt" tikai izvēlēties tieši saskaņošanas failus. 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/' {} \”. Beidzot "-exec" daļā stāsta "Meklēt", lai izpildīt komandu, kas seko, kas šajā gadījumā ir "sed" komandu, lai aizvietotu tekstu - "sed-i 's / vīns / šampanietis /' () \ ".
I realize that the above command seems complicated. Es atklāju, ka iepriekš minēto komandu šķiet sarežģīta. However, once you use it a little bit you will realize that it is probably worth noting it down and using it. Tomēr, tiklīdz Jūs to lietosiet mazliet tu saproti, ka tas, iespējams, vērts atzīmēt to uz leju, un to izmanto. Now try changing a string of text in multiple levels of directories. Tagad mēģiniet mainīt teksta virkne, kas dažādos līmeņos direktorijai.























> Then you pass it the parameter “-s” which stands for “in place of”. > Pēc tam jums dot parametrs "-s", kas nozīmē "vietā".
I think it must be “-i” Es domāju, ka tam jābūt "i"
Now open the file “database.inc” and check to see if the new IP address has taken place of your old one. Tagad atveriet failu "database.inc" un pārbaudiet, vai jaunā IP adrese ir notikusi jūsu veco.
database.inc here I think it should be database.txt database.inc šeit es domāju, ka tas būtu database.txt
Eso es muy facil…. Eso es muy Fácil ....
pero por ejemplo como harias lo siguiente pero por ejemplo como harias lo siguiente
tienes una ruta windows en un codigo y quieres pasarla a ruta de tipo linux tienes una ruta logi en un código y Quieres pasarla ruta de tipo linux
la cadena que buscas es C:\ejemplos\archivos\aqui la Cadena que buscas 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 mapi ...