Skip to content
Advertisement

How to match and cut the string with different conditions using sed?

I want to grep the string which comes after WORK= and ignore if there comes paranthesis after that string .

The text looks like this :

JavaScript

So, desirable output should print only :

JavaScript

So far , I could just match and cut before WORK= but could not remove WORK= itself:

JavaScript

I am not sure how to continue . Can anyone help please ?

Advertisement

Answer

You can use

JavaScript

Details:

  • -n – suppresses the default line output
  • /WORK=.*([^()]*)/! – if a line contains a WORK= followed with any text and then a (...) substring skips it
  • s/.*WORK=([^,]*).*/1/p – else, takes the line and removes all up to and including WORK=, and then captures into Group 1 any zero or more chars other than a comma, and then remove the rest of the line; p prints the result.

See the sed demo:

JavaScript

Output:

JavaScript
Advertisement