From: Sashi on
Hi all, I have a bunch of *.csv files. These are comma separated
values. I'd like to replace the first field with the file name, minus
the .csv extension.
For example, my_file.csv has the following fields:
1,2,3,4
5,6,7,8

I'd like to edit the file to be
my_file, 2, 3,4
my_file, 6,7,8

Any ideas?
Thanks,
Sashi
From: pk on
Sashi wrote:

> Hi all, I have a bunch of *.csv files. These are comma separated
> values. I'd like to replace the first field with the file name, minus
> the .csv extension.
> For example, my_file.csv has the following fields:
> 1,2,3,4
> 5,6,7,8
>
> I'd like to edit the file to be
> my_file, 2, 3,4
> my_file, 6,7,8
>
> Any ideas?
> Thanks,
> Sashi

awk -F, -v OFS=, 'FILENAME!=p{if(p"")close(p);p=FILENAME}
{$1=p;print > (p".new")}' *.csv

then you just need to rename *.new to *.csv.

You can also use a loop but that's likely to be less efficient as you'll
need to run one process per file.

From: Sashi on
On Nov 25, 2:34 pm, pk <p...(a)pk.invalid> wrote:
> Sashi wrote:
> > Hi all, I have a bunch of *.csv files. These are comma separated
> > values. I'd like to replace the first field with the file name, minus
> > the .csv extension.
> > For example, my_file.csv has the following fields:
> > 1,2,3,4
> > 5,6,7,8
>
> > I'd like to edit the file to be
> > my_file, 2, 3,4
> > my_file, 6,7,8
>
> > Any ideas?
> > Thanks,
> > Sashi
>
> awk -F, -v OFS=, 'FILENAME!=p{if(p"")close(p);p=FILENAME}
>                   {$1=p;print > (p".new")}' *.csv
>
> then you just need to rename *.new to *.csv.
>
> You can also use a loop but that's likely to be less efficient as you'll
> need to run one process per file.

Thank you.
I made a small modification to suit my needs:
awk -F, -v OFS=, 'FILENAME!=p{if(p"")close(p);p=FILENAME}
{$1=substr(p,1,length(p)-4);print > (p".new")}'
*.csv
Sashi
From: pk on
Sashi wrote:

> On Nov 25, 2:34 pm, pk <p...(a)pk.invalid> wrote:
>> Sashi wrote:
>> > Hi all, I have a bunch of *.csv files. These are comma separated
>> > values. I'd like to replace the first field with the file name, minus
>> > the .csv extension.
>> > For example, my_file.csv has the following fields:
>> > 1,2,3,4
>> > 5,6,7,8
>>
>> > I'd like to edit the file to be
>> > my_file, 2, 3,4
>> > my_file, 6,7,8
>>
>> > Any ideas?
>> > Thanks,
>> > Sashi
>>
>> awk -F, -v OFS=, 'FILENAME!=p{if(p"")close(p);p=FILENAME}
>> {$1=p;print > (p".new")}' *.csv
>>
>> then you just need to rename *.new to *.csv.
>>
>> You can also use a loop but that's likely to be less efficient as you'll
>> need to run one process per file.
>
> Thank you.
> I made a small modification to suit my needs:
> awk -F, -v OFS=, 'FILENAME!=p{if(p"")close(p);p=FILENAME}
> {$1=substr(p,1,length(p)-4);print > (p".new")}'
> *.csv

Oh right, you wanted it without .csv. Thanks for fixing that.
From: Ed Morton on
On Nov 25, 1:25 pm, Sashi <small...(a)gmail.com> wrote:
> Hi all, I have a bunch of *.csv files. These are comma separated
> values. I'd like to replace the first field with the file name, minus
> the .csv extension.
> For example, my_file.csv has the following fields:
> 1,2,3,4
> 5,6,7,8
>
> I'd like to edit the file to be
> my_file, 2, 3,4
> my_file, 6,7,8
>
> Any ideas?
> Thanks,
> Sashi

for file in *.csv
do
sed "s/[^,]*/${file%.csv}/" "$file" > tmp &&
mv tmp "$file"
done

Regards,

Ed.