grep Filter lines matching a pattern
Flags
--pattern string default:
--invert boolean default: false
--caseSensitive boolean default: false
Examples
Filter log lines matching ERROR
Usage
"INFO: started
ERROR: disk full
INFO: request ok
ERROR: timeo..." | grep --pattern=ERROR Find all lines containing a specific product name
Usage
"Widget Pro - $29.99
Gadget Plus - $49.99
Widget Basic - $9.9..." | grep --pattern=Widget Extract lines with phone numbers from meeting notes
Usage
"Call John at 555-1234
Email: [email protected]
Fax is obsolete
C..." | grep --pattern=555 View source
(input, opts = {})=>{
if (!opts.pattern) return input;
try {
const flags = opts.caseSensitive ? "" : "i";
const re = new RegExp(opts.pattern, flags);
return input.split("\n").filter((l)=>(opts.invert ? !re.test(l) : re.test(l))).join("\n");
} catch {
return `Error: invalid regex "${opts.pattern}"`;
}
}