How do I extract all words which use a certain character from a text file ?

Discussion in 'Mac Programming' started by XPcentric, Feb 12, 2013.

  1. macrumors regular

    Joined:
    Oct 16, 2008
    #1
    I want to extract all words which have a character (ie. $) from a text. Is there a simple command in Terminal or even text editors ? Thanks!
     
  2. macrumors 6502a

    ConCat

    Joined:
    Jul 27, 2012
    Location:
    In an ethereal plane of existence.
    #2
    Most text editors have a "find" function where you can search for anything containing any number of letters.
     
  3. thread starter macrumors regular

    Joined:
    Oct 16, 2008
    #3
    Yes but I want to extract all of the different words which include that character. When the file is long, is hard to copy-paste all words one by one. There must be a Unix command which I'm searching now, before anyone post an answer.
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Assuming you are in the correct directory containing the file, the file is called file.txt and you want the list of words in output.txt then this will do it (without correcting for caps/non-caps). We are searching for c. If you want to search for $ you might well have to escape it

    Code:
    cat file.txt | tr -d '[:punct:]' | tr ' ' '\n' | grep c | sort | uniq > output.txt
    
     
  5. thread starter macrumors regular

    Joined:
    Oct 16, 2008
    #5
    Thank you so much, you saved me good amount of searching. I finally had to replace the special character to do the task quicker. I tried to escape it with single and double quotes but it didn't work.
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    What is the special character? $ or something else?
     
  7. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #7
    $ is a special character in regular expressions (end of line). I generally escape it like this \[\$]. The backslashes are probably overkill.

    B
     
  8. thread starter macrumors regular

    Joined:
    Oct 16, 2008
    #8
    I don't know if 'special character' is the right expression, I just wanted to extract those.
     
  9. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #9
    What character are you wanting to extract the words containing?
     
  10. thread starter macrumors regular

    Joined:
    Oct 16, 2008
    #10
    This time I was looking for @ and $. I assign these characters to tasks, its nothing to do with their usual connotation.
     
  11. macrumors 65816

    Partron22

    Joined:
    Apr 13, 2011

Share This Page