Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

convert bash pipeline into a function with parameter

I have a pipeline I use to preview csv files:

cat file_name.csv | sed -e 's/,,/, ,/g' | column -t -s ","| less -s

But i want to create an alias viewcsv that will allow to just replace the filename.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried viewcsv="cat $1 | sed -e 's/,,/, ,/g' | column -t -s ","| less -s" but that didn’t work. Googling turned up that I need to convert this pipeline to a function? How can i convert this to a function so that viewcsv file_name.csv will return same output as cat file_name.csv | sed -e 's/,,/, ,/g' | column -t -s ","| less -s does?

>Solution :

Function syntax looks like this:

viewcsv() {
    sed -e 's/,,/, ,/g' "$1" | column -t -s ","| less -s
}

Notice that I have replaced cat "$1" | sed with sed "$1".

csvkit has a CSV previewer, by the way:

$ csvlook <<< $'a,b,c\n10,20,30'
|  a |  b |  c |
| -- | -- | -- |
| 10 | 20 | 30 |
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading