"grep"ing first 12 of last 24 character from a line

awk, grep, sed, shell

Solution

One way with `GNU sed` (without counting dots):

$ sed -r 's/.*(.{11}).{12}/\1/' file
0.41207E-09

Similarly with `GNU grep`:

$ grep -Po '.{11}(?=.{12}$)' file
0.41207E-09

Perhaps a `python` solution may also be helpful:

python -c 'import sys;print "\n".join([a[-24:-13] for a in sys.stdin])' < file
0.41207E-09

I'm not sure your example data and question match up so just change the values in the `{n}` quantifier accordingly.

Problem

I am trying to extract "first 12 of last 24 character" from a line, i.e., for a line: ``` species,subl,cmp= 1 4 1 s1,torque= 0.41207E-09-0.45586E-13 ``` I need to extract "0.41207E-0". (I have not written the code, so don't curse me for its formatting. ) I have managed to do this via: ``` var_s=`grep "species,subl,cmp= $3 $4 $5" $tfile |sed -n '$s/.*\(........................\)$/\1/p'|sed -n '$s/\(............\).*$/\1/p'` ``` but, is there any more readable way of doing this, rather then counting dots? EDIT Thanks to both of you; so, I have sed,awk grep and bash. I will run that in loop, for 100's of file. so, can you also suggest me which one is most efficient, wrt time?

Original source