2

I am always typing things like this:

cat some_list_of_elements.txt | awk '{print "\047"$1"\047"}' | paste -d, -s

It takes a list of items from a file, wraps each in quotes, and merges them together in a comma-separated list.

Rather than typing out all that stuff, I'd like to do something like this:

cat some_list_of_elements.txt | csl

How can csl be implemented in bash? Is there a way to do it as an alias?

2 Answers 2

3
alias csl="awk '{print "\047"$1"\047"}' | paste -d, -s"

then just cat whatever | csl

This seems to work, but really doesn't, for the reasons mentioned in the comments below. See them for actual, good working solutions :)

By the way, this is not ubuntu-specific, so feel free to post on stackoverflow.com if my answer doesn't work for you.

4
  • Not working for me. The awk command outputs "20007" for each line. Aug 9, 2012 at 22:10
  • This works.... alias csl='awk '\''{print "\047"$1"\047"}'\'' | paste -d, -s' Aug 9, 2012 at 22:15
  • 1
    @frankadelic, you could also use a function: csl() { awk '{print "\047" $1 "\047"}' - | paste -d, -s; } -- don't have to mess around with a lot of quotes and backslashes. I explicitly put the argument - for awk to denote that it's reading from stdin. Aug 10, 2012 at 0:36
  • @roadmr, the difficulty is you're using double quotes as the "outer" quotes, so the $1 is getting substituted at the time you're defining the alias. Aug 10, 2012 at 0:37
0

This worked for me:

alias csl='awk '\''{print "\047"$1"\047"}'\'' | paste -d, -s'

Thanks to @roadmr for pointing me in the right direction.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .