0

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?

closed as unclear what you're asking by G-Man, GAD3R, Anthon, don_crissti, Julie Pelletier Sep 7 '16 at 0:39

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Can you please edit your question to be a bit more specific on what you want to do and how grep doesn't fit in? Do you mean you want to do the search on fixed character positions on every line? Can you give an example of the input? – ilkkachu Sep 6 '16 at 13:35
  • If you're trying to match mas but not thomas, you may find word boundary anchors useful e.g. grep '\bmas\b' – steeldriver Sep 6 '16 at 13:49
  • 1
    Please edit your question and clarify. Ideally, give us a minimal example that we can play with and the output you would like to see from it. What do you mean by "position"? Line numbers? Byte numbers? Letters? Something else? Do you want to search for loc cd mas or any of loc, cd, or mas? – terdon Sep 6 '16 at 14:14
  • while I was doing a grep I used grep '^.\{'620'\}'mas'' file > fileb and it gave me the error "grep: RE error 11: Range endpoint too large." when I am trying sed -n '624,629 { /'mas'/p }' file > fileb its giving me sed: command garbled: 623,629 { /mas/p } – Sanjana Sep 6 '16 at 14:35
  • grep -n -P '^.{620}mas' file works find for me... You are probably overquoting. – JJoao Sep 6 '16 at 16:09
1

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.

  • while I was doing a grep I used grep '^.\{'620'\}'mas'' file > fileb and it gave me the error "grep: RE error 11: Range endpoint too large." when I am trying sed -n '624,629 { /'mas'/p }' file > fileb its giving me sed: command garbled: 623,629 { /mas/p } – Sanjana Sep 6 '16 at 14:35
0

I would try

awk '/mas/ && NR >= 620 && NR <= 628' file

where

  • NR (number of record) is line number
  • /mas/ is searched pattern
  • default action is to print the line
0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.