Example: Lets say this is in a file:
AB: Test CD: Testing EF: Rest
I want a script to search for CD and save "Testing" into a variable.
I've tried something with
awk but somehow I'm not making any progress.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
grep and perl style regex you can use positive lookbehind:
variable_a=`grep -Po '(?<=CD: )[^ ]+' file.txt`
In bash this says:
variable_aand give it the value of whatever comes out of the command in backticks
grepshould use perl regex (
-P) and only return the matching part of the line (