fork download
  1. #!/bin/bash
  2.  
  3. # ideone boilerplate: run in temp dir
  4. t=$(mktemp -d -t ideone.XXXXXXXX) || exit
  5. trap 'rm -rf "$t"' ERR EXIT
  6. cd "$t"
  7.  
  8. mkdir -p myfiles/all1 myfiles/all2 myfiles/all3
  9. populate () {
  10. printf '%s\n' "$@" | gzip
  11. }
  12. populate 'Avast! Ahoy! Alas!' \
  13. 'Shiver me whiskers' >myfiles/all1/input.gz
  14. populate 'One' 'Two' 'Alas!' \
  15. 'Four' >myfiles/all2/input.gz
  16. populate 'Move along' \
  17. 'Nothing to see here' >myfiles/all3/input.gz
  18.  
  19. cat <<\____ >prog.py
  20. import gzip
  21. import glob
  22.  
  23. with open('file1.txt', 'w') as o:
  24. for file in glob.glob('myfiles/all*/input.gz'):
  25. with gzip.open(file, 'rt') as f:
  26. for line in f:
  27. if 'Alas!' in line:
  28. print(line, file=o, end='')
  29. ____
  30.  
  31. python3 ./prog.py
  32.  
  33. tail file1.txt
Success #stdin #stdout 0.02s 9808KB
stdin
Standard input is empty
stdout
Alas!
Avast! Ahoy! Alas!