I want to use bash shell to split string like:
Calcipotriol - Daivonex Cream 50mcg/1g 30 g [1]
Aspirin - DBL Aspirin 100mg [1] tablet
I want to get brand name "Davionex Cream" and "DBL Aspirin" I want to get the name in front of parttern ***mg or ***mcg or ***g
how to do it?
You can use
sed
this way:^[[:alpha:]]+ -
=> matches all the characters until the pattern we need to extract([[:alpha:] ]+)
=> this is the part we want to extract[[:digit:]]+.*
=> this is everything that comes after; we assume this part starts with a space and one or more digits, followed by any number of characters\1
=> the part extracted by the(...)
expression above; we replace the entire string with the matched partYou can check out this site to learn more about regexes: http://regexr.com/
If your sample input is representative,
awk
may offer the simplest solution:yields:
In Bash you can do:
Prints: