Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 12, 2013, 04:41 PM   #1
XPcentric
macrumors regular
 
Join Date: Oct 2008
How do I extract all words which use a certain character from a text file ?

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!
XPcentric is offline   0 Reply With Quote
Old Feb 12, 2013, 04:51 PM   #2
ConCat
Banned
 
Join Date: Jul 2012
Location: In an ethereal plane of existence.
Most text editors have a "find" function where you can search for anything containing any number of letters.
ConCat is offline   0 Reply With Quote
Old Feb 12, 2013, 04:54 PM   #3
XPcentric
Thread Starter
macrumors regular
 
Join Date: Oct 2008
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.
XPcentric is offline   0 Reply With Quote
Old Feb 12, 2013, 04:56 PM   #4
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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
robbieduncan is offline   0 Reply With Quote
Old Feb 12, 2013, 05:17 PM   #5
XPcentric
Thread Starter
macrumors regular
 
Join Date: Oct 2008
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.
XPcentric is offline   0 Reply With Quote
Old Feb 12, 2013, 05:25 PM   #6
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
What is the special character? $ or something else?
robbieduncan is offline   0 Reply With Quote
Old Feb 12, 2013, 05:27 PM   #7
balamw
Moderator
 
balamw's Avatar
 
Join Date: Aug 2005
Location: New England, USA
Quote:
Originally Posted by XPcentric View Post
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.
$ is a special character in regular expressions (end of line). I generally escape it like this \[\$]. The backslashes are probably overkill.

B
__________________
MBA (13" 1.7 GHz 128GB), UMBP (15" SD 2.8 GHz), UMB (13" 2.4 GHz), iMac (17" Yonah), 32GB iPad 3 WiFi+LTE, 64 GB iPad WiFi, 32 GB iPhone 5, Airport Extreme
balamw is offline   0 Reply With Quote
Old Feb 13, 2013, 07:04 AM   #8
XPcentric
Thread Starter
macrumors regular
 
Join Date: Oct 2008
Quote:
Originally Posted by robbieduncan View Post
What is the special character? $ or something else?
I don't know if 'special character' is the right expression, I just wanted to extract those.
XPcentric is offline   0 Reply With Quote
Old Feb 13, 2013, 08:10 AM   #9
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by XPcentric View Post
I don't know if 'special character' is the right expression, I just wanted to extract those.
What character are you wanting to extract the words containing?
robbieduncan is offline   0 Reply With Quote
Old Feb 13, 2013, 01:15 PM   #10
XPcentric
Thread Starter
macrumors regular
 
Join Date: Oct 2008
This time I was looking for @ and $. I assign these characters to tasks, its nothing to do with their usual connotation.
XPcentric is offline   0 Reply With Quote
Old Feb 14, 2013, 11:18 AM   #11
Partron22
macrumors 6502a
 
Join Date: Apr 2011
Convert each word to a line, and use sed: http://unix.stackexchange.com/questi...r-standard-sed
Partron22 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
switching some text's words but not others in script fallleaves03 Mac Programming 0 Sep 25, 2013 09:57 PM
Underlined Words in Text and iMessage? pssmith84 iOS 7 3 Jun 12, 2013 10:30 AM
How Do You Extract URL's From Text Files ? jacobg70 OS X 4 Aug 21, 2012 05:55 PM

Forum Jump

All times are GMT -5. The time now is 12:54 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC