Skip to content Skip सामग्री के लिए


Linux command line magic – find and replace Linux कमांड लाइन जादू - खोजें और जगह

लिनक्स

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. तुम Linux कमांड लाइन पर काम कर रहे हैं और जब आप एक बड़ी फ़ाइल या फ़ाइलें, जिनमें आप एक के साथ एक निश्चित पाठ की जगह की जरूरत के बड़ी संख्या में आते हैं, खोज और पाठ के एक उदाहरण पर चिपकाने थोड़ा समय लगता हो सकता है. Well, worry no more. ठीक है, ज्यादा चिंता नहीं. Linux has just the solution for you. लिनक्स तुम्हारे लिए अभी हल है. Here's a way to find and replace a string of text in one or more files automatically. यहां एक रास्ता मिल रहा है और एक या एक से अधिक फ़ाइलों में पाठ की एक तार स्वचालित रूप से बदल रहे हैं.

For the purpose of this exercise we will use a Linux command line tool called “sed”.  ”sed” is a very powerful and versatile tool, and a lot can be written about its capabilities. "Sed" एक बहुत शक्तिशाली और बहुमुखी उपकरण, और बहुत कुछ अपनी क्षमताओं के बारे में लिखा जा सकता है. We are using a very limited aspect of “sed” here. हम एक बहुत सीमित पहलू प्रयोग कर रहे हैं "sed" यहाँ. I would definitely recommend that you read up a little more on “sed” if you find this aspect of it interesting. मैं निश्चित रूप से अनुशंसा होता है कि आप "पर sed" अगर आप के इस पहलू मिल रुचिकर और अधिक पढ़ें एक सा.

We are going to use the following syntax to find and replace a string of text in a file: हम निम्नलिखित वाक्य रचना का उपयोग नहीं कर पाते जा रहे हैं और एक फ़ाइल में पाठ की एक स्ट्रिंग के स्थान:

# sed -i 's/[orginal_text]/[new_text]/' filename.txt # Sed '/ i [orginal_text] / [नया _ पाठ] / filename.txt s

Say you have a file called “database.txt” with numerous instances of the IP address of your database server in it. कहते हैं कि तुम एक फ़ाइल "आपके डाटाबेस सर्वर के IP पता की उस में अनेक उदाहरणों के साथ database.txt" कहा जाता है. You have just switched to a new database server and need to update it with the new server's IP address. तुम सिर्फ एक नए डेटाबेस सर्वर से बंद है और इसे नए सर्वर के IP पता के साथ अद्यतन करने की जरूरत है. The old IP address is 192.168.1.16 and the new one is 192.168.1.22. पुराने आईपी पता 192.168.1.16 और नए से एक है 192.168.1.22 है. Here's how you go about it: यहाँ है तुम इसके बारे में कैसे जा:

# cat database.txt # बिल्ली database.txt
LOCAL_DATABASE = 192.168.1.16 = 192.168.1.16 LOCAL_DATABASE
LOCAL_DIR = /home/calvin/ LOCAL_DIR = / घर केल्विन / /
PROD_DB = 192.168.1.16 = 192.168.1.16 PROD_DB

# sed -i 's/192.168.1.16/192.168.1.22/g' database.txt # Sed 'मैं' s/192.168.1.16/192.168.1.22/g database.txt
# c at database.txt Database.txt में # ग
LOCAL_DATABASE = 192.168.1.22 = 192.168.1.22 LOCAL_DATABASE
LOCAL_DIR = /home/calvin/ LOCAL_DIR = / घर केल्विन / /
PROD_DB = 192.168.1.22 = 192.168.1.22 PROD_DB

