Mohamed Ali JAMAOUI
Mohamed Ali JAMAOUI

Reputation: 14699

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

I loaded a csv file into a variable and then I am trying to cut out some columns which results in this error /usr/bin/cut: Argument list too long. Here is 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?

Upvotes: 3

Views: 1093

Answers (1)

anubhava
anubhava

Reputation: 786329

Reason of that error is your use of $input_file which has full file data.

You need to run cut on the file not on the file content so use:

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

To run cut against file content use:

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

Upvotes: 4

Related Questions