1

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'.

3 Answers 3

2

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
1

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.

2
  • 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 {]. Commented Nov 9, 2016 at 1:31
  • Yeah I assumed you meant shell scripting. You should specify the programming language in the title next time. :) Commented Nov 9, 2016 at 15:33
0

script | tee path/to/file.txt

script | cut -c 2

script | cut -c 2 | tee path/to/file.txt

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.