Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: printing terminal stout to text file

  1. #1
    Join Date
    Oct 2018
    Beans
    178

    printing terminal stout to text file

    I've written some math quiz scripts for my grandkids, and started using ansi codes to output colored text in the terminal. The script creates a temp file and a permanent file, then copies tempt to perm file. I've included 1 script below. The problem I'm having, is that the file shows the ansi code. I've included a copy of 1 such file below. The ideal case would be to be able to print in color, but the main goal is to find a way for it not to show the ansi codes in the file. With the ansi codes included, it gets hard to read. I could edit the codes out in a text editor and then print, but this is a short script, and I've got 1 that is multiple levels and has a lot of stout, thus making the editing a long process. So the question is, is there an easy way to eliminate the ansi codes prior to saving the file.
    My script is:
    Code:
    #!/bin/bash
    
    #This script waas written for my grandchildre. It is a quiz with addition
    #problems. The user can set the number of problems, and maximum number size.
    #To use the script as written, you must install cowsay and lolcat, which can be
    #installed with 'sudo apt install cowsay' and 'sudo apt install lolcat'.
    #I have commented out the make file and copy functions because the file saves and
    #prints all the ansi codes and I haven't yet figured out a way to avoid that( any
    #help here would be appeciated).
    function_1 () {
    	x=$[RANDOM%$R1] y=$[RANDOM%$R2]
    	sum=$(($x + $y))
        good=$(shuf -n1 /home/charles/Scripts/Bash_in-progress/right_answer.txt)
        bad=$(shuf -n1 /home/charles/Scripts/Bash_in-progress/wrong_answer.txt)
    	echo  -e "${blue}What is $x + $y?${nc}" | cowsay
    		read answer
    	echo "Let's see here, you answered $answer. So I do this...caarry that..." | cowsay | lolcat
    	sleep 3
    	if [[ $answer = $sum ]] ;
    		then echo -e "${green}${good}${nc}" | cowsay
    		C=$(( $C + 1 ))
    		echo "Press enter to continue"
    read -r -s -p $"Press <ENTER> to continue....\n"
    clear
    	else
    		echo -e "${red}${bad}${nc}" | cowsay -f dragon
    		sleep 2
    		echo -e "${red}The correct answer is ${green}$sum.${nc}" | cowsay
    		I=$(( $I + 1 ))
    		echo "Press enter to continue"
    read -r -s -p $"Press <ENTER> to continue....\n"
    clear
    fi
    }
    
    
    green='\033[0;32m'
    red='\033[0;31m'
    blue='\033[0;94m'
    nc='\033[0m'
    now=$(date +"%m-%d-%y_%I:%M_%p")
    echo "This test was taken $now"
    I=0
    C=0
    
    
    echo -e "${blue}Hello, why don't you tell me your name.${nc}" | cowsay
    	read name
    	name=$name
    clear
    echo -e "${blue}Hello $name. My name is Alex and I'll be giving you your test today.${nc}" | cowsay
    
    
    #file=$name-$now
    #touch /home/charles/Tests/$file.txt
    
    
    
    
    echo -e "${blue}OK $name, how many problems would you like to do today?${nc}" | cowsay
    	read number
    	TP=$number
    clear
    echo -e "${blue}Very good, we will do ${TP} problems today.${nc}" | cowsay
    
    
    echo -e "${blue}What's the biggest you want your numbers to be?${nc}" | cowsay
    	read limit
    	R1=$limit
    	R2=$((R1+1))
    clear
    echo -e "${blue}OK, your numbers will not be larger than $limit.${nc}" | cowsay
    
    
    echo "OK, let's get started." | cowsay | lolcat
    
    
    echo "Press enter to continue"
    read -r -s -p $"Press <ENTER> to continue....\n"
    clear
    PC=1
    	while [[ $PC -le $TP ]]
    	do
    	PC=$(( $PC + 1))
    		function_1
    	done
    
    
    score=$(( ($C*1000/$TP+5)/10 ))
    
    
    echo "OK, let's see how you did today." | cowsay | lolcat
    sleep 1
    
    
    echo -e "${green}On todays quiz, you got $C problems correct, you got $I wrong.${nc}" | cowsay
    echo -e "${green}Your score today is $score.${nc}"
    
    
    #cp /home/charles/Tests/temp_file.txt /home/charles/Tests/$file.txt
    #sleep 3
    
    
    #echo "Would you like to print a copy of the test?"
    #	read answer
    #	answer=${answer^^}
    #	answer=${answer:0:1}
    #		if [[ ${answer} = 'Y' ]] ;
    #			then lp /home/charles/Tests/${file}.txt
    #			echo "It should be printing."
    #			echo "I'll see you next time."
    #			sleep 3
    #			exit
    #		elif [[ ${answer} = 'N' ]] ;
                then echo "OK. The file is saved and can be printed anytime."
    #fi
    		echo -e "${green}You did well today.${nc}" | cowsay
    		sleep 4
    		clear
    		echo -e "${green}If you didn't do as good as you wanted, don't worry about it. A little more pratice and you will be perfect next time.${nc}" | cowsay
    		echo "Press enter to continue"
    read -r -s -p $"Press <ENTER> to continue....\n"
    clear
    		echo -e "${green}After all that work, I'll ask the conductor to give you a ride home.${nc}" | cowsay
    		sleep 3
    		echo "Hey Mr. Conductor, can you give $name a ride home today?" | cowsay | lolcat
    		sleep 5
    		clear
    		echo "Of course I can, ALL ABOARD" |cowsay -f elephant | lolcat
    		sleep 5
    		clear
    echo -e "${green}OK,I'll see you next time.${nc}" | cowsay
    sleep 5
    sl
    clear
    exit
    And the saved file is:

    Code:
    This test was taken 08-07-21_12:09_PMHello, why don't you tell me your name.
    Hello Coley. My name is Alex and I'll be giving you your test today.
    OK Coley, how many problems would you like to do today?
    Very good, we will do 5 problems today.
    What's the biggest you want your numbers to be?
    OK, your numbers will not be larger than 10.
    OK, let's get started.
    What is 2 + 0?
    You answered 2.
    [0;32mThat is correct!! Great job.[0m
    What is 6 + 7?
    You answered 13.
    [0;32mThat is correct!! Great job.[0m
    What is 9 + 9?
    You answered 17.
    [0;31mSorry,that is not right. The correct answer is [0;32m18.[0m
    What is 5 + 4?
    You answered 9.
    [0;32mThat is correct!! Great job.[0m
    What is 8 + 1?
    You answered 9.
    [0;32mThat is correct!! Great job.[0m
    OK, let's see how you did today.
    On todays quiz, you got 4 problems correct, you got 1 wrong.
    Your score today is 80.
    Would you like to print a copy of the test?
    request id is TS3300USB-21 (1 file(s))
    It should be printing.
    I'll see you next time.
    The script responds different color based on it is comments and questions, correct answer, or incorrect answer. The idea is to be able to print the test and use it as a study guide. Any tips would be great, aand as always, thanks in advance for the help.
    Last edited by deadflowr; November 24th, 2021 at 06:32 PM. Reason: code tags
    I'm a firm believer that the only stupid question is the one you don't ask.
    Lenovo ideapad320-15iap, 1.1G Intel processor with onboard graphics, x64, 1TB SSD, 8GB ram
    lubuntu 22.04.1, fully updated

  2. #2
    Join Date
    Oct 2018
    Beans
    178

    Re: printing terminal stout to text file

    Sorry about that. I made the post with code tags, and checked a preview and lost the original. I copied the post and pasted in the preview page and it did awaay with the code tags. Next time I won't preview post.
    I'm a firm believer that the only stupid question is the one you don't ask.
    Lenovo ideapad320-15iap, 1.1G Intel processor with onboard graphics, x64, 1TB SSD, 8GB ram
    lubuntu 22.04.1, fully updated

  3. #3
    Join Date
    Aug 2021
    Beans
    194
    Distro
    Xubuntu

    Re: printing terminal stout to text file

    You can edit your post. Try using http://pastebin.com

  4. #4
    Join Date
    Apr 2011
    Location
    Mystletainn Kick!
    Beans
    13,611
    Distro
    Ubuntu

    Re: printing terminal stout to text file

    I made the post with code tags, and checked a preview and lost the original. I copied the post and pasted in the preview page and it did awaay with the code tags.
    Fixed it for you.
    Splat Double Splat Triple Splat
    Earn Your Keep
    Don't mind me, I'm only passing through.
    Once in a blue moon, I'm actually helpful
    .

  5. #5
    Join Date
    Oct 2018
    Beans
    178

    Re: printing terminal stout to text file

    Thank you @deadflowr, I won't be using the preview option anymore...lol. Live and learn, it's the linux way.
    I'm a firm believer that the only stupid question is the one you don't ask.
    Lenovo ideapad320-15iap, 1.1G Intel processor with onboard graphics, x64, 1TB SSD, 8GB ram
    lubuntu 22.04.1, fully updated

  6. #6
    Join Date
    Dec 2014
    Beans
    2,573

    Re: printing terminal stout to text file

    Once you have the file with all the embedded terminal control codes in it you can use the streaming editor sed to remove them.
    Code:
    sed 's/\d27\[0;32m//g;s/\d27\[0;31m//g;s/\d27\[0;94m//g;s/\d27\[0m//g' /home/charles/Test/${file}.txt|lp -
    This may look rather daunting, but is actually quite simple: each 's/something//g' is a substitute command for sed, telling it to search for 'something' and substitute nothing for it. The 'something' sed searches for is a regular expression, and since '[' has a special meaning in regular expressions it must be escaped by prefixing it with a '\'. The '\d27' is the way to write the escape character. The 'g' at the end of each command is there in case there are multiples of one item that need replacing in one line (sed works on a line by line basis; without the 'g' it would only remove one occurrence of each control sequence per line). sed sends the results of it's work to standard output, so we pipe that into 'lp' and give '-' as a parameter to tell it to print it's standard input.
    In theory you could also use something like '\d27[^m]*m' as the term to replace (all sequences begin with escape and end with an 'm' with characters that aren't 'm' in between) and have only
    Code:
    sed 's/\d27[^m]*m//g' /home/charles/Test/${file}.txt|lp -
    but I'm not completely certain that this will always do the right thing; there might be sequences that don't end with an 'm' - in which case sed would remove to much - or there might be sequences which do have an 'm' before the end - in which case sed would remove to little.

    Holger
    Last edited by Holger_Gehrke; November 24th, 2021 at 10:44 PM. Reason: used \d27 and added the shorter regexp

  7. #7
    Join Date
    Nov 2007
    Location
    London, England
    Beans
    7,699

    Re: printing terminal stout to text file

    Holger_Gehrke has much the same approach than I would take, although I'm not sure while he's sending it straight to the printer.
    He gives a nice description of the sed operation, but for some reason I feel compelled to search for ESC followed by [ rather than just ESC as the trigger. In your case that won't make any difference, but it just feels more right to me.
    Instead of copying the temp file to the permanent file, I would pass it through sed like this, using sed to strip the ansi code and write the output to the permanent file:
    Code:
    sed 's/\d27\[[^m]*m//g' ~/Tests/temp_file.txt > ~/Tests/$file.txt

  8. #8
    Join Date
    Oct 2018
    Beans
    178

    Re: printing terminal stout to text file

    Great tips. I've been putting learning sed for a while, seems the time has come to study it a little more. What i'm going too do is use sed commands as suggested and redirect that to the permanent file, as my printing is done from there it will work quite well for my purposes. I will have to rewrite the script for a printable version(keeping this 1 because it's cool), because I've add a 'press enter to continue',cowsay, lolcat and sl, which all produce unwanted stuff in the saved file. I will take the suggestions as a kick in the rear to learn 'sed' a little better. Thanks for the help.
    I'm a firm believer that the only stupid question is the one you don't ask.
    Lenovo ideapad320-15iap, 1.1G Intel processor with onboard graphics, x64, 1TB SSD, 8GB ram
    lubuntu 22.04.1, fully updated

  9. #9
    Join Date
    Dec 2014
    Beans
    2,573

    Re: printing terminal stout to text file

    Why not simply use the script as is and pipe the commands whose output you want in the file through 'tee' to send them both to the screen and to the file ? The option '--append' of tee does just what the name suggests.

    Also: I see you're reading the files with good and bad messages every time you ask a question. Why not read all the messages into an array using mapfile (a bash builtin; also known as readarray) once during initialization and then select a random one ?

    Holger

  10. #10
    Join Date
    Oct 2018
    Beans
    178

    Re: printing terminal stout to text file

    OK. I tried to reply but the reply was to long, so I'm going to try pastbin, never tied before so I hope this works.

    Thanks for the tips Holger. As for your first idea. I've made an alias for this script, alias add_test="cd /home/charles/Tests/scripts && ./add-with-file.sh | tee /home/charles/Tests/temp_file.txt". This saves me a lot of typing. By running this I pipe the script to the tee command, so similar to you suggestion. If your saying I can do this on an as-wanted bases from within the script, please point me in the direction of some further reading. I was looking for a way to do that originally.
    As to your second idea. As I was searching for a way to use random lines from an external text file, I did see references to using an array, randomizing the array, then use the lines 1 by 1 until all are used, eliminating back to back same answers. After all the lines are used, I would have to re-randomize the lines and begin using them again. I do get times when it will reply the same on 2 or 3 problems in a row, and using an array should solve that problem. If you could point toward further reading on that it would be great.
    I'm using the command that The Cog suggested, sed 's/\d27\[[^m]*m//g' ~/Tests/temp_file.txt > ~/Tests/$file.txt,r and it works fine for removing the ansi codes, But at every instance of the clear command, I still get strange stuff. I'm going to rewrite the script to a more print-friendly version, at this point it's more of a learn to do this thing. Now for the stuff that's new to me. I think I have the files, 1 as output from the tee command, before using sed, and 1 as saved after running sed. I edited the after version with[[ I want to remove this line]] to make it easier to spot the strange stuff I still getting. Also, you'll notice that with the ASCII art in place, the file would make for a rather large print job, which is what lead to the decision to rewrite the script.
    The raw file, before editing with sed:
    http://pastebin.com/6GXNTGaG
    And after using sed: http://pastebin.com/S778ad4F
    Hopefully I did that right and you can see the files. As always, thanks for all the help.
    Last edited by coley9225; November 27th, 2021 at 07:16 PM. Reason: Had to edit the pastebin for 1 of the links, my typing isn't great sometimes.
    I'm a firm believer that the only stupid question is the one you don't ask.
    Lenovo ideapad320-15iap, 1.1G Intel processor with onboard graphics, x64, 1TB SSD, 8GB ram
    lubuntu 22.04.1, fully updated

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •