3

I have a .csv file with some data, for example:

a | b | c | d | e
f | g | h | i | f

I would like to insert a header in this file to indicate each field:

h1 | h2 | h3 | h4 | h5
a | b | c | d | e
f | g | h | i | l

I would like to do it from the Windows command line, how can I do?

2 Answers 2

3

First, write the header to a new text file (escape the pipe symbol | using ^)

echo h1 ^| h2 ^| h3 ^| h4 ^| h5 > combined.csv

Then append the original file to this newly created file:

type originalfile.csv >> combined.csv

At last, you overwrite the original file with the content of the new file (including the header line)

type combined.csv > originalfile.csv
--or--
move /Y combined.csv originalfile.csv

Based on these answers: using batch echo with special characters and Easiest way to add a text to the beginning of another text file in Command Line (Windows)

Sign up to request clarification or add additional context in comments.

Comments

1

You can use Miller and run

mlr --csv --implicit-csv-header --fs "|" label h1,h2,h3,h4,h5 input.csv

to have

h1|h2|h3|h4|h5
a|b|c|d|e
f|g|h|i|f
  • --csv to set the format
  • --implicit-csv-header to declare that the input has no heading
  • --fs "|" to set the field separator
  • label h1,h2,h3,h4,h5 to set the heading

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.