Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
Hi, im a newbie in RH9 and i have a few problems wif shell commands. can any1 pls help me wif that? i m trying to use grep to show ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2004
    Location
    Singapore
    Posts
    22

    Shell Commands


    Hi,

    im a newbie in RH9 and i have a few problems wif shell commands. can any1 pls help me wif that?

    i m trying to use grep to show me words in my /usr/share/dict/words that contain the following conditions,

    1) a single 'z' that is not adjacent to a vowel (this also includes words ending and starting wif 'z')

    i tried

    grep '[^aeiouzAEIOUZ]z\|z[^aeiouzAEIOUZ]' /usr/share/dict/words

    but it show me words that is like

    Alpheratz
    analyzable
    analyze
    analyzed
    analyzer
    analyzers
    analyzes
    analyzing
    Auschwitz
    Aztec
    Aztecan
    Balzac
    bedazzle
    bedazzled
    bedazzlement
    bedazzles
    bedazzling
    .
    .
    .

    y is this so when i have areadi included z/Z not to be in the search?
    i also tried

    grep '^z\|^Z\|z$\|Z$' /usr/share/dict/words | grep '[^aeiouzAEIOUZ]z\|z[^aeiouzAEIOUZ]'

    which shows

    Alpheratz
    Auschwitz
    Benz
    Berkowitz
    Berlitz
    Blatz
    blitz
    chintz
    Dietz
    ersatz
    Franz
    Fritz
    gigahertz
    Gonzalez
    Heinz
    Helmholtz
    hertz
    Horowitz
    Hurwitz
    .
    .
    .

    I thought i had areadi included no vowels or z infront or behind z, but y is 'Gonzalez' appearing in my search??

    Having headache now. Pls help. THanks.

  2. #2
    Linux User
    Join Date
    Jan 2003
    Location
    Cardiff, Wales
    Posts
    478

    so close

    you were so close.

    grep '[^aeiouzAEIOUZ]z\|Z[^aeiouzAEIOUZ]' /usr/share/dict/words

    this meams first part - [^aeiouzAEIOUZ]z
    OR
    second part - Z[^aeiouzAEIOUZ]'

    so you need
    grep '[^aeiouzAEIOUZ][z\|Z][^aeiouzAEIOUZ]' ./words

    have fun.
    No trees were harmed during the creation of this message. Its made from a blend of elephant tusk and dolphin meat.

  3. #3
    Linux User
    Join Date
    Jan 2003
    Location
    Cardiff, Wales
    Posts
    478

    missed one

    oops this regexp doesn't seem to work with blatz, which has a z on the end. the optional char ? doesn't seem to work. [^aeiouzAEIOUZ]?[z\|Z][^aeiouzAEIOUZ]?' ./words

    so that means we need to complicate this somewhat
    (z or Z followed by not a vowel or z) OR (expression above) OR (not a vowel or z followed by a z or Z).

    don't quite know how to plug this all together. have a google for regular expression tutorials.
    No trees were harmed during the creation of this message. Its made from a blend of elephant tusk and dolphin meat.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Mar 2004
    Location
    Singapore
    Posts
    22
    Hi kpzani,

    Thanks for your reply.

    Yes i do admit its a contradicting questions and process. It seems like commands that are supposed to work this way is not doing that but something else instead. It starts to get on my nerves everytiime i get that.. so close yet so far.sigh.

    i will try the google thing.

    here's yet another crazy questions. hope u are able to help me with this.

    All words containing "ae" and beginning with lower case letters and does not begin or end with "ae". these 4 conditions into 1. and I m supposed to use only 1 regular expression as i would need to assume that 'ae' appears at most once in ea line.

    i have tried

    egrep '[^ae]|[ae$]' /usr/share/dict/words

    and they gave me all words that do not begin or end wif ae

    i oso did

    egrep 'ae' /usr/share/dict/words

    in which they gave me words that have ae inside but when i combine both 2gether..

    egrep 'ae|[^ae]|[ae$]' /usr/share/dict/words

    they gave me the results same as [^ae]|[ae$], they did not include words with ae inside.

    so i tried this

    egrep '[:lower:]' /usr/share/dict/words

    they gave me wif capital letters words. when i leave out the '', they still gave the same results. even wif '[[:lower"]]' or with [[:lower:]], it still yields the same results. i try [:upper:], they still show me small caps letters.

    can anyone tell me what is happening in here? why isit that i cant show my lower and upper case words as i want? and why cant i combine the 3 commands into 1
    ->ae, with [^ae], [ae$]

    thanks.
    heres wat i tried.

    .
    .
    .

    which when i check the rest of the words till Z...it w

  6. #5
    Linux User
    Join Date
    Jan 2003
    Location
    Cardiff, Wales
    Posts
    478

    second one

    I've got the second one:

    egrep '^[^ABCDEFGHIJKLMNOPQRSTUVWXYZae].*ae.+' words

    so first carat = match start of string
    upper didn't work for me neither did [^A-Zae] so replaced it with long string above
    followed by .* any character repeated zero or more times.
    followed by any char at least once.
    No trees were harmed during the creation of this message. Its made from a blend of elephant tusk and dolphin meat.

  7. #6
    Linux User
    Join Date
    Jan 2003
    Location
    Cardiff, Wales
    Posts
    478

    for the first one

    for the first one you need to use look ahead and look behind
    http://www.regular-expressions.info/lookaround.html

    but I can't seem to get grep to do it. maybe it doesn't support this.

    but I think it should be something like this
    egrep '(?<![aeiouzAEIOUZ])[z\|Z](?![aeiouzAEIOUZ])' words

    this may be a perl thing. dunno!
    No trees were harmed during the creation of this message. Its made from a blend of elephant tusk and dolphin meat.

  8. #7
    Just Joined!
    Join Date
    Mar 2004
    Location
    Singapore
    Posts
    22

    Yessss

    Woah you are gd wif the 2nd 1.

    with the thought of putting any char at least once. cos as long as the 2nd last character is not 'a', then the last character can be any char. hmm that's a very good 1. I can never think that way. Thanks.
    (m i correct to say the above)

    for the 1st 1, i gt the answer from another forum. it shld be the 1 below

    grep -i '\(^\|[^aeiouzAEIOUZ]\)z\([^aeiouzAEIOUZ]\|$\)' /usr/share/dict/words

    you are quite close to it in ur attempts. Thanks Again.

    Here goes another question. If i m sort of bothering you then it's ok not to answer cos i noe i sort of like dump all questions here but believe me, i hve more questions to do than all these i posted, i only post those that i really really have problems doing atfer several/alot of attempts.

    You know if there are any other ways to extract lines from a text files other than
    cat ... | cut ....
    or
    cat ... | grep ... | cut ....
    or
    perl?

    Like for example, to extract a certain 'name' from a file given the condition that the 'time' is more than y days? as in the conditions given is not direct. if the conditions given is direct then I can happily use

    cat xy.txt | grep 'x' | cut -f1,2

    so as to show the fields in the text that corresponds to x

    but now this part requires a condition to be fulfilled as in whether the 'time' is more than y days for the whole list and to show the 'name' of those who fulfilled the 'time' condition.

    You know?

  9. #8
    Just Joined!
    Join Date
    Mar 2004
    Location
    Singapore
    Posts
    22

    Re: second one

    Quote Originally Posted by kpzani
    I've got the second one:

    egrep '^[^ABCDEFGHIJKLMNOPQRSTUVWXYZae].*ae.+' words

    followed by any char at least once.
    at least once? as in becos of the '+'? why do we need the '+'? for this situation, when i del the '+', n remove the e from egrep, i m able to get the same results. mayb for tis case is such coincidental but may i know why do we ned the '+' Cos according to a bk, the '+' need to be after a bracket expression so that it matches only 1 char in order to be qualify for its usage.

    since a '.' matches a sinbgle (non-null) char, won't that be enuff? I am really curious.

    hope you can enlightened me.

  10. #9
    Linux User
    Join Date
    Jan 2003
    Location
    Cardiff, Wales
    Posts
    478

    defaults

    the defaults with grep include something like .*pattern.* by default. so anything you type in will be found.

    So the pattern I gave is 100% correct. but the defaults in grep mean that it will work without the + as the actual pattern executed is something like this:
    '^[^ABCDEFGHIJKLMNOPQRSTUVWXYZae].*ae..*'

    Does that make sense?

    But there is still a problem with it. it doesn't actually pick up any words that start with a or e. Oops. Don't know how to fix this. my knowledge has long been exhausted.

    in answer to the other question I think you need to start using shell scripts. you can of course pipe commands together - which might achieve what you're after.
    No trees were harmed during the creation of this message. Its made from a blend of elephant tusk and dolphin meat.

  11. #10
    Just Joined!
    Join Date
    Mar 2004
    Location
    Singapore
    Posts
    22
    Ya i do realise that aft ur post...hmm i try to get it then..thanks for all ur help

    another prob, if i were to ned to extract some data from a text file, x hr, and the data that i ned to retrieve is those y name that corresponds with hrs above x hr. How do i do that?..i try...

    cut -f2,3 abc.txt | grep [0-x]

    bt they do not show me values that are between 0 - x, but oso show above x values. how isit so?..m i approaching hte qn the rite way? m i xplaning it clearly?

Page 1 of 2 1 2 LastLast

Posting Permissions

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