2

I'm trying to write a script in bash that reads a file file1 and writes to another file file2 every 2 seconds, which is easy enough.

However, I want to write to file2 on either a line by line basis or using delimiters.

For example:

File1 = Some text goes in here

I want to write to file2 every 2 seconds the first word Some, but only once, after 2 seconds the second word and so on until the end of file1.

2

I'd convert the delimiter to new lines to achieve "line by line basis or delimiters", something like:

Script:

echo -e "foo:bar\nbaz" > myfile
i=0
while read -r foo; do 
    echo $i $foo
    i=$((i += 1))
done < <(cat myfile | tr ':' '\n')

Output:

0 foo
1 bar
2 baz
1

One python script example from where you can start to write the script you want, reading the words from the first file and printing to stdout, but you can open another file and write there

#!/usr/bin/python

import time

File = open("words", "r")
Wordlist = File.readline().split()

n = 0

while True:
    time.sleep(2)
    print "File2 = " + Wordlist[n] #you can change this
    n = n + 1
    if n == Wordlist.__len__():
            break
1
  • Yeah, ok +1 for you on this. I was so caught up in doing it in bash I forgot there are easier ways. Thank you. – user2994883 Jul 10 '14 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy