Trouble with script to modify txt file

Forum rules
Before you post please read this

Trouble with script to modify txt file

Postby Bad_Dream on Sun Jun 24, 2012 2:38 pm

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
Bad_Dream
Level 1
Level 1
 
Posts: 7
Joined: Tue Jun 19, 2012 9:27 am

Linux Mint is funded by ads and donations.
 

Re: Trouble with script to modify txt file

Postby Anakinholland on Sun Jun 24, 2012 3:30 pm

Short script that does what you ask for:

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

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

Code: Select all
sed s!\*!!

into

Code: Select all
sed 's!\*! Answer: !'

Output:

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


Regards,

Anakin
Please Edit your initial post title and add [SOLVED] once your question is resolved.
User avatar
Anakinholland
Level 3
Level 3
 
Posts: 145
Joined: Sun Oct 30, 2011 8:09 am

Re: Trouble with script to modify txt file

Postby Bad_Dream on Sun Jun 24, 2012 4:45 pm

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 :D
Bad_Dream
Level 1
Level 1
 
Posts: 7
Joined: Tue Jun 19, 2012 9:27 am


Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 4 guests