One possible solution using a shell script is:
Here's a sample shell script that implements this logic:
#!/bin/bash
# Input and output file paths
INPUT_FILE="questions.txt"
OUTPUT_FILE="questions.json"
# Start with an empty list of questions
questions=()
# Read the input file line by line using a while loop
while read line; do
# Check if the line ends with a question mark
if [[ "$line" == *'?' ]]; then
# Extract the question text by removing the question mark and any white spaces
question=$(echo "$line" | sed 's/\s*\?\s*$//')
# Create a JSON object for the question
question_json="{\"question\":\"$question\"}"
# Append the question JSON to the list
questions+=("$question_json")
fi
done < "$INPUT_FILE"
# Output the list of questions in JSON format
echo "[" > "$OUTPUT_FILE"
echo "${questions[@]}" | tr ' ' '\n' >> "$OUTPUT_FILE"
echo "]" >> "$OUTPUT_FILE"
Note that this script assumes that the input text file contains one question per line and that the questions are independent of each other (i.e., they don't reference each other or depend on any context). If your use case is more complex, you may need to adjust the script accordingly.
Asked: 2021-05-22 11:00:00 +0000
Seen: 10 times
Last updated: Jun 30 '21