Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

XPcentric

macrumors 6502
Original poster
Oct 16, 2008
271
0
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

macrumors 6502
Original poster
Oct 16, 2008
271
0
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.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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
 

XPcentric

macrumors 6502
Original poster
Oct 16, 2008
271
0
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.
 

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
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
 

XPcentric

macrumors 6502
Original poster
Oct 16, 2008
271
0
This time I was looking for @ and $. I assign these characters to tasks, its nothing to do with their usual connotation.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.