Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

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.

share|improve this answer
    
Not working for me. The awk command outputs "20007" for each line. –  frankadelic Aug 9 '12 at 22:10
    
This works.... alias csl='awk '\''{print "\047"$1"\047"}'\'' | paste -d, -s' –  frankadelic Aug 9 '12 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. –  glenn jackman Aug 10 '12 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. –  glenn jackman Aug 10 '12 at 0:37
add comment
up vote 0 down vote accepted

This worked for me:

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

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.