I'm not sure why this question title has been edited "from file" to "from a line" while the OP doesn't exclude the possibility across multiple lines even though the example seems to be one line only. Whatever, it might helpful to provide multiple lines solution here.
This works for cross-lines (This answer works if from1
and to2
exist in the file.):
from1=consectetuer; to2=elit; a="$(cat file)"; a="$(echo "${a#*"$from1"}")"; echo "$from1${a%%"$to2"*}$to2"
Examples:
[xiaobai@xiaobai tmp]$ cat file
1
abc consectetuer lsl
home
def elit dd
2 consectetuer ABC elit
[xiaobai@xiaobai tmp]$ from1=consectetuer; to2=elit; a="$(cat file)"; a="$(echo "${a#"$from1"}")"; echo "$from1${a%%"$to2"}$to2"
consectetuer lsl
home
def elit
[xiaobai@xiaobai tmp]$
reference: Shell Parameter Expansion
sed
. It can also beperl
, or even pure bash. – muru Nov 15 '15 at 22:01