Γραμμή εντολής Linux μαγική - βρείτε και αντικαταστήστε

από Sukrit Dhandhania [[on]] 24 Δεκεμβρίου 2008

Linux

Όταν εργάζεστε στη γραμμή εντολής Linux και συναντείτε τυχαία ένα μεγάλο αρχείο ή ένας μεγάλος αριθμός αρχείων στον οποίο πρέπει να αντικαταστήσετε ένα ορισμένο κείμενο με άλλο, που βρίσκει και που κολλά πέρα από κάθε περίπτωση του κειμένου μπορεί να είναι λίγο χρονοβόρος. Καλά, μην ανησυχήστε πλέον. Το Linux έχει ακριβώς τη λύση για σας. Εδώ είναι ένας τρόπος να βρεθεί και να αντικατασταθεί μια σειρά του κειμένου σε ένα ή περισσότερα αρχεία αυτόματα.

Με σκοπό αυτήν την άσκηση θα χρησιμοποιήσουμε ένα εργαλείο γραμμών εντολής Linux αποκαλούμενο «sed». ; » sed» είναι ένα πολύ ισχυρό και ευπροσάρμοστο εργαλείο, και πολύ μπορεί να γραφτεί για τις ικανότητές του. Χρησιμοποιούμε μια πολύ περιορισμένη πτυχή «του sed» εδώ. Σίγουρα θα σύστηνα ότι διαβάζετε επάνω λίγο περισσότερων «στο sed» εάν βρίσκετε αυτήν την πτυχή από το που ενδιαφέρει.

Πρόκειται να χρησιμοποιήσουμε την ακόλουθη σύνταξη για να βρούμε και να αντικαταστήσουμε μια σειρά του κειμένου σε ένα αρχείο:

# sed - ι» s [original_text]/[new_text]/» filename.txt

Πέστε ότι καλείτε ένα αρχείο «database.txt» με τις πολυάριθμες περιπτώσεις της διεύθυνσης IP του κεντρικού υπολογιστή βάσεων δεδομένων σας σε το. Μεταπηδήσατε μόλις σε έναν νέο κεντρικό υπολογιστή βάσεων δεδομένων και πρέπει να τον ενημερώσετε με τη διεύθυνση IP του νέου κεντρικού υπολογιστή. Η παλαιά διεύθυνση IP είναι το 192.168.1.16 και ο νέος είναι το 192.168.1.22. Εδώ είναι πώς εσείς πηγαίνει για το:

# γάτα database.txt
LOCAL_DATABASE = 192.168.1.16
LOCAL_DIR = /home/calvin/
PROD_DB = 192.168.1.16

#?sed - ι» s 192.168.1.16/192.168.1.22 /g»; database.txt
# γστο database.txt
LOCAL_DATABASE = 192.168.1.22
LOCAL_DIR = /home/calvin/
PROD_DB = 192.168.1.22

Τώρα ανοίξτε το αρχείο «database.inc» και τον έλεγχο που βλέπει εάν η νέα διεύθυνση IP έχει πραγματοποιηθεί παλαιά μια σας. Εδώ είναι η αποσύνθεση της ανωτέρω εντολής. Πρώτα καλείτε την εντολή «sed». Κατόπιν τον περνάτε η παράμετρος «- s» που αντιπροσωπεύει «αντί». Τώρα χρησιμοποιούμε λίγος τις κανονικές εκφράσεις, που είναι γνωστές συνήθως ως «regex»; για το επόμενο κομμάτι. Το «s» στις αναφερμένες στάσεις σειράς για «το υποκατάστατο», και το «γ» στο τέλος αντιπροσωπεύουν «σφαιρικό». Μεταξύ τους οδηγούν σε μια «σφαιρική αντικατάσταση της σειράς του κειμένου που τοποθετείτε μεταξύ τους.

Μπορείτε προαιρετικά να πηδήσετε το «γ» στο τέλος. Αυτό σημαίνει ότι η αντικατάσταση δεν θα είναι σφαιρική, το οποίο μεταφράζει ουσιαστικά στην αντικατάσταση μόνο της πρώτης περίπτωσης της σειράς σε μια γραμμή. Έτσι εάν είχατε μια γραμμή με τις πολλαπλάσιες περιπτώσεις του κειμένου προσπαθείτε να αντικαταστήσετε, είναι εδώ τι θα συμβεί

# 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>