#!/bin/bash # ideone boilerplate - we can't write files in the home directory; # so create a temporary directory for our files instead t=$(mktemp -d -t ideone.XXXXXXXXXXXX) || exit trap 'rm -rf "$t"' ERR EXIT cd "$t" cat <<\: >test_final.txt here are words in some sort of order 1234 hello123 here are more words : cat <<\: >demo.py import random import re # import sys # unused import # Changed the regex so that it can actually match words mama = r"[a-zA-Z]+\d*" # Python variables should prefer snake_case over headlessCamelCase list_of_words = [] # Specify encoding here, too with open("test_final.txt", "r", encoding="utf-8") as f: for line in f: for word in line.rstrip('\n').split(): # Does this line match the regular expression? if re.match(mama, word): list_of_words.append(word) else: # Print a warning when we are discarding a line print('# discarding', word) print("Result: ", list_of_words) random.shuffle(list_of_words) print("Shuffled stuff: ", list_of_words) finalString = " ".join(list_of_words) print(finalString) with open("test_final.txt", "w", encoding = 'utf-8') as file: file.write(finalString) : echo '** before' cat test_final.txt echo '** run' python3 demo.py echo '** after' cat test_final.txt
Standard input is empty
** before here are words in some sort of order 1234 hello123 here are more words ** run # discarding 1234 Result: ['here', 'are', 'words', 'in', 'some', 'sort', 'of', 'order', 'hello123', 'here', 'are', 'more', 'words'] Shuffled stuff: ['sort', 'more', 'here', 'order', 'in', 'of', 'here', 'words', 'are', 'are', 'hello123', 'words', 'some'] sort more here order in of here words are are hello123 words some ** after sort more here order in of here words are are hello123 words some