Reputation: 11
I am trying to go through a folder containing multiple CSV files and combine them into a single larger file. To do that I want to copy the info in the CSV along with its name (ie. foo from csvfiles/foo.csv) into a new CSV. How do I get the name of the CSV that I'm reading from as I read the whole set?
I am using
IFS=","
files=/CSVFiles/*
cat $files|while read col1 col2
do
echo $col1 $col2
done
I just need to get the name of the specific file that col1 and col2 are from. I tried putting the while loop inside a a for f in $files
but got an unspecified error. Any ideas?
Upvotes: 0
Views: 49
Reputation: 5221
You can use awk:
cd CSVFiles
awk -F , -vOFS=, '
FNR == 1 {
f = FILENAME
sub(/\.csv$/, "", f)
print f
}
{print $1,$2}' *.csv
You can use f
where you want, eg. like {print f": "$1,$2}
.
Upvotes: 1