#!/bin/bash# Author: Massoud Seifi, Ph.D. @ MetaDataScience.com# Count the number of lines in the input filelines=$(wc -l < "$1")# Generate unique ids and store them in a temporary fileuids=$(tempfile)for i in `seq 1 $lines`; do uuidgen -t; done > "$uids"# Insert the unique ids at the end of each line of the input filepaste --delimiter ",""$1""$uids"# Remove the temporary filerm "$uids"
For example, running the script on the sample CSV file below:
If first line of your CSV file is the column headings, you can use code below:
Add unique ids to a CSV file with headers (gen_uid_header.sh)download
123456789101112131415
#!/bin/bash# Author: Massoud Seifi, Ph.D. @ MetaDataScience.com# Count the number of lines in the input filelines=$(wc -l < "$1")# Generate unique ids and store them in a temporary fileuids=$(tempfile)for i in `seq 1 $lines`; do if[$i== 1 ]; then echo"uid"; else uuidgen -t; fi; done > "$uids"# Insert the unique ids at the end of each line of the input filepaste --delimiter ",""$1""$uids"# Remove the temporary filerm "$uids"
Sample CSV file with headers (test_header.csv)download