From: Dave Gibson on
Sashi <smalladi(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

for f in *.csv ; do
printf ',s\a^[^,]*,\a%s,\a\nw\nq\n' "${f%.csv}" | ed -s "$f"
done
From: John W. Krahn 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

perl -i.bak -pe's/^[^,]+/substr$ARGV,0,rindex$ARGV,"."/e' my_file.csv



John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway
From: pk on
pk wrote:

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

This is wrong. In addition to not removing the .csv at the end as you
already noted, it also does the close() wrong. So it should be:

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

From: Rakesh Sharma on
On Nov 26, 12:25 am, 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


perl -F, -i.bak -MFile::basename -lpe '$_ = join",",basename($ARGV),@F
[1..$#F]' myfile1.csv myfile2 .csv ........
From: John W. Krahn on
Rakesh Sharma wrote:
> On Nov 26, 12:25 am, 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
>
>
> perl -F, -i.bak -MFile::basename -lpe '$_ = join",",basename($ARGV),@F
> [1..$#F]' myfile1.csv myfile2 .csv ........

-MFile::basename should be -MFile::Basename, but basename() does not
remove the file name extension.

Without the -a option the @F array will be empty.



John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway