user6920966
user6920966

Reputation: 33

How to select section in regular expression in linux commands

I have these lines that every line begin a word then equal and several sentence so I like select every section. For example:

      delete = \account
          user\
          admin
           admin right is good.
      add = \
          nothing
          no out
          input output is not good 
      edit = permission 
          bob
          admin
          alice killed bob!!!

I want to select a section for example:

  add = \
      nothing
      no out
      input output is not good 

I like do it with regular expression.

Upvotes: -2

Views: 153

Answers (2)

Cœur
Cœur

Reputation: 38717

Solution by OP.

I find this solution:

csplit -k fileName '/.*=/' '{*}' 

Thanks @haggisandchips

Upvotes: 0

haggisandchips
haggisandchips

Reputation: 542

Your question is a bit vague but you could try the following ...

/\s*(\w+) = ([^=]*\n)*/m

... subject to the requirement that the last section is terminated with \n.

this works by:

  • '\s*' matching some optional leading whitespace
  • '(\w+)' capturing the name of the section
  • ' = ' matches the space equals space separator
  • '([^=]*\n)' it then captures a string that does not include an equals and ends with a newline
  • '*' and it does that last bit multiple times

The m flag is then required to set multi-line.

See the following to quickly see the groups that are output for each match ...

https://regex101.com/r/oDKSy9/1

(NOTE: The g flag will probably not be required depending on how you use the regex.)

Upvotes: 0

Related Questions