Linux bash find in files

Find and Replace Inside a Text File from a Bash Command

What’s the simplest way to do a find and replace for a given input string, say abc , and replace with another string, say XYZ in file /tmp/file.txt ? I am writting an app and using IronPython to execute commands through SSH — but I don’t know Unix that well and don’t know what to look for. I have heard that Bash, apart from being a command line interface, can be a very powerful scripting language. So, if this is true, I assume you can perform actions like these. Can I do it with bash, and what’s the simplest (one line) script to achieve my goal?

18 Answers 18

The easiest way is to use sed (or perl):

sed -i -e 's/abc/XYZ/g' /tmp/file.txt 

Which will invoke sed to do an in-place edit due to the -i option. This can be called from bash.

If you really really want to use just bash, then the following can work:

while IFS='' read -r a; do echo "$" done < /tmp/file.txt >/tmp/file.txt.t mv /tmp/file.txt

This loops over each line, doing a substitution, and writing to a temporary file (don’t want to clobber the input). The move at the end just moves temporary to the original name. (For robustness and security, the temporary file name should not be static or predictable, but let’s not go there.)

For Mac users:

sed -i '' 's/abc/XYZ/g' /tmp/file.txt 

(See the comment below why)

Except that invoking mv is pretty much as ‘non Bash’ as using sed. I nearly said the same of echo, but it’s a shell builtin.

The -i argument for sed doesn’t exist for Solaris (and I would think some other implementations) however, so keep that in mind. Just spent several minutes figuring that out.

Note for Mac users who get an invalid command code C error. For in-place replacements, BSD sed requires a file extension after the -i flag because it saves a backup file with the given extension. For example: sed -i ‘.bak’ ‘s/find/replace/’ /file.txt You can skip the backup by using an empty string like so: sed -i » ‘s/find/replace/’ /file.txt

File manipulation isn’t normally done by Bash, but by programs invoked by Bash, e.g.:

perl -pi -e 's/abc/XYZ/g' /tmp/file.txt 

The -i flag tells it to do an in-place replacement.

See man perlrun for more details, including how to take a backup of the original file.

The purist in me says you can’t be sure Perl will be available on the system. But that’s very seldom the case nowadays. Perhaps I’m showing my age.

Can you show a more complex example. Something like replacing «chdir /blah» with «chdir /blah2». I tried perl -pi -e ‘s/chdir (?:\\/[\\w\\.\\-]+)+/chdir blah/g’ text , but I keep getting an error with Having no space between pattern and following word is deprecated at -e line 1. Unmatched ( in regex; marked by

This is an old post but for anyone wanting to use variables as @centurian said the single quotes mean nothing will be expanded.

Читайте также:  Аналог smart pss linux

A simple way to get variables in is to do string concatenation since this is done by juxtaposition in bash the following should work:

sed -i -e "s/$var1/$var2/g" /tmp/file.txt 

Problem solved, well, . $li comes from a file line, so there is e.g. \n and the error is there. So either awk or another language like python comes.

I was surprised when I stumbled over this.

There is a replace command which ships with the «mysql-server» package, so if you have installed it try it out:

# replace string abc to XYZ in files replace "abc" "XYZ" -- file.txt file2.txt file3.txt # or pipe an echo to replace echo "abcdef" |replace "abc" "XYZ" 

See man replace for more on this.

Two things are possible here: a) replace is a useful independent tool and the MySQL folks should release it separately and depend on it b) replace requires some bit of MySQL o_O Either way, installing mysql-server to get replace would be the wrong thing to do 🙂

That’s because you don’t have mysql-server package installed. As pointed by @rayro, replace is part of it.

Be careful not to run the REPLACE command on Windows! On Windows the REPLACE command is for a fast replication of files. Not relevant to this discussion.

Bash, like other shells, is just a tool for coordinating other commands. Typically you would try to use standard UNIX commands, but you can of course use Bash to invoke anything, including your own compiled programs, other shell scripts, Python and Perl scripts etc.

In this case, there are a couple of ways to do it.

If you want to read a file, and write it to another file, doing search/replace as you go, use sed:

If you want to edit the file in place (as if opening the file in an editor, editing it, then saving it) supply instructions to the line editor ‘ex’

 echo "%s/abc/XYZ/g w q " | ex file 

Example is like vi without the fullscreen mode. You can give it the same commands you would at vi ‘s : prompt.

Not as far as I know. for f in report*.txt; do echo «%s/abc/XYZ/g \n w \n q \n» | ex file; done is clean and simple. Why put functionality into ex that the shell already has?

I found this thread among others and I agree it contains the most complete answers so I’m adding mine too:

    sed and ed are so useful. by hand. Look at this code from @Johnny:

sed -i -e 's/abc/XYZ/g' /tmp/file.txt 
x='abc' y='XYZ' sed -i -e 's/$x/$y/g' /tmp/file.txt #or, sed -i -e "s/$x/$y/g" /tmp/file.txt 

but, what can we do? As, @Johnny said use a while read. but, unfortunately that’s not the end of the story. The following worked well with me:

#edit user's virtual domain result= #if nullglob is set then, unset it temporarily is_nullglob=$( shopt -s | egrep -i '*nullglob' ) if [[ is_nullglob ]]; then shopt -u nullglob fi while IFS= read -r line; do line="$'/$server>" line="$'/$alias>" line="$'/$user>" line="$'/$group>" result="$result""$line"'\n' done < $tmp echo -e $result >$tmp #if nullglob was set then, re-enable it if [[ is_nullglob ]]; then shopt -s nullglob fi #move user's virtual domain to Apache 2 domain directory . 

The most suitable solution I can think of with the given assumptions of the problem.

Источник

How to Find Text in Files in Linux

For a system administrator, working with text files is a common phenomenon. Maybe need to find a specific section from piles of log files for troubleshooting something? Or, need to find the document that contains essential information quickly?

Читайте также:  Linux ssl certificate server

In the case of Linux, there are numerous methods to find texts in files. It’s possible using both built-in tools and 3rd-party apps. Check out how to find texts in files in Linux.

Finding text in files

Depending on the number of files you have to perform a search on, there are two ways to perform the text search: automated or manual. If you have to work with a couple of text files, the manual search is more suitable. However, if there are hundreds of text files, then the automated search is the most efficient.

For automated search, we’ll be using grep. Grep comes pre-installed on any Linux distro. As for manual search, any modern text editor will do the job.

Find text in files using grep

In Linux, grep is the default tool for searching texts. Its name is derived from the ed command g/re/p that stands for “globally search for a regular expression and print matching lines.” It’s available on any modern Linux distro.

Grep is a command-line tool. Its command structure is as follows.

As the name of grep suggests, the pattern to search for is described using a regular expression. The regular expression is a special type of string that describes a pattern to match, locate, and manage. To learn more about grep and regular expression, check out using grep and egrep with regular expression.

For demonstration purposes, grab a sample text file. In this example, download the GNU General Public License v3.0 text file.

The fundamental way of using grep is to search for a basic string.

Take a look at the following grep command. It’ll search for the word “GNU” in the text file.

To show the line number, use the “-n” flag.

To perform a case-insensitive search using grep, use the “-i” flag.

You may not want to see the search matches but only the file name where the match happened in some situations. To print only the filename, use the “-l” flag. Here, the asterisk denotes to use all the text files in the current directory.

We can also pipe the output of other commands to grep.

Regular expression

Regex offers a smart way of fine-tuning the search. It has its own rules. However, different applications and programming languages implement regular expression differently. Here are a couple of examples that you can use with grep.

To define that the string is to be found at starting a line, use the caret (^) symbol.

To define that the string is to be found at the end of a line, use the dollar sign ($).

To describe that there can be any character at a certain location of the pattern, use the period character (.). For example, the expression “G.U” is valid if there’s any character between “G” and “U”.

To describe that there can be a subset of characters at a particular location of the pattern, use the brackets ([]). For example, the expression “t[wo]o” tells that the match is valid for “two” and “too” only.

Extended regular expression

As the name suggests, an extended regular expression can do more complex things than basic regular expressions. To use extended regular expression with grep, you have to use the “-E” flag.

Читайте также:  What is my chipset linux

To search for two different strings, use the OR operators (|).

Finding text in files

Now comes the main part. Instead of manually telling grep the file to perform the search on, grep can do it automatically. In the following command, grep will use all the available text files in the current directory for searching the pattern.

If you want to grep to perform the search on a different directory, then you have to specify the location.

If there are folders, grep doesn’t explore them by default. To tell grep to search recursively, use the “-R” flag.

Grep GUI

If you prefer to work with GUI but still want to enjoy grep’s features, then check out searchmonkey. It’s a front-end solution for grep. The package is available on almost all the major Linux distros.

Find text in files using nano

GNU Nano is a simple and powerful text editor that comes with any Linux distro. It has built-in features to search for text in a text file.

Note that in this method, you have to open the text file, and search manually. It’s doable if there’s only a handful of text files to work with. If there’s more, then using grep is the most optimal choice.

Open the text file in nano.

To search for a string match, press “Ctrl + W”. After typing the string to search for, press “Enter”.

Find text in files using Vim

Vim is a well-known and reputed text editor. It’s the command-line equivalent of a modern text editor. Vim comes with numerous advanced features like plugins, macros, auto-completion, filters, etc.

Similar to nano, Vim works with a single file at a time. If you have multiple text files, then using grep is the most optimal way to go.

To search in a text file, first, open it in Vim.

Enter the following Vim command and hit “Enter”.

Find text in files using GNOME Text Editor

The GNOME Text Editor is the text editor that comes with the GNOME desktop. It’s a simplistic text editor with all the basic features you’d expect. It’s a nice alternative to the command-line text editors.

Similar to nano and vim, the same caution applies to this method. If the number of text files is large, then you better stick with grep.

Open the text file in Text Editor. Press “Ctrl + F” to bring up the search bar.

Find text in files using VS Code

Visual Studio Code is a powerful text editor with tons of features. It’s optimized for programmers to be used as if it’s a full-fledged IDE. It’s available on almost all the major Linux distros.

Install the Visual Studio Code snap package.

Open the text file in VS Code. Press “Ctrl + F” to start searching.

Final thoughts

There are numerous ways to search text in files. It’s an easy task to master. It’s strongly recommended to master the grep command because it offers the most value in terms of efficiency and ease-of-use.

If you prefer GUI, then there are numerous text editors to choose from. Any modern text editor will provide the text search option.

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.

Источник

Оцените статью
Adblock
detector