mi7c514
mi7c514

Reputation: 23

add column with incrementing integer to multiple csv files

I have a directory with thousands of csv files, each with 64 columns and 700+ lines. I would like to combine for importing into database tables.

Using cat, combining the files is no problem. However, each file represents a separate event, so when querying the database I would like to be able to extract just the lines from an individual file.

Is there a way to add an incrementing integer to each file before combing them?

for example:

log_file1

log_file2

to

log_file1

log_file2

Thanks

Upvotes: 1

Views: 115

Answers (2)

Ed Morton
Ed Morton

Reputation: 203635

Using GNU awk:

awk '{print ARGIND "," $0}' *.csv

Upvotes: 2

glenn jackman
glenn jackman

Reputation: 246827

With awk, you could write:

awk 'FNR==1 {count++} {print count "," $0}' *.csv

That increments the "count" variable when the first record of each file is being processed.

Upvotes: 1

Related Questions