Linux -/usr/bin/cut : Argument list too long in bash script

/usr/bin/cut : Argument list too long in bash script… here is a solution to the problem.

/usr/bin/cut : Argument list too long in bash script

I

load a csv file into a variable, and then I try to remove some of the columns that are causing this error /usr/bin/cut: Argument list too long. Here’s what I did :

if [ $# -ne 1 ]; then 
    echo "you need to add the file name as argument"
fi 

echo "input file name $1"
input_file=$(<$1)

#cut the required columns. 
cut_input_file=$(cut -f 1,2,3,5,8,9,10 -d \| $input_file)

echo $(head $cut_input_file)

What am I missing?

Solution

The cause of this error is that you used $input_file with full file data.

You need to run cut on the file and not the file contents, so use:

cut -f 1,2,3,5,8,9,10 -d '|' "$1"

To run cut: against the contents of the file

cut -f 1,2,3,5,8,9,10 -d '|' <<< "$input_file"

Related Problems and Solutions