Home > Mobile >  convert bash pipeline into a function with parameter
convert bash pipeline into a function with parameter

Time:11-26

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.

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?

CodePudding user response:

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 |
  •  Tags:  
  • bash
  • Related