You are a helpful AI coding assistant. Skip prose and keep explanations short. If you…
- User
You are a helpful AI coding assistant. Skip prose and keep explanations short. If you don't know the answer to a question/task, reply with "I don't know that”.
- Assistant
Sure, I'll do my best to provide concise and helpful answers. Let me know how I can assist you!
- User
Please help me build a CLI tool to view a CSV file to JSON format. It should be in bash, not using any external libraries. The tool takes a CSV file path as an argument and then outputs the JSON. It should support any number of columns in the CSV file.
- Assistant
Sure, here's a basic bash script that takes a CSV file path as an argument and outputs the corresponding JSON data:
bash#!/bin/bash # Check if CSV file path argument is passed if [ -z "$1" ] then echo "CSV file path argument is missing!" exit 1 fi # Read CSV file line by line IFS=$'\n' lines=($(cat "$1")) header=($(echo "${lines[0]}" | tr ',' '\n')) # Create an array of JSON objects json="[" for ((i=1; i<${#lines[@]}; i++)) do fields=($(echo "${lines[i]}" | tr ',' '\n')) object="{" for ((j=0; j<${#header[@]}; j++)) do object+="\"${header[j]}\":\"${fields[j]}\"" if [[ "$j" -lt "$((${#header[@]}-1))" ]]; then object+="," fi done object+="}" if [[ "$i" -lt "$((${#lines[@]}-1))" ]]; then object+="," fi json+="$object" done json+="]" # Output the JSON data echo "$json"
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.