Bash script to search string in file




















Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Related 5. Hot Network Questions. Question feed. Accept all cookies Customize settings. To replace a string in a file using a Bash script you can use the sed command.

The sed command allows to perform text manipulation and it can be called as part of your scripts in the same way you can do with other Linux or Unix commands. A practical example in which replacing strings in a file can be handy is when you want to generate files based on templates where certain strings are updated with user inputs. First of all, we will look at a few examples on how to use sed in the terminal and then I will show you how to call sed in your Bash scripts to replace one or multiple strings in a file.

I will start with a simple example that shows how to use the sed command directly in the Linux command line. Then using the pipe I will pass the output of the echo command to the sed command and replace the words Athens and Greece with Rome and Italy.

I have used two pipes with two sed commands, one to replace the word Athens with Rome and the other one to replace Greece with Italy. They are both applied to the output of the echo command. We replace the string Athens with the string Rome in the message printed by the echo command. As we will see in the next section, we can also use a similar sed command to replace strings in a file. As you can see we can use a single sed command and this time within the single quotes we specify two regular expressions separated by semicolon.

Now, instead of replacing strings in a message generated by the echo command, we will create a file that contains the same message.

We can then use the same sed syntax we have seen in the previous section, this time we specify the sed command followed by the name of the file. To update the content of the file with the sed command we need to pass an additional flag, the -i flag that edits files in place.

I have updated the content of the message. The letter g , that stands for global and replaces all the occurrences of a string in the file. Before executing the sed command we verify that the number of arguments passed to the Bash script is correct.

We stop the execution of the script with the exit command and exit code 1 if the number of arguments is incorrect. Finally, we run the sed command using the syntax explained in the previous section, the only difference is that we specify variable names in the sed command.

To troubleshoot a Bash script you can add bash -x before the script name in the terminal. The problem is that the variables in the sed command are not being replaced with the values we have passed to the script. To interpolate variables use double quotes instead. Therefore, to interpolate the variables inside the sed expression we need to use double quotes. Sign up or log in Sign up using Google.

Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Linked Related 0. Hot Network Questions. Question feed. Accept all cookies Customize settings. You don't need [[ ]] here. Just run the command directly. Add -q option when you don't need the string displayed when it was found.

The grep command returns 0 or 1 in the exit code depending on the result of search. You can specify commands as an condition of if. If the command returns 0 in its exitcode that means that the condition is true; otherwise false.

As you can see you run here the programs directly. No additional [] or [[]]. In case if you want to check whether file does not contain a specific string, you can do it as follows. In addition to other answers, which told you how to do what you wanted, I try to explain what was wrong which is what you wanted. In Bash, if is to be followed with a command. If the exit code of this command is equal to 0, then the then part is executed, else the else part if any is executed. Another thing is the way you quote things.

In case you want to checkif the string matches the whole line and if it is a fixed string, You can do it this way. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to check if a file contains a specific string using Bash Ask Question. Asked 9 years, 6 months ago.



0コメント

  • 1000 / 1000