Now open the file “database.inc” and check to see if the new IP address has taken place of your old one. अब फ़ाइल खोलने "database.inc" और यदि नए आईपी पते आपके पुराने एक के स्थान पर ले लिया है देखने की जाँच करें. Here's the breakup of the above command. यहाँ ऊपर कमान की अव्यवस्था है. First you call the “sed” command. पहले तुम फोन "" sed कमान. Then you pass it the parameter “-s” which stands for “in place of”. तो फिर तुम यह पैरामीटर पास 's "जो के स्थान पर' के लिए खड़ा है." Now we use a little bit of regular expressions, commonly known as “regex”  for the next bit. अब हम नियमित अभिव्यक्ति का एक छोटा सा, सामान्यतः अगले कुछ के लिए 'regex' के रूप में जाना जाता का उपयोग करें. The “s” in the quoted string stands for “substitute”, and the “g” at the end stands for “global”. "उद्धृत स्ट्रिंग में एस" "विकल्प" के लिए खड़ा है, और "छ" अंत में "के लिए वैश्विक खड़ा है." Between them they result in a “global substitution of the the string of text you place in between them. उन दोनों के बीच पाठ आप स्थान के तार के एक "ग्लोबल प्रतिस्थापन में वे उन दोनों के बीच में परिणाम.

You can optionally skip the “g” at the end. आप वैकल्पिक "छ" अंत में छोड़ सकते हैं. 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. इसका मतलब यह है कि प्रतिस्थापन नहीं, वैश्विक होगा जो व्यावहारिक रूप से एक लाइन में तार का केवल पहला उदाहरण के प्रतिस्थापन के लिए sake. So if you had a line with multiple instances of the text you are trying to replace, here's what will happen तो अगर आप पाठ आप को बदलने के प्रयास कर रहे हैं के कई उदाहरणों के साथ एक पंक्ति थी, यहाँ है क्या होगा

# cat database.txt # बिल्ली database.txt
LOCAL_DATABASE = 192.168.1.16 = 192.168.1.16 LOCAL_DATABASE
LOCAL_DIR = /home/calvin/ LOCAL_DIR = / घर केल्विन / /
PROD_DB = 192.168.1.16, 192.168.1.16 192.168.1.16 =, 192.168.1.16 PROD_DB

# sed -i 's/192.168.1.16/192.168.1.22/' database.txt # Sed 'मैं s/192.168.1.16/192.168.1.22' / database.txt
# cat database.txt # बिल्ली database.txt
LOCAL_DATABASE = 192.168.1.22 = 192.168.1.22 LOCAL_DATABASE
LOCAL_DIR = /home/calvin/ LOCAL_DIR = / घर केल्विन / /
PROD_DB = 192.168.1.22, 192.168.1.16 192.168.1.22 =, 192.168.1.16 PROD_DB

Here comes the real magic. यहाँ असली जादू आता है. 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”. अब कहो, तुम सिर्फ एक ही फ़ाइल में पाठ की एक स्ट्रिंग नहीं बदलना चाहते हैं, लेकिन पूरी निर्देशिका में तुम वहाँ पाठ फ़ाइलें जिसमें आप खोज और शराब "की जगह" शैम्पेन "के साथ की जरूरत के एक नंबर रहे हैं अंदर कर रहे हैं" .

# find . # लगता है. -maxdepth 1 -name “*.txt” -type f -exec sed -i 's/wine/champagne/' {} \ maxdepth-1-नाम "*. txt 'टाइप एफ sed exec' मैं S / शराब / शैंपेन / '() \

We use the find command to get a list of all the text files in the current directory. हम उपयोग कमांड में सभी मौजूदा निर्देशिका में पाठ फ़ाइलों की एक सूची प्राप्त लगता है. That's the “find . यह खोज '. -maxdepth 1 -name “*.txt” -type f” part. हिस्सा maxdepth 1-नाम "*. txt 'टाइप च'. “find . maxdepth 1″ tell the computer to look in the current directory and go no deeper than the current directory. ". 1 maxdepth मिल" कंप्यूटर कहना मौजूदा निर्देशिका में देखो और कोई मौजूदा निर्देशिका से गहरे जाओ. The '-name  ”*.txt”' part tells find to only list files with the extension of “.txt”. '' *. Txt का नाम ' "भाग के विस्तार के साथ ही फाइलों की सूची के लिए खोजें. कहता है" txt ". Then the “-type f” section specifies that “find” should only pick exactly matching files. फिर 'प्रकार अनुभाग च "निर्दिष्ट करता है कि" खोज "केवल लेने चाहिए वही फ़ाइलें मिलान. 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/' {} \”. अंत में 'exec हिस्सा "पता बताता है" "के लिए आदेश है कि इस प्रकार है, जो, इस मामले में," "sed कमान के पाठ की जगह है निष्पादित -" sed' मैं S / शराब / शैंपेन / '() \ ".

I realize that the above command seems complicated. मुझे पता है कि इसके बाद के संस्करण कमान जटिल लगता है. However, once you use it a little bit you will realize that it is probably worth noting it down and using it. हालांकि, एक बार आप इसे उपयोग थोड़ा तुम्हें एहसास होगा कि यह शायद यह टिप्पण नीचे और इसे उपयोग के लायक है. Now try changing a string of text in multiple levels of directories. अब निर्देशिका की कई स्तरों में पाठ की एक तार को बदलने की कोशिश करो.

Posted in पोस्ट में Linux लिनक्स . .

Related Posts: संबंधित पोस्ट:

How to resolve the '/bin/rm: Argument list too long' error कैसे '/ बिन rm / हल करने के लिए: तर्क भी' लंबे समय त्रुटि सूची
How to post to Twitter from the Linux command line कैसे Linux कमांड लाइन से चहचहाना पोस्ट करने के लिए
How to find your public IP address with the Linux command line कैसे Linux कमांड लाइन के साथ अपने सार्वजनिक आईपी पता लगाने के लिए
How to enable the root user account in Ubuntu Linux कैसे Ubuntu Linux में जड़ उपयोगकर्ता खाते में सक्षम करने के लिए
Bash one liner – how to compress, move, and extract a directory पार्टी की योजना बनाई एक लाइनर - कैसे जमना, के लिए कदम है, और एक निर्देशिका निकालें

3 Responses 3 जवाब

Stay in touch with the conversation, subscribe to the बातचीत के साथ संपर्क में रहो, की सदस्यता लें RSS feed for comments on this post आरएसएस के इस पोस्ट पर टिप्पणियों के लिए फ़ीड . .

  1. marco says मार्को कहते हैं,

    > Then you pass it the parameter “-s” which stands for “in place of”. > तो फिर तुम यह पैरामीटर पास 's "जो के स्थान पर' के लिए खड़ा है."
    I think it must be “-i” मुझे लगता है, यह होना चाहिए "मैं"

  2. myhnet myhnet says कहता है

    Now open the file “database.inc” and check to see if the new IP address has taken place of your old one. अब फ़ाइल को खोलने "database.inc" और यदि नए आईपी पते आपके पुराने एक के स्थान पर ले लिया है देखने की जाँच करें.

    database.inc here I think it should be database.txt यहाँ database.inc मुझे लगता है यह database.txt जाना चाहिए

  3. Faustino says Faustino कहते हैं,

    Eso es muy facil…. Eso es muy facil ....
    pero por ejemplo como harias lo siguiente पेरो क्रम ejemplo कोमो harias लो siguiente
    tienes una ruta windows en un codigo y quieres pasarla a ruta de tipo linux tienes ऊना संयुक्त राष्ट्र codigo y en ruta quieres pasarla एक ruta de tipo Linux Windows
    la cadena que buscas es C:\ejemplos\archivos\aqui ला cadena que es buscas 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… पैरा संयुक्त राष्ट्र grupo de archivos que से encuentran en la misma carpeta ...



Some HTML is OK कुछ HTML है ठीक है

or, reply to this post via या, इस पोस्ट के माध्यम से जवाब trackback Trackback . .