Fixed width file search based on position value

Hi,
I am unable to find the right option to extract the data in the fixed width file.

sample data 

abcd1234xgyhsyshijfkfk
hujk9876       io xgla
loki8787eljuwoejroiweo
dkfj9098           dja

Search based on position 8-9="xg" and print the entire row
output 

abcd1234xgyhsyshijfkfk

Thanks
onesuri

$ awk ' substr($0,9,2) == "xg" ' file
abcd1234xgyhsyshijfkfk

The position of "xg" is 9-10 despite the phrasing in the spec. Try

$ grep -E "^.{8}xg" file
abcd1234xgyhsyshijfkfk

or

$ sed -En "/^.{8}xg/p" file
abcd1234xgyhsyshijfkfk
grep ^........xg yourfile
egrep ^.{8}xg yourfile
1 Like

Dear ctsgnb,

What a fantastic and concise coding technique. I've fought with this for years, often cobbling some terrible script round it.

Wonderful.

Robin