Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Extracting 3word lines from files

  1. #1
    Junior Member
    Join Date
    Feb 2002
    Posts
    59

    Extracting 3word lines from files

    HI Everyone,

    I am plumb stumped on this one.
    What I need to be doing is to extract all the words that have only 3 letters to the word per line. So only lines that have 3 characters per line.

    I have read teh tutorial here at linuxjunior and plus my two textbooks plus my two personal unix books that i have bought and after the full day of banging my head with grep and such I am no where futher than where i started this morning.

    So just so you have idea what Im talking about.
    Say my file consist of these words.

    Code:
    Linux
    Unix
    Redhat
    Cat
    rabbit
    dog
    Konquerer
    KDE
    Flux
    The only output i want to see are the words.
    Cat
    dog
    KDE

    I can't seem to accomplish this at all from the CL.

    TIA

    Edit: One other question, I often see this result when doing commands, I will type command hit enter and it will return with this...
    MORE:>
    What does that mean? I can't find any reference on this.

  2. #2

    Re:Extracting 3word lines from files

    Here's a small Python script that should do the trick:
    Code:
    import sys
    
    try:
      f = open(sys.argv[1])
      lines = f.readlines()
      for line in lines:
        if len(line) == 4:  # We also count newlines (\n)
          print line
    except Exception, e:
      print "Uncaught exception:", e

  3. #3
    Junior Member
    Join Date
    Feb 2002
    Posts
    59

    Re:Extracting 3word lines from files

    Yes I know a script would be easier, however we are not allowed to use a script for this particular question. It has to be done from the command line. :-\

  4. #4
    Senior Member
    Join Date
    Jul 2001
    Posts
    280

    Re:Extracting 3word lines from files

    Did you try the suggestion I gave @ techimo?

  5. #5

    Re:Extracting 3word lines from files

    Code:
    [vince@vincent: ~/prog/python]% grep '^[a-zA-Z][a-zA-Z][a-zA-Z]$' heh 
    KDE
    Vim
    Bar
    Foo

  6. #6
    Junior Member
    Join Date
    Feb 2002
    Posts
    59

    Re:Extracting 3word lines from files

    Type in this
    grep '^[a-zA-Z][a-zA-Z][a-zA-Z]$' /home/pathfilename
    and got cannot open file.

    [quote author=GnuVince link=board=10;threadid=4337;start=0#43222 date=1027708856]
    Code:
    [vince@vincent: ~/prog/python]% grep '^[a-zA-Z][a-zA-Z][a-zA-Z]$' heh 
    KDE
    Vim
    Bar
    Foo
    [/quote]

  7. #7
    Junior Member
    Join Date
    Feb 2002
    Posts
    59

    Re:Extracting 3word lines from files

    [quote author=t048 link=board=10;threadid=4337;start=0#43220 date=1027708256]
    Did you try the suggestion I gave @ techimo?
    [/quote]

    Yes I did, same response. ???

  8. #8
    Junior Member
    Join Date
    Feb 2002
    Posts
    59

    Re:Extracting 3word lines from files

    grep '^[a-zA-Z][a-zA-Z][a-zA-Z]$' pathfilename
    Killer dude!!! I couldn't get the thing to work, so it keep saying cannot open file im like what the hey, i can open the file doinga cat. well apparently or something cat is not as case sentitive or something, dunno. ???

    Anyhow i decided to do a ls -a of the direcotry the file is in and whoa dude, i added 4 charachters too much to the end of the words file.
    its jut plain ole words nothing else.
    i was putting in words.dat

    Im a happy camper now Thanks guys! You're great!!!!!!

  9. #9
    Junior Member
    Join Date
    Feb 2002
    Posts
    59

    Re:Extracting 3word lines from files

    hmmm okay, what if a file had words like this in it.

    u.s

    why doesn't the same command as posted above include these?


    EDIT:
    Okay got it, had to do with a few pipes. Ended up doing this...
    grep '^U[a-zA-Z|.][a-zA-Z|.]$' | grep '^[a-zA-Z][a-zA-Z][a-zA-Z]$'


  10. #10
    Mentor
    Join Date
    Jun 2001
    Posts
    1,672

    Re:Extracting 3word lines from files

    [quote author=NeoStarO1 link=board=10;threadid=4337;start=#43231 date=1027710968]
    hmmm okay, what if a file had words like this in it.

    u.s

    why doesn't the same command as posted above include these?
    [/quote]

    Try it like this. Looks cleaner anyway:

    Code:
    grep '^...$' path/filename

Similar Threads

  1. deleting previous 'n' lines of matching the pattern
    By Suhas! in forum Linux - Software, Applications & Programming
    Replies: 3
    Last Post: 05-02-2007, 03:45 PM
  2. PERL: get lines between 2 markers
    By Blaqb0x in forum Linux - Software, Applications & Programming
    Replies: 4
    Last Post: 11-15-2002, 09:35 AM
  3. lines across screen after gentoo install
    By blueminder in forum Linux - General Topics
    Replies: 7
    Last Post: 08-06-2002, 01:18 PM
  4. deb sources.list lines needed please
    By capybara in forum Linux - General Topics
    Replies: 4
    Last Post: 07-23-2002, 09:44 PM
  5. Geek Pickup lines
    By in forum General Chat
    Replies: 29
    Last Post: 12-22-2001, 07:36 PM

Bookmarks

Posting Permissions

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