Continuously write terminal output to file?

8,787

Solution 1

you can save the terminal output of the script : if the script print the number every time it will be saved if you redirect the output to a file

script > file.txt

if you want to append .

script >> file.txt

then to show only 5 results from the center of file.txt y

len=$(wc -l < file.txt);
#to store the length of file

len=$((($len/2)-3));  
#to get the middle of file

tail -n $len file.txt | head -n 5 ; 
#show 5 lines from midlle of file

Solution 2

There are multiple steps for that. First you need to record your bash output in a text file. You can use tee for that. (man page) Lets call your script "myscript".

Pipe your your input to

tee /path/to/myscript.txt

That writes the output of your bash input to /path/to/myscript.txt

So it will look something like

sh /path/to/myscript.sh | tee /path/to/myscript.txt

You can perform standard text processing stuff on /path/to/myscript.txt.

Not entirely sure what you mean by middle, since with the "median" it all depends on whether or not you have an even or odd number of lines. But you can use sed to print whatever lines you want.

sed -n 'x,yp' /path/to/myscript.txt

where x and y are the bounds of the interval of the lines you're printing.

Share:
8,787

Related videos on Youtube

froffen
Author by

froffen

Updated on September 18, 2022

Comments

  • froffen
    froffen over 1 year

    Given that I have a script which prints out a random number between 100-999 every 10th second, how can I gather this information into, for example, a .txt file? Is it also possible to only gather the number in the middle? Example: gather '5' out of '659'.

  • froffen
    froffen over 7 years
    Thanks guys! This seems to be working well with some of my scripts. But my main goal was to save output data from a curl. My curl is built as the following: curl -k --data "objectID=ID&username=username&password=passwd" url "Header: 4 " The output is in a html format where I need so save the information in inner 2 {].