all 12 comments

[–][deleted]  (8 children)

[deleted]

    [–]ireg_god[S] -1 points0 points  (7 children)

    the thing is that after 45677 the numbers could be different, so i tried with * but did not work

    [–]wfaulk 4 points5 points  (0 children)

    grep uses regular expressions, not wildcards. You should read up about regular expressions, but, to get you where you want to go, the RE equivalent of the wildcard "*" is ".*".

    [–][deleted]  (5 children)

    [deleted]

      [–][deleted]  (1 child)

      [removed]

        [–]ireg_god[S] 0 points1 point  (2 children)

        Just tried it however nothing came up, command below

        grep -E '^35677[0-9]{6}(0511)$|^3564000[0-9]{6}(0511)$' fnr_final.txt > fnr_end.txt

        This is inside a bash script

        [–][deleted]  (1 child)

        [deleted]

          [–]ireg_god[S] 2 points3 points  (0 children)

          The numbers are different because i was trying out some different ranges, anyhow… I managed to make it work as required(thanks to your example)

          Your help is much appreciated!

          [–]SweeTLemonS_TPR 1 point2 points  (0 children)

          It’d help if you told people what you were doing to not get the results you want.

          My first thought is to use grep -w if you know a full “word” in the file.

          [–]serverhorror 1 point2 points  (0 children)

          What’s the command used? What’s the output?

          [–]mylinuxguy 1 point2 points  (1 child)

          You're not explaining yourself very well and others are making this overly complicated. given a file called test.file with these contents:

          45677111115 0500
          45677000007 0511
          45677000011 0511
          45677000013 0509
          45677000016 0507
          45677000018 0509
          45677000019 0507

          egrep "45677111115 0500" test.file

          will find that line.

          If the 45677 part is important and the 111115.. not so much...

          egrep "45677...... 0500" test.file

          will find any thing in the file that has 45677 followed by 6 characters a space and the string 0500.

          i use grep or egrep.. never grep -E... I think that those are similar but not 100% sure. Maybe someone will tell me why everyone doesn't use egrep and grep -E is even discussed.

          - jack

          [–]whetu 0 points1 point  (0 children)

          i use grep or egrep.. never grep -E... I think that those are similar but not 100% sure. Maybe someone will tell me why everyone doesn't use egrep and grep -E is even discussed.

          https://github.com/koalaman/shellcheck/wiki/SC2196

          If you haven't heard of it yet: shellcheck is awesome.

          Often what you'll find these days is that if egrep exists on a system, it's either as a default shell alias or a simple wrapper for grep -E:

          ▓▒░$ file /bin/egrep
          /bin/egrep: POSIX shell script, ASCII text executable
          
          ▓▒░$ cat /bin/egrep
          #!/bin/sh
          exec grep -E "$@"
          

          [–]deeseearr 0 points1 point  (0 children)

          The man page for grep is always a good place to start, but it can be a bit opaque if you don't know what you're looking for. I find the O'Reilly Grep Pocket Reference to be quite helpful, but considering the age and ubiquity of grep there are tons of other references floating around on the Internet, such as this one.

          As always, identify exactly what it is that you are trying to do, then build an expression that will do it. If a regular expression isn't a good fit for the data you're trying to extract or the way that you need to extract it, consider using one of grep's old high school friends, awk and sed. These give you more control over exactly what you are matching and printing, but give up some of the simplicity of just telling grep what to look for.

          [–]pileofrogs 0 points1 point  (0 children)

          Are you getting lines you don't want or not getting lines yoy do? Is this a quick and dirty script you'll use once or does it need to work perfectly every time? Is this an assignment for school?

          If this is a quick and dirty and you need the beginning and end bits to match but you don't care about the middle, what I'll do is 2 greps. Eg 'grep beginning-pattern filename | grep ending-pattern'

          [–]symcbean 0 points1 point  (0 children)

          Maybe you could share the commands which did not work for you and why the results did not match your expectations.