sophie: A cartoon-like representation of a girl standing on a hill, with brown hair, blue eyes, a flowery top, and blue skirt. ☀ (Default)
[personal profile] sophie posting in [community profile] command_liners
Sometimes you want to paste the output of a 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, upscuddle


Very useful sometimes :D

Date: 2011-12-29 11:52 pm (UTC)
doldonius: (Default)
From: [personal profile] doldonius
You could also make it right in the shell.

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

Date: 2011-12-30 01:11 am (UTC)
From: [personal profile] babysprite

cat, grep, sed and awk are three of the most powerful utilities in the *NIX toolbox.

... wishing you a safe and happy Hogamany and New Year,

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.
Edited Date: 2011-12-30 01:13 am (UTC)

Date: 2011-12-30 04:33 am (UTC)
afuna: Cat under a blanket. Text: "Cats are just little people with Fur and Fangs" (Default)
From: [personal profile] afuna
Neat, thanks!

Date: 2011-12-30 05:35 am (UTC)
jld: Batman says, “An *Amazon* attack, a deadly *bee* weapon... Bees.  My God.” (bee weapon)
From: [personal profile] jld
That xargs makes me twitchy...

grep '^it.' /usr/share/dict/words | xargs echo


Or, for more fun, on a GNU system:

xargs echo << EOT
-e
\\\\e[40;33;1m
BEES
BEES
BEES
EOT

Date: 2011-12-30 10:27 am (UTC)
karmag: Stylized face based on Dreamwidth logo (Default)
From: [personal profile] karmag
I don't know whether it's something BSD-specific or not, but rs can do this pretty elegantly too.

$ whatis rs
rs (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, 1
oxymandelic,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.

Date: 2012-01-01 09:58 am (UTC)
doldonius: (Default)
From: [personal profile] doldonius
I don't know whether it's something BSD-specific or not

I'm afraid it is. Even Debian lacks it.

Profile

command_liners: A command line prompt with a blinking cursor after it, green against black. (Default)
Command Liners

September 2014

S M T W T F S
 123456
78910111213
1415 1617181920
21222324252627
282930    

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 20th, 2017 10:04 pm
Powered by Dreamwidth Studios