How to match exact string using `sed`? But not the part of it.?

Use word boundaries:

grep '\bNAME1\b'

would match NAME1 and not NAME1XYZ or XYZNAME1.

Similarly,

sed -n '/11\b/,/14\b/p'

wouldn't match lines containing 111 and 142.


EDIT: It seems that the numbers in the input file are actually line numbers. If that is the case, you could simply say:

sed '11,14!d'

to get the desired lines.


You can use AWK

awk 'NR>=13 && NR<=17 && /NAME/{print $NF}' infile

This will look lines between 13 to 17 then search for Name and if match then it will print last word from Name = LastWord


You don't need any other tool for this, sed will easily handle it entire.

sed -nr '/11/,/14/{s/^.*NAME =\s*(\S*).*$/\1/p}' <$infile

That should provide you only with the first sequence of non-whitespace characters following the phrase "NAME = " for every line on which that phrase is found between lines 11 and 14 of any input file sed is fed.