regex - Sed command to remove everything following the colon

25
2013-11
  • Dzung

    I have the collection of text files containing/not the phrase (B core file).

    Then I tried this:

    sudo find / -type f -name core | xargs file | grep 'B core file' | sed 's/:ELF.*//g' | xargs rm -f 
    

    To remove specific files with the phrase "B core file".

    But that command won't work.

    I'm hoping for the solution. Thank you very much.

  • Answers
  • ceving

    Only the find command will be run with root permissions. The rm will be run with your user id and because of this will fail. Put the whole command in a script and run the script with sudo.

    You can also use an extra sudo for each command, which access the files, which are probably not readable or writable for you:

    sudo find / -type f -name core | xargs sudo file | sudo grep 'B core file' | sed 's/:ELF.*//g' | xargs sudo rm -f 
    
  • Mikael E

    That looks overly complicated.

    try:

    grep -l 'B core file' * | xargs rm

    It will remove files containing 'B core file' from the directory you're standing in when running this.


  • Related Question

    text - using sed to remove lines in a file
  • eleven81

    I have a file that looks something like this:

    Heading - 
      - Completed foo
        - More information
        - Still more
      * Need to complete bar
      - Did baz (comment blah blah) ***
    
    Another - 
      * Need to complete foo
      - Completed bar (blah comment blah) ***
      - Done baz
    

    I need to run the text file through sed to remove all of the lines that start with spaces (number varies) and a hyphen, and another space.

    What is the regex or pattern I need to use with sed to make the output look like this below?

    Heading - 
      * Need to complete bar
    
    Another - 
      * Need to complete foo
    

  • Related Answers
  • eleven81

    I used Phoshi's answer, assisted by Dennis Williamson, to help me come up with sed /^\s+-\s.*/d which works as expected.

  • Phoshi

    "s/\s*-\s.*//g" should do it, I think.

    That's \s to match a space, * to match zero or more of the preceding character (the space), a literal hyphen character, then another space, then .+ to match everything after it.

  • Ryan Thompson

    You should use egrep or grep for this task, sed is a stream editor, grep is more in line with the line-at-a-time philosophy.

    You need a regex that matches the start of line, whitespace, hyphen, space. Sounds like this would work:

    egrep  -v  '^[ ]+-[ ]' filename
    

    The -v option causes egrep to REMOVE the matching lines -- this is easier than building a regex that rejects the lines.

    Example:

     nobody$ egrep -v  '^[ ]+-[ ]' /tmp/foof
     Heading - 
       * Need to complete bar
    
     Another - 
       * Need to complete foo
     nobody$ cat /tmp/foof
     Heading - 
       - Completed foo
         - More information
         - Still more
       * Need to complete bar
       - Did baz (comment blah blah) ***
    
     Another - 
       * Need to complete foo
       - Completed bar (blah comment blah) ***
       - Done baz
     nobody$ _
    

    Dealing with Tab characters only means you need them in the bracket expressions,but that's hard to show online.