From: Sashi on 25 Nov 2009 14:25 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 25 Nov 2009 14:34 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 25 Nov 2009 14:53 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 25 Nov 2009 14:52 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 25 Nov 2009 15:27 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.
|
Next
|
Last
Pages: 1 2 3 Prev: Quote problem Next: Replace a particular string at a particular position |