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> I don't want the whole line, just the word. Anyone help? :) Thanks, Richard
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 Cheers, -- Brett Parker
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
On 13/07/10 16:54, Brett Parker wrote:
But, Martijn has a neater solution in the form: grep -E -o '\$\w+' file.txt
Just beaten to the post! But I'll post my version in case it's useful: grep -oE '\$\w+' file.txt | sort -u -E enables regex matching -o tells grep to only output the match (not the whole line) sort sorts the results, -u removes duplicates -- Mark Rogers // More Solutions Ltd (Peterborough Office) // 0844 251 1450 Registered in England (0456 0902) @ 13 Clarke Rd, Milton Keynes, MK1 1LG
You can test your regular expressions here: http://gskinner.com/RegExr/ (Flash based, sorry) Tim.
Thanks everyone so much for your swift responses. Perhaps one day I will be a regex guru too... Richard
On 13/07/10 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>
I don't want the whole line, just the word.
You can even do it with just grep: mak@yoda:~$ (echo hi; echo 'now $food fight $win'; echo '$$$'; echo there) | grep -E -o '\$\w+' $food $win Cheers, -- Martijn
participants (5)
-
Brett Parker -
Mark Rogers -
Martijn Koster -
Richard Parsons -
Tim Green