sed - Linux: Remove lines starting with [ -


i know can use grep -v '^#' remove lines starting #

now run issues when try remove [ ie. grep -v '^[' or sed '/^[/ d'

why happening , how can accomplish this?

consider test file:

$ cat brackets  keep [remove] 

using grep:

$ grep -v '^\[' brackets keep 

or:

$ grep -v '^[[]' brackets keep 

using sed:

$ sed '/^\[/d' brackets  keep 

or:

$ sed '/^[[]/d' brackets  keep 

why

when computer command fails work expected, important @ error message. consider:

$ grep -ve '^[' brackets grep: invalid regular expression 

the error message reporting invalid regular expression found. because [ regex-active character: [...] used define character list. thus, if regex contains unescaped [, must contain matching ]. there 2 ways avoid this:

  1. escape it. if [ regex-active character, \[ treated regular (inactive) character.

  2. put in character list. [[] character list matches 1 character: [.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -