13 Jul
2010
13 Jul
'10
4:54 p.m.
On 13 Jul 16:47, Brett Parker wrote:
On 13 Jul 16:28, Richard Parsons wrote:
Friends,
I admit that I am terrible with regex. I need a script to output all words beginning with "$" from a text file. Here's what I've got so far:
cat file.txt | grep "$" | sed <some-regex>
How about:
sed -e 's#[^$]*\(\$[^[:space:]]*\)#\1 #g; s#\(.*\)\([$][[:alpha:]]*\).*$#\1\2#; s# #\n#g;' file.txt
Doesn't work if there's not a $ on a line, though, it then splits that line up... so, a revision later... sed -e '/[$]/ { s#[^$]*\(\$[^[:space:]]*\)#\1 #g; s#\(.*\)\([$][[:alpha:]]*\).*$#\1\2#; s# #\n#g; p; }; d;' file.txt But, Martijn has a neater solution in the form: grep -E -o '\$\w+' file.txt (Dunno if he's going to post that though :) -- Brett Parker