Home > OS >  Cut a string in two groups in bash
Cut a string in two groups in bash

Time:01-29

Lets say I have this string:

A single (very ugly) tree (Outdoor treks) 2.

I wanna split it in 2 groups: the first one contains the substrings that are not in (), and the second one the last substring that is in ()

1stGroup:

A single tree 2

2ndGroup:

Outdoor treks

CodePudding user response:

$ firstGroup=$(sed 's/\([^(]*\)([^)]*)/\1/g' input_file)
$ secondGroup=$(sed 's/.*(\([^)]*\)).*/\1/' input_file)
$ echo "$firstGroup"
A single tree 2.
$ echo "$secondGroup"
Outdoor treks
  •  Tags:  
  • Related