Linux,  技术

Handy Bash Command

It always very handy to know a little bit bash to do file operation. E.G. generate SQL by csv.

Convert csv to a SQL:

Input

289104,4cca04f6-2b72-4e28-b3c2-052bad0d61bb,b40522d5-707e-4d8c-959b-b1f9f88cca1b,1e9a82bd-7b3b-4d38-99ef-fbfb558987ce
289948,4cca04f6-2b72-4e28-b3c2-052bad0d61bb,803cf363-7132-43ed-b213-7e127ecf7141,16a109ba-478c-4511-b8a3-e75126cd7f9a

Script

#!/bin/bash
input="/Users/lwang2/Downloads/changeUUID.csv"
while read -r line
do
	var_uuid=$(echo $line | cut -d "," -f 4)
	echo $var_uuid
	var_id=$(echo $line | cut -d "," -f 1)
	echo $var_id
	echo UPDATE STORE SET store_uuid='"'"$var_uuid"'"' WHERE id="$var_id"";" >> updateUUID.sql
done < "$input"

Output

UPDATE STORE SET store_uuid="1e9a82bd-7b3b-4d38-99ef-fbfb558987ce" WHERE id=289104;
UPDATE STORE SET store_uuid="16a109ba-478c-4511-b8a3-e75126cd7f9a" WHERE id=289948;

Notes:

  1. the the pipe result we need $(echo XX) to put the result to a variable.
  2. Split string and get one sub string by position.
  3. To output double quote in the output, we need the two single quote.
  4. Append a new file using >>.
  5. It seems there is but the last line doesn’t output.

Generate UUID and convert big case to small case

#!/bin/bash
input="/Users/lwang2/Downloads/POYNT.csv"
while read -r line
do
	uuid=$(uuidgen)
	echo "$line""$uuid" | awk '{print tolower($0)}' >> result.csv
done < "$input"

Leave a Reply

Your email address will not be published. Required fields are marked *