Linux befaller fodrar magi - finna och byt ut

vid Sukrit Dhandhania December 24, 2008

Linux

Befalla fodrar, när du är funktionsduglig på Linuxen, och du kommer över ett stort sparar, eller ett stort nummer av sparar i vilket du behöver för att byta ut en bestämd text med another och att finna, och klistra över varje anföra som exempel av texten kan vara a bet tidskrävande. Brunnen oroar inte mer. Linux har precis lösningen för dig. Är här a långt som finner, och att byta ut en stränga av text i one or more sparar automatiskt.

För ämna av detta öva oss ska bruk som en Linux befaller fodrar bearbetar kallat ”sed”. ? ” sed” är ett mycket kraftigt, och mångsidigt bearbeta, och ett lott kan vara skriftligt om dess kapaciteter. Vi använder en mycket inskränkt aspekt av ”sed” här. Jag skulle rekommenderar bestämt att du läser upp mer på ”sed lite”, om du finner denna aspekt av den som intresserar.

Vi går att använda efter syntaxen för att finna och byta ut en stränga av text i en spara:

# sed - I' s [original_text]/[] för new_text/' filename.txt

Något att säga som du har en spara som kallas ”database.txt” med talrikt, anföra som exempel av IPet address av din databasserver i den. Du har kopplat till en ny databasserver och behöver precis att uppdatera den med den nya serverens IP address. Det gammala IPet address är 192.168.1.16, och den nya är 192.168.1.22. Är här hur du går om det:

# katt database.txt
LOCAL_DATABASE = 192.168.1.16
LOCAL_DIR = /home/calvin/
PROD_DB = 192.168.1.16

#?sed - I' s 192.168.1.16/192.168.1.22 /g'? database.txt
nr. cpå database.txt
LOCAL_DATABASE = 192.168.1.22
LOCAL_DIR = /home/calvin/
PROD_DB = 192.168.1.22

Nu öppna spara ”database.inc” och kontrollera för att se, om det nya IPet address har ägt rum av ditt gammala. Här är upplösningen av det ovannämnt befaller. Först ”sed” du den appellen befaller. Därefter passerar du det parametern ”- s” som står för ”i stället för”. Nu använder vi a bet lite av stamgästuttryck, gemensamt bekant som ”regex”? för det nästa bet. ”Set” i citerad stränger stativ för ”ersättning” och ”Get” på avslutastativen för ”globalt”. Dem emellan resulterar de i ”en global ersättning av stränga av text som du förlägger in - dem emellan.

Du kan valfritt hoppa over ”Get” på avsluta. Detta anföra som exempel hjälpmedlet, som ersättningen ska för att inte vara global, som praktiskt översätter till ersättningen av endast första, av stränga i en fodra. , om så du hade en fodra med multipel anföra som exempel av texten som du är pröva att byta ut, är här ska vad händ

# cat database.txt
LOCAL_DATABASE = 192.168.1.16
LOCAL_DIR = /home/calvin/
PROD_DB = 192.168.1.16, 192.168.1.16

#?sed -i ’s/192.168.1.16/192.168.1.22/’?database.txt
# cat database.txt
LOCAL_DATABASE = 192.168.1.22
LOCAL_DIR = /home/calvin/
PROD_DB = 192.168.1.22, 192.168.1.16

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/’ {} \

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. “find .?maxdepth 1″ tell the computer to look in the current directory and go no deeper than the current directory. The ‘-name??”*.txt”‘ part tells find to only list files with the extension of “.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/’ {} \”.

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.

Related Posts:
  • Some useful Linux bash tricks
  • Running process in the background with Nohup
  • Get started using the Vim text editor
  • How to download files from the Linux command line
  • How to set the date on your Linux machine
  • Get Simple Help tutorials just like this one in your email inbox every day - for free! Just enter your email address below:

    You can always opt out of this email subscription at any time.


    Bookmark and Share

    { 3 comments… read them below or add one }

    1 marco 12.29.08 at 11:36 pm

    > Then you pass it the parameter ?-s? which stands for ?in place of?.
    I think it must be ?-i?

    2 myhnet 12.31.08 at 9:03 pm

    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 I think it should be database.txt

    3 Faustino 01.19.09 at 3:56 pm

    Eso es muy facil….
    pero por ejemplo como harias lo siguiente
    tienes una ruta windows en un codigo y quieres pasarla a ruta de tipo linux
    la cadena que buscas es C:\ejemplos\archivos\aqui
    Cambiarla a /ejemplos/archivos/aqui

    para un grupo de archivos que se encuentran en la misma carpeta…

    Leave a Comment

    You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>