0

How can i grep for a specific pattern of numbers like

1000010000000

in a whole file - the line has to have thes numbers at the beginning not somewhere else...

  • In only one file and always the same pattern? – A.B. Jun 5 '15 at 6:10
  • Your question is ambiguous about the variability of the sequence of numbers. A sample of lines from your file (some lines that qualify and some that do not) which represent any fringe cases would go a long way to clarify your question. – mickmackusa Apr 13 at 13:32
2

For a search with that exact string 1000010000000 at the beginning of the line, a

grep "^1000010000000"

is ok.


More flexible is a pattern like this (as example):

grep -P "^1[0]{4,4}1[0]{7,7}" <your_file>

Explanation:

  • ^1[0]{4,4}1[0]{7,7}

    Regular expression visualization

    • ^ assert position at start of the string
    • 1 matches the character 1 literally
    • [0]{4,4} match a single character present in the list below
      • Quantifier: {4,4} Exactly 4 times
      • 0 the literal character 0
    • 1 matches the character 1 literally
    • [0]{7,7} match a single character present in the list below
      • Quantifier: {7,7} Exactly 7 times
      • 0 the literal character 0
| improve this answer | |
  • 6
    This s[e]{2,2}ms n[e]{2,2}dlessly complex. – thomasrutter Jun 5 '15 at 6:54
  • Why are people down-voting this answer? – Stephen Jun 5 '15 at 11:55
  • The downvotes are probably because this answer is promoting a needlessly verbose quantifier syntax as well as unnecessarily using character classes to list a single character -- as thomas cleverly insinuated. – mickmackusa Apr 13 at 13:25
1

grep '^1000010000000' filename

The caret means beginning of line.

Edit:
Just found a helpful tutorial for this sort of thing here.

| improve this answer | |
  • OP wrote " ... a specific pattern of numbers like ... " – A.B. Jun 5 '15 at 7:13
  • This is the simplest regex that would find that specific pattern of numbers at the beginning of a line. – thomasrutter Jun 7 '15 at 22:46
1

You can do

sed -n '/^1000010000000/p' file

Explanation

The -n means suppress normal output, so we only print the lines we want.

The /^1000010000000/ means match line with the pattern at the beginning of the line. The ^ is an anchor meaning the start of a line.

The p prints out the matched line.

| improve this answer | |
  • OP wrote " ... a specific pattern of numbers like ... " – A.B. Jun 5 '15 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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