72896

How can I avoid “no input files” error from sed?

Question:

I have this shell script to update IP addresses in my configuration files (any that match $old_address_pattern must be changed to $new_address):

<pre class="lang-sh prettyprint-override">grep -rl "$old_address_pattern" /etc \ | xargs sed -i "s/$old_address_pattern/$new_address/g"

If the grep command finds no matching files, then sed will complain 'no input files'. How can I make this pipeline succeed when the list of files is empty?

Answer1:

If you want to avoid running sed when grep produces no output, then (since you've tagged this with Ubuntu), you can give the <strong>-r</strong> or <strong>--no-run-if-empty</strong> argument to xargs:

<blockquote>

<strong>--no-run-if-empty</strong><br /><strong>-r</strong><br /> If the standard input does not contain any nonblanks, do not run the command. Normally, the command is run once even if there is no input. This option is a GNU extension.

</blockquote>

So your command should look like:

<pre class="lang-sh prettyprint-override">grep -rl "$old" /etc | xargs -r sed -i "s/$old/$new/g"

Recommend

  • C++ how to check if file is in use - multi-threaded multi-process system
  • Python bytecode function call passing self
  • How can I restore the order of an (incomplete) select list to its original order?
  • how to define colormap with absolute values with matplotlib
  • Finding distance of rectangle with known aspect ratio in OpenCV
  • ToolTip versus Popup (WPF control)
  • GLKViewControllerDelegate getting blocked
  • Real-time data transfer from Python to MATLAB
  • Check status of process and restart process if unresponsive/stopped - Python
  • Remove unneeded Woocommerce image meta
  • Retrieving Value from Row in Struts2 Table While using Displaytag
  • python re-sample at a uniform semiannual period (equivaent of 'BQ' in pandas resample)
  • How to create device specific layouts?
  • Run perl file from PHP script but not wait for output on Windows Server
  • Images tile on Google map in android
  • Sync two textboxes with different models
  • Iterating through a folder using batch script
  • Docker build error: “could not connect to server” (behind proxy)
  • How to order the ties in data so that the previously observed value appears first
  • Retrieving Application's DisplayName/UninstallString from Registry Using Powershell
  • MSBuild does not build solution file because of mismatching package name
  • Python C binding error
  • How to delete first 7 characters of folder name by using batch script?
  • connect.cookieParser and connect.session
  • Adding Dynamic Row and Data on Checkbox Click
  • Homomorphic filtering in OpenCV
  • I need a PHP mySQL Login script that directs user to specific URL based on username and password
  • How to get “crispEdges” for SVG text?
  • Sample deviceQuery cuda program
  • JQuery Mobile Ajax Navigation in Single-Page Template
  • How to select multiple items from a List view - JavaFX 8
  • When to use the tag in the head and body section of a html page? [duplicate]
  • Create an average of multiple excel chart without the data source
  • Testing approach - DB, Junit
  • Another “Cannot make static reference…” Question