Print only one line at a time from text file

10,231

Solution 1

You can use sleep and clear commands in your script as following:

for word in $(< read)
do
    echo "$word"
    sleep 1
    clear
done

Explanation:

The sleep command make delay for a specified amount of time (in seconds). With sleep 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; Like sleep .1 for 1/10 second delay or sleep .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:

awk '{i=1; while(i<=NF){ print $((i++)); system("sleep 1; clear") }}' read

Explanation:

In awk, the NF 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 the system("sleep 1; clear") part, we are telling to awk 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:

IFS=$'\n'
for word in $(< read)
do
    echo "$word"
    sleep 1
    clear
done

And change the awk command like:

awk '{ $0; system("sleep 1; clear") }1' read
  • $0 specifies the current line. and the 1 on end enables the default awk's print command.

Solution 2

With bash, I'd do:

while IFS=$'\n' read -r line
do
    printf "%-${COLUMNS}s\r" "$line"
    sleep 1
done < file

By using the carriage return (\r) instead of line feed (\n), you can overwrite the current line. The -${COLUMNS} pads the output with spaces so that previous lines are completely overwritten.

For a per-word solution, I think a double loop is needed:

while read -ra line
do 
    for word in "${line[@]}"
    do
        printf "%-${COLUMNS}s\r" "$word"
        sleep 1
    done
done < file
Share:
10,231

Related videos on Youtube

Hassen Fatima
Author by

Hassen Fatima

Updated on September 18, 2022

Comments

  • Hassen Fatima
    Hassen Fatima over 1 year

    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!
    
  • αғsнιη
    αғsнιη about 9 years
    I thought OP's input file has more than one word in each line :/
  • muru
    muru about 9 years
    @KasiyA Hmmm. I assumed that OP meant IFS and not FS in the first line, in which case, it goes line by line.
  • αғsнιη
    αғsнιη about 9 years
    But your script will fails if the first word in fist line was long-length and second word in next line was small-length than previous shown line. run your script on this input as example
  • muru
    muru about 9 years
    @KasiyA fixed that.
  • Hassen Fatima
    Hassen Fatima about 9 years
    Thank you this solved my problem. Can you also tell me how do I display 120 or more words in one minutes!
  • αғsнιη
    αғsнιη about 9 years
    @HassenFatima decrease the second to low unit by sleep .1 (it means each word/line per 1/10 second) or sleep .001 (means each word/line in 1/1000 second) and lower by .00000000000...1