Question: The file size is huge and I have to extract only the lines that match a certain location code which is of variable ( varying from 2 bytes to 8 bytes) length. 

I cannot use normal grep since that may give undesirable results. When I search for loc cd mas, if I give `grep mas`, it matches with name thomas... 

Since this is a fixed length file, I know the position where to search. Is there any command to search at particular position(from 620-628 in my case) and extract the matching lines?

Answer: 
`sed` is a good fit for this

```
sed -n '620,628 { /mas/p }'

```

Now it's also possible to have `sed` terminate (so run quicker) after you've read up to the last line you care about; eg

```
sed -n -e '620,628 { /mas/p }' -e '629q'

```

For complete word matches you can change the expression by surrounding it with `\b`

eg

```
sed -n -e '620,628 { /\bmas\b/p }' -e '629q'

```

Now that will match `this contains mas and stuff` but will *not* match `this is a masterful piece of work`.
I would try

```
grep -P '^.{620}mas' file

```

If your pattern is changing, you can use variables

```
PAT='mas\b'
grep  -nP "^.{620}$PAT" file

```