I need to get a specific string from a bigger string:
From these Abcd1234_Tot9012_tore.dr
or Abcd1234_Tot9012.tore.dr
I want to get those numbers which are between Tot
and _
or .
, so I should get 9012
. Important thing is that the number of characters before and after these numbers may vary.
Could anyone give me a nice solution for this? Thanks in advance!
awk
sed
Example
This should also work if you are looking only for numbers after Tot
I know this is tagged as bash/sed but perl is clearer for this kind of task, in my opinion. In case you're interested:
-ne
tells perl to loop the specified one-liner over the input file without printing anything by default.The regex is readable as: match Tot, followed by a number, followed by either a dot or an underscore; capture the number (that's what the parens are for). As it's the first/capture group it's assigned to the
$1
variable, which then is printed.This might work for you:
This works equally as well:
Pure Bash:
Remove longest leading part ending with '_Tot'.
Remove longest trailing part beginning with '_' or '.'.
Result:
You can use
perl
one-liner:Test: