[LINUX] How to put a line number at the beginning of a CSV file

I wanted to put a line number at the beginning of a CSV file that has hundreds of lines. I was wondering if there was any good way, and I don't want to use Excel because unexpected things happen when I process CSV files with Excel. Would you like to write it in a shell? The method I found when I thought it was troublesome.

--Environment --Windows10 64bit version 1909 - nl (GNU coreutils) 8.31

How to put a line number at the beginning of a CSV file


$ nl -w1 -s, {Original CSV}.csv > {CSV with line numbers}.csv

The nl command displays the file with line numbers.

$ nl sample.csv
     1  "hoge",2020/05/31,"fuga"
     2  "kuma",2020/05/30,"usagi"
     3  "tako",2020/05/29,"inu"
     4  "ponsuke",2020/05/02,"numa"

Minimize the line number display width with the -W1 option.

#I got angry when I specified "0"
$ nl -w0 sample.csv
nl: invalid line number field width: ‘0’: Numerical result out of range

$ nl -w1 sample.csv
1       "hoge",2020/05/31,"fuga"
2       "kuma",2020/05/30,"usagi"
3       "tako",2020/05/29,"inu"
4       "ponsuke",2020/05/02,"numa"

The -s, option displays "," after the line number.

$ nl -w1 -s, sample.csv
1,"hoge",2020/05/31,"fuga"
2,"kuma",2020/05/30,"usagi"
3,"tako",2020/05/29,"inu"
4,"ponsuke",2020/05/02,"numa"

reference

[[Nl] Command-Output a text file with line numbers: Linux basic command Tips (98)[email protected]](https://www.atmarkit.co.jp/ait/articles/1703/30/news024 .html)

Recommended Posts

How to put a line number at the beginning of a CSV file
Various ways to read the last line of a csv file in Python
How to output the output result of the Linux man command to a file
How to calculate the volatility of a brand
How to read a CSV file with Python 2/3
[Python] How to put any number of standard inputs in a list
[Python] How to read a csv file (read_csv method of pandas module)
How to play a video while watching the number of frames (Mac)
How to put a lot of pipelines together and put them away at once
Find out how to divide a file with a certain number of lines evenly
How to count the number of elements in Django and output to a template
I want to add silence to the beginning of a wav file for 1 second
How to know the port number of the xinetd service
[GoLang] Set a space at the beginning of the comment
How to hit the document of Magic Function (Line Magic)
Output the output result of sklearn.metrics.classification_report as a CSV file
How to identify the element with the smallest number of characters in a Python list?
[Ansible] Example of playbook that adds a character string to the first line of the file
The world's most easy-to-understand explanation of how to make a LINE BOT (1) [Account preparation]
[Ubuntu] How to delete the entire contents of a directory
How to find the optimal number of clusters in k-means
How to put a hyperlink to "file: // hogehoge" with sphinx-> pdf
[python] Change the image file name to a serial number
How to run a Python file at a Windows 10 command prompt
How to shuffle a part of a Python list (at random.shuffle)
How to increase the number of machine learning dataset images
How to see the contents of the Jupyter notebook ipynb file
How to find the scaling factor of a biorthogonal wavelet
How to connect the contents of a list into a string
How to put a symbolic link
How to display a specified line of a file or command result on Linux (sed, awk)
How to create a config file
How to insert a specific process at the start and end of spider with scrapy
How to make an interactive LINE BOT 004 (answer the closing date of a listed company)
How to paste a CSV file into an Excel file using Pandas
How to determine the existence of a selenium element in Python
Conditional branch due to the existence of a shell script file
How to check the memory size of a variable in Python
How to make a command to read the configuration file with pyramid
How to specify a .py file to load at startup in IPython 0.13
How to check the memory size of a dictionary in Python
How to find the memory address of a Pandas dataframe value
How to get the vertex coordinates of a feature in ArcPy
How to extract the desired character string from a line 4 commands
[Python] How to store a csv file as one-dimensional array data
How to create a CSV dummy file containing Japanese using Faker
[NNabla] How to remove the middle tier of a pre-built network
How to write when you want to put a number after the group number to be replaced with a regular expression in re.sub of Python
[Python] How to convert db file to csv
How to check the version of Django
Shout Hello, Reiwa! At the beginning of Reiwa
What is the XX file at the root of a popular Python project?
[Python] A program that calculates the number of socks to be paired
How to find out the number of CPUs without using the sar command
How to specify a .ui file in the dialog / widget GUI in PySide
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[LPIC 101] How to specify the disk partition number in the GRUB configuration file
[NNabla] How to add a quantization layer to the middle layer of a trained model
How to create a wrapper that preserves the signature of the function to wrap
[Bash] Command to write a file line by line to a serial number file [One liner (?)]
I made a program to check the size of a file in Python