Results 1 to 11 of 11

Thread: grep pattern

  1. #1
    Join Date
    Oct 2003
    Posts
    14

    Exclamation Unanswered: grep pattern

    hi, me again with prob guru.... i have a .txt file with numeric and words together eg . hello 1
    2 how
    are 3 you

    which pattern do u used to get just the numeric ? eg 1 2 3
    i tried many pattern .... grep [0-9] test.txt , i tried from * to ?
    can u help me guru

  2. #2
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Are you wanting to edit the input line to produce just the numeric portion? If so, grep is not the tool to use. Try sed...

    sed 's/[^0-9]//g'

    This will substitute every character NOT (that's the ^) in [0-9] for an empty string.

    HTH

  3. #3
    Join Date
    Oct 2003
    Posts
    14
    yes, i am trying to get the numeric in the text file and sum it up ... thanx guru, never thought sed can do that

  4. #4
    Join Date
    Oct 2003
    Posts
    14
    guru, just say that one of the words in the txt is "world4" but i dont want the 4 to be part of the output, what pattern can be used for it ?

  5. #5
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    This would work for that specific example...

    sed 's/[a-Z][a-Z]*[0-9]*//g'

  6. #6
    Join Date
    Oct 2003
    Posts
    14
    the sed for that is not working
    i have error : sed: -e expression #1, char 22: Invalid range end

    btw, can u explain briefly ? i can't seem to understand why twice the [a-Z]

    edit : my txt consist of hello 1
    2 how
    are 3 you
    today4

    so my output should be 1 2 3 only ... can't seem to ge the correct pattern....
    Last edited by jinroh; 10-13-03 at 10:18.

  7. #7
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Different version of sed!

    Replace a-Z with a-zA-Z.

  8. #8
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Originally posted by jinroh

    btw, can u explain briefly ? i can't seem to understand why twice the [a-Z]
    The * matches 0 or more occurences, you want at least one.

    The following are all the same but I wouldn';t expect them all to work on all versions of sed...

    [a-z][a-z]*
    [a-z]+
    [a-z]\{1,\}

  9. #9
    Join Date
    Oct 2003
    Posts
    14
    thanx , this sed 's/[a-z][A-Z]*[0-9]*//g' test.txt works ... but if you dont mind, can plz explain a bit ? i really want to learn , thanx guru

  10. #10
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Originally posted by jinroh
    thanx , this sed 's/[a-z][A-Z]*[0-9]*//g' test.txt works ... but if you dont mind, can plz explain a bit ? i really want to learn , thanx guru
    substitute/this/forThis/globally

    The 'this' pattern breaks down as...

    1 character in the range a-Z followed by zero or more characters in the range a-Z followed by zero or more characters in the range 0-9.

    This would match:

    world4
    world
    world44

    but not...

    world 4
    4


    HTH

  11. #11
    Join Date
    Oct 2003
    Posts
    14
    thanx guru for helping me learn one or more thing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •