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
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
  • 5
    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
  • @Stephen I have no idea. :\ – A.B. Jun 5 '15 at 11:56
  • you deserve an upvote!! @A.B. – Ramana Reddy Dec 29 '15 at 10:46
1

grep '^1000010000000' filename

The caret means beginning of line.

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

  • 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.

  • 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.