I have a csv file which has multiple columns. Here is an example of what i'm stuck with. Below is sample input data:


I want to split the data based on unique values from column 1, and then further split the resultants based on unique value from column 7. Currently i'm trying to achieve this by first splitting the sheet based on column 1, then i'm reading the resultant csv files from the directory, storing it in an output txt file and then further taking the values from output txt file as variables and running an awk script to split the files further. But somehow the 2nd split is not happening as per expectation. Below is what i have done so far:

awk -F\, '{print > $7".csv"}' $dir/../testfile.csv

ls -R | grep "\.csv$" > testfile.txt

while IFS= read -r a \n b \n c; do
    awk -F\, '{print > $1".csv"}' $dir/$a
    awk -F\, '{print > $1".csv"}' $dir/$b
    awk -F\, '{print > $1".csv"}' $dir/$c
done < testfile.txt

Please help me identify the issue here. This has to be in shell.

1 Answer 1


As I think a bit, you finally need below output which this command does:

awk -F, '{print >$1"_"$7".csv"}' infile
  • Getting: awk: cannot open "04477C9A875B80.csv" for output (Too many open files)
    – pee2pee
    Commented Jul 20, 2019 at 16:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .