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

2
  • In only one file and always the same pattern?
    – A.B.
    Jun 5, 2015 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. Apr 13, 2020 at 13:32

3 Answers 3

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
3
  • 6
    This s[e]{2,2}ms n[e]{2,2}dlessly complex. Jun 5, 2015 at 6:54
  • Why are people down-voting this answer?
    – Stephen
    Jun 5, 2015 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. Apr 13, 2020 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.

2
  • OP wrote " ... a specific pattern of numbers like ... "
    – A.B.
    Jun 5, 2015 at 7:13
  • This is the simplest regex that would find that specific pattern of numbers at the beginning of a line. Jun 7, 2015 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.

1
  • OP wrote " ... a specific pattern of numbers like ... "
    – A.B.
    Jun 5, 2015 at 7:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .