Sophie (
sophie) wrote in
command_liners2011-12-29 08:40 pm
Generating a comma-separated list from grep
Sometimes you want to paste the output of a
...you get:
Very useful sometimes :D
grep command into IRC or IM, and don't want each match on a separate line. Fortunately, it's easy to convert it to a comma-separated list instead - simply pipe the output through xargs echo | sed 's/ /, /g'. So, for example, instead of:Sophie@Sophie-Laptop:~/primtionary$ grep cuddle american-english-insane
cuddle
cuddleable
cuddled
cuddler
cuddlers
cuddles
cuddlesome
scuddle
scuddled
scuddles
upscuddle...you get:
Sophie@Sophie-Laptop:~/primtionary$ grep cuddle american-english-insane | xargs echo | sed 's/ /, /g'
cuddle, cuddleable, cuddled, cuddler, cuddlers, cuddles, cuddlesome, scuddle, scuddled, scuddles, upscuddleVery useful sometimes :D
no subject
function tocsl(){
export comma='';
while read word; do echo -n "$comma$word"; comma=', '; done;
echo;
}
(This doesn't really have to be a function, of course. A simple
{ list; }would do just as well.)cat, grep, sed and awk
cat, grep, sed and awk are three of the most powerful utilities in the *NIX toolbox.
Robert
/\__/\ (='.'=) (")_(")Kitty wishes you a safe and happy one, too. Too many pints for me, so I'd better be making my way back home.no subject
no subject
grep '^it.' /usr/share/dict/words | xargs echo
Or, for more fun, on a GNU system:
no subject
$ whatis rsrs (1) - reshape a data array
Invoked as "rs -C, 1" it will convert an input separated by newlines to a comma separated output:
$ grep '^oxym' /usr/share/dict/words | rs -C, 1oxymandelic,oxymel,oxymethylene,oxymoron,oxymuriate,oxymuriatic,
Plus you can undo the transformation too, if you pipe the comma separated input through "rs -c, 0 1" (lower case c).
The downside is that rs will leave you with a trailing comma, but as long as that's not an issue it's a pretty neat trick.
(no subject)