i have a different type of trivia question text file now and am unsure how to modify the script i have to place it in the correct format i need. this is the script im trying to modify
#!/bin/bash
FILENAME="trivia1.txt"
while read string
do
# Start of line parsing.
echo $string # Prints out the string.
i=`expr index "$string" :` # Obtain index of :
# Extracts a substring of position x and position x + 1, or the letter at position x
# Checks if equal to a colon.
answer=${string:0:$((i-1))} # Obtains everything before the colon
question=${string:$i} # Obtains everything after the colon
question=${question%?} # Remove the newline
echo "$answer"
echo "$question"
result=`echo $question*$answer`
echo $result
echo $result >> ./result.txt
done < $FILENAME # Reads the file.
echo ""
echo "File conversion has finished. Check result.txt in this directory."
echo "Press enter to exit"
read enter # Just a goodie, no need to remember this
this is the format of the txt file i need to modify
Category: Architectural terms
Question: A bell tower, usually not actually attached to a church.*
Answer: campanile
Category: Architectural terms
Question: A continuous aisle in a building, especially around the apse in a church.*
Answer: ambulatory
Category: Architectural terms
Question: A curved structure used to span an opening.*
Answer: arch
Category: Architectural terms
Question: A curved triangle at the corners of a square or polygonal room, used at the opening of a dome.*
Answer: pendentive
this is the format i need the them in
Category: Architectural terms : Question: A bell tower, usually not actually attached to a church.*campanile
Category: Architectural terms : Question: A continuous aisle in a building, especially around the apse in a church.*ambulatory
Category: Architectural terms : Question: A curved structure used to span an opening.*arch
Category: Architectural terms : Question: A curved triangle at the corners of a square or polygonal room, used at the opening of a dome.*pendentive
in the original text file it places an asterisk after all periods and question marks. i was wodnering if there was a way to just remove the spaces and backspace the line with Answer: so that its right after the question. that way i can just use search and replace in gedit to first remove all asterisks then replace Answer: with *
if you need any more info ask me please...and thanks in advance for any help offered
Trouble with script to modify txt file
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
Trouble with script to modify txt file
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Re: Trouble with script to modify txt file
Short script that does what you ask for:
I have no idea what you meant by replacing "Answer" with "*", because "Answer" isn't even in the output you desire? If you meant replacing "*" with "Answer", then change
into
Output:
Anakin
Code: Select all
#!/bin/bash
rm -f result.txt
while read i
do
if [[ ${i} =~ "Category" ]]
then
TEMP=${i}
elif [[ ${i} =~ "Question" ]]
then
TEMP=`echo ${TEMP} : "${i}"| sed s!\*!!`
elif [[ ${i} =~ "Answer" ]]
then
ANSWER=`echo ${i}| awk -F ":" '{ print $NF }'`
TEMP=`echo "${TEMP}""${ANSWER}"`
echo ${TEMP} | tee -a result.txt
fi
done < trivia1.txt
Code: Select all
sed s!\*!!
Code: Select all
sed 's!\*! Answer: !'
Regards,Script wrote:Category: Architectural terms : Question: A bell tower, usually not actually attached to a church. campanile
Category: Architectural terms : Question: A continuous aisle in a building, especially around the apse in a church. ambulatory
Category: Architectural terms : Question: A curved structure used to span an opening. arch
Category: Architectural terms : Question: A curved triangle at the corners of a square or polygonal room, used at the opening of a dome. pendentive
Anakin
Re: Trouble with script to modify txt file
ty anakin! and what i meant by replacing answer with * is because it would be easy to use find and replace in gedit to just replace the word answer with * rather than write it into the script because now if i i try to search and replace to add the * between the question and the answer i have nothing unique to search for to add the asterisk....but im sure i can fiure out how to add it in your script and i have trouble ill come back here and ask.
thank you very much
thank you very much