bashcut

Using cut command with criterion from a bash variable


I would like to use the cut command to extract some columns of a csv file. However, instead of having my criterion hard-coded, I would like to take them from a bash variable.

Without a variable, my command would look like the following:

cut -d';' -f 1-8,20,22 "$input_file" > ANFR_reduced.csv

However, when I store the critera -f 1-8,20,22 within a bash variable, my cut command fails to execute correctly (error fields are numbered from 1)

criteria_col="-f 1-8,20,22"
cut -d';' "$criteria_col" "$input_file" > ANFR_reduced.csv

Any ideas to why this is not working, please?


Solution

  • If you enable debug mode (set -xv) you'll see what bash sees:

    $ touch some_file
    $ criteria_col="-f 1-8,20,22"
    
    $ set -xv
    
    $ cut -d';' "$criteria_col"  some_file
    + cut '-d;' '-f 1-8,20,22' some_file
                ^^^^^^^^^^^^^^
    cut: fields are numbered from 1
    Try 'cut --help' for more information.
    

    Notice that -f 1-8,20,22 is being treated as a single string '-f 1-8,20,22' which in turn causes the error.

    One simple fix would be to move the -f from the variable to the command line, eg:

    $ criteria_col="1-8,20,22"
    
    $ cut -d';' -f "$criteria_col"  some_file
                ^^
    + cut '-d;' -f 1-8,20,22 some_file
    $                                    # no error; no output since my file is empty
    

    If you need the -f to be part of the criteria then I'd recommend F.Hauri's comment/suggestion of using an array.