1

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

  • a, b, c, d,...
  • a, b, c, d,...
  • a, b, c, d,...

log_file2

  • a, b, c, d,...
  • a, b, c, d,...
  • a, b, c, d,...

to

log_file1

  • 1, a, b, c, d...
  • 1, a, b, c, d...
  • 1, a, b, c, d...

log_file2

  • 2, a, b, c, d...
  • 2, a, b, c, d...
  • 2, a, b, c, d...

Thanks

0

2 Answers 2

2

Using GNU awk:

awk '{print ARGIND "," $0}' *.csv
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.