0


I have thousands of CSV files with the approximately the following structure:

csv_20160601T004301Z:

name,x,y,
name1,23,66
name2,23,67


csv_20160601T005001Z:

name,x,y,
name1,23,66
name2,23,67

etc.

As you can see, the filename contains a timestamp. I'd like to add a new field to each of the csv files and populate the field with the filename, like so:

csv_20160601T004301Z:

name,x,y,timestamp
name1,23,66,csv_20160601T004301Z
name2,23,67,csv_20160601T004301Z


csv_20160601T005001Z:

name,x,y,timestamp,
name1,23,66,csv_20160601T005001Z
name2,23,67,csv_20160601T005001Z

etc.

Of course, the "csv_" prefix is not really needed, just wanted to illustrate the idea. Any thoughts on how to do this? Is this something that can be achieved with Notepad++ or similar? Or will this require a more sophisticated approach?


I have very, very little experience in coding/programming, so I may need the potential ideas explained in the simplest way possible.

edit: SOLVED. I found a solution that helped me. I used the following in cmd: for /f %a in ('dir /b .csv') do for /f "tokens=" %b in (%a) do echo %b,%a >> all.csv

Source: https://community.spiceworks.com/topic/322603-merge-csv-files-and-create-a-column-with-the-filename-from-the-original-file

closed as off-topic by PolyGeo Jun 5 '17 at 12:02

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    There does not seem to be a GIS component to this question. – PolyGeo Jun 5 '17 at 12:02
  • 3
    You could use any of a dozen languages to accomplish this task. Try one. – Vince Jun 5 '17 at 12:03
  • I added this question under GIS because it felt like the safest place to go, since I work with geographic information here. I don't mind the idea of moving this question under another, more general IT topic. – Stinkender Jun 5 '17 at 14:59
1

You can use a simple BASH script:

for i in csv_*      
do
  x=${i#csv_}
  awk '{print $0, '$x'}' $i
done

Not the answer you're looking for? Browse other questions tagged or ask your own question.