Search code examples
linuxbashsedcsh

SED Command Replacement


Suppose I have a file with warnings. Each warning in a new line with an id that has 3 capital letters followed by 3 digits only, should be replaced by its id.

Example:

SIM_WARNING[ANA397]: Node q<159> for vector output signal does not exist

The output should be ANA397 and the rest of line is deleted.

How to do so using sed?


Solution

  • I don't think you need sed for that. A simple grep with --only-matching could do, as in:

    grep -E 'SIM_WARNING\[(.)\]' --only-matching 
    

    should work for you.

    Where:

    • -E does "enhanced regular expressions. I think we need those for capturing with ( )
    • then follows the pattern, which consists of the fixed SIM_WARNING, followed by a match inside the square brackets
    • --only-matching simply makes grep print only matching content

    In other words: by using ( match ) you tell grep that you only care about the thing in that match pattern.