how can I print or display only word from a text file at a time.
When I run my script it should only display each word from the text file one by one, but only display that word.
This is what I have done, but it displays each word on separate line, but it shows all of them:
FS=$'\n'
for j in `cat read`
do
echo "$j"
done
I want the output to look something like this:
root@matrix:~> first word ---> this word disappear when second is displayed
root@matrix:~> second word ---> this disappear when third is displayed
root@matrix:~> third word ---> this disappear when fourth is displayed and continues like this to the end of the file!
Best Answer
You can use
sleep
andclear
commands in your script as following:Explanation:
The
sleep
command make delay for a specified amount of time (in seconds). Withsleep 1
delay would be for 1 second. You can change for more time delay by incrementing the second parameter or for delaying less than 1 second divide it to low units; Likesleep .1
for 1/10 second delay orsleep .001
for 1/1000 second delay and etc.The
clear
command clear the terminal screen.Even better you can do this through below
awk
command:Explanation:
In
awk
, theNF
specifies the total number of fields in the current input record/line, so by using a variable as a counter (i
) and looping over it, we are printing all of them from 1st position to the end of them (NF
). Then by using thesystem("sleep 1; clear")
part, we are telling toawk
to calling the system commands to sleeping for 1 second and clearing the screen.In above we are displaying the input file as word by word. If you are going to display it line by line add
IFS=$'\n'
in the script like:And change the
awk
command like:$0
specifies the current line. and the1
on end enables the defaultawk
's print command.