Find and replace a word within a file (Linux Command Line)

If you have a text file and we need to search and replace a word within itWith another word or phrase on Windows we have many solutions. We can use NotepadThe, WordPadCPC or   Office (This would be the most common)

Replace Text in Notepad - Windows

Order "search & replace"In large text files is a problem Windows. Experts say it as a text file over 50 MB in which we replaced the word with "Replace All"Becomes a problem for an operating system Whether the file was opened in Notepad, WordPad or in another text editor.

The problem becomes much simpler when it comes to . With a simple commandline (Bash command) can search and replace a word within a file (file can be.,.,. Txt,. Sql, etc format containing text) in a very short time. Much shorter than on Windows.

Find and replace text within a file using the command "sit".

Let's say you have a text file (Document.txt) Word "a B C"We want to replace it with"xyz. "Command line that can replace the word will be as follows:

sed-ie 's / abc / xyz / g' Document.txt

When you execute the command, the condition is that we are in the folder that was Document.txt. If we are not in your Documents folder, but know the "way" it, the command line would be:

sed-ie 's / abc / xyz / g' / var / www / Document.txt

Where "/ var / www /" is the folder in which the file is Document.txt.

After execution of the command line option "I" will create a backup file identical to the original, but the extension which will add the letter "e". In our case, the document will be backup Document.txt.

-I [SUFFIX],-in-place [= SUFFIX] edit files in place (Makes backup if extension Supplied)

's Script-expression = script: add the script to the commands to be executed.

The command "find & replace" not only applies to files. Txt. You can replace words inside any file that contains text. Even Database files (. SQL)

Find Replace ad - sed Bash Command Line

Find & Replace Inside a Text File (Linux Bash Command Line).

Find and replace a word within a file (Linux Command Line)

About the author

Stealth LP

Founder and editor Stealth SettingsIn 2006 date.
Experience on Linux operating systems (especially CentOS), Mac OS X, Windows XP> Windows 10 and WordPress (CMS).

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment is processed.