Results 1 to 7 of 7

Thread: Shell: remove all lines containing specified pattern from all files in a folder

  1. #1
    Join Date
    Dec 2009
    Beans
    548

    Shell: remove all lines containing specified pattern from all files in a folder

    Hi,
    I would like to remove all lines containing a specified pattern from all files in a folder. I know that the command
    Code:
    grep -v "patternr" ./file > temp && mv temp file
    does the first part i.e. removes all lines containing a specified pattern from a specific file. How can I achieve this for all files in a folder?
    I tried something like this but it does not work.
    Code:
    grep -Rv "pattern" ./folder > temp && mv temp file
    Do I need necessarily iterate over the files of the folder or there is a more simple way?
    Thank you
    Last edited by erotavlas; December 4th, 2015 at 06:25 PM.

  2. #2
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Shell: remove all lines containing specified pattern from all files in a folder

    what's wrong with loops?

    Code:
    shopt -s globstar
    sed -r -i '/pattern/d' ./folder/**/*
    globstar for recursiveness, if the files are directly under folder then folder/* would cut it

    without globstar

    Code:
    find ./folder -type f -exec sed -r -i /pattern/d +
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  3. #3
    Join Date
    Dec 2009
    Beans
    548

    Re: Shell: remove all lines containing specified pattern from all files in a folder

    The second solution does not work
    Code:
    find ./folder -type f -exec sed -r -i /pattern/d +
    find: missing argument to `-exec'
    I also tried to remove + and put ; without difference.

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Shell: remove all lines containing specified pattern from all files in a folder

    a case of brainfart

    Code:
    find ./folder -type f -exec sed -r -i '/pattern/d' {} +
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  5. #5
    Join Date
    Dec 2009
    Beans
    548

    Re: Shell: remove all lines containing specified pattern from all files in a folder

    Ok thank you

  6. #6
    Join Date
    Dec 2009
    Beans
    548

    Re: Shell: remove all lines containing specified pattern from all files in a folder

    I'm sorry, but I have one more question. Now I would like to replace a string with another in each file inside a folder.
    Code:
    find ./folder -type f -exec sed -i 's/Hobbies</span></a> |/Hobbies</span></a>/g' {} +
    I make some mistake, or have I to escape some character?
    Code:
    sed: -e expression #1, char 19: unknown option to `s'
    Thank you

  7. #7
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Shell: remove all lines containing specified pattern from all files in a folder

    Quote Originally Posted by erotavlas View Post
    Code:
    find ./folder -type f -exec sed -i 's/Hobbies</span></a> |/Hobbies</span></a>/g' {} +
    The slash ( / ) is a the delimiter so you can't use it in any other way without escaping it.

    Code:
    find ./folder -type f -exec sed -i 's/Hobbies<\/span><\/a> |/Hobbies<\/span><\/a>/g' {} +
    Alternately, you can use different delimiters like a pound sign ( # ).

    Code:
    find ./folder -type f -exec sed -i 's#Hobbies</span></a> |#Hobbies</span></a>#g' {} +

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •