Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7634699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:14:45+00:00 2026-05-31T07:14:45+00:00

How can I perform an operation for each item listed by grep individually? Background:

  • 0

How can I perform an operation for each item listed by grep individually?

Background:

I use grep to list all files containing a certain pattern:

grep -l '<pattern>' directory/*.extension1

I want to delete all listed files but also all files having the same file name but a different extension: .extension2.

I tried using the pipe, but it seems to take the output of grep as a whole.

In find there is the -exec option, but grep has nothing like that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T07:14:46+00:00Added an answer on May 31, 2026 at 7:14 am

    If I understand your specification, you want:

    grep --null -l '<pattern>' directory/*.extension1 | \
        xargs -n 1 -0 -I{} bash -c 'rm "$1" "${1%.*}.extension2"' -- {}
    

    This is essentially the same as what @triplee’s comment describes, except that it’s newline-safe.

    What’s going on here?

    grep with --null will return output delimited with nulls instead of newline. Since file names can have newlines in them delimiting with newline makes it impossible to parse the output of grep safely, but null is not a valid character in a file name and thus makes a nice delimiter.

    xargs will take a stream of newline-delimited items and execute a given command, passing as many of those items (one as each parameter) to a given command (or to echo if no command is given). Thus if you said:

    printf 'one\ntwo three \nfour\n' | xargs echo
    

    xargs would execute echo one 'two three' four. This is not safe for file names because, again, file names might contain embedded newlines.

    The -0 switch to xargs changes it from looking for a newline delimiter to a null delimiter. This makes it match the output we got from grep --null and makes it safe for processing a list of file names.

    Normally xargs simply appends the input to the end of a command. The -I switch to xargs changes this to substitution the specified replacement string with the input. To get the idea try this experiment:

    printf 'one\ntwo three \nfour\n' | xargs -I{} echo foo {} bar
    

    And note the difference from the earlier printf | xargs command.

    In the case of my solution the command I execute is bash, to which I pass -c. The -c switch causes bash to execute the commands in the following argument (and then terminate) instead of starting an interactive shell. The next block 'rm "$1" "${1%.*}.extension2"' is the first argument to -c and is the script which will be executed by bash. Any arguments following the script argument to -c are assigned as the arguments to the script. This, if I were to say:

    bash -c 'echo $0' "Hello, world"
    

    Then Hello, world would be assigned to $0 (the first argument to the script) and inside the script I could echo it back.

    Since $0 is normally reserved for the script name I pass a dummy value (in this case --) as the first argument and, then, in place of the second argument I write {}, which is the replacement string I specified for xargs. This will be replaced by xargs with each file name parsed from grep‘s output before bash is executed.

    The mini shell script might look complicated but it’s rather trivial. First, the entire script is single-quoted to prevent the calling shell from interpreting it. Inside the script I invoke rm and pass it two file names to remove: the $1 argument, which was the file name passed when the replacement string was substituted above, and ${1%.*}.extension2. This latter is a parameter substitution on the $1 variable. The important part is %.* which says

    • % "Match from the end of the variable and remove the shortest string matching the pattern.
    • .* The pattern is a single period followed by anything.

    This effectively strips the extension, if any, from the file name. You can observe the effect yourself:

    foo='my file.txt'
    bar='this.is.a.file.txt'
    baz='no extension'
    printf '%s\n'"${foo%.*}" "${bar%.*}" "${baz%.*}"
    

    Since the extension has been stripped I concatenate the desired alternate extension .extension2 to the stripped file name to obtain the alternate file name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of EmpIds. I need to perform some operation for each
Let's say you have a business logic method that can perform some operation across
How expensive is it to perform the dereference operation on a pointer? I can
I have a MDI WinForms application that can perform several tasks. Each task is
MSMQ: What can cause a Insufficient resources to perform operation error when receiving from
While writing a function which will perform some operation with each number in a
I'm trying to perform an operation on each character of a string which is
I've always been curious: how can I perform arithmetic operations on very long decimals--for
Possible Duplicate: Java operator overload In c++, we can perform the operator overloading. But
we can perform vim substitute on a set of lines by selecting them in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.