Linux下批量传参执行命令并将结果追加至单一文件
Solution to Automate Word Processing and Append Output
Let's break down how to solve both of your problems with a straightforward bash script:
1. Automatically Read Words from Your Input File
You can use a while read loop to iterate over each word in your .txt file—this works perfectly if each word is on its own line (the standard format for large word lists). Here's the core logic:
# Replace /path/to/your/input_words.txt with the actual path to your 1M word file while IFS= read -r word; do # Skip empty lines (in case your input file has any) if [ -n "$word" ]; then # Run your dumpindex command with the current word ./dumpindex /home/pankaj/indri-5.0/Query/indri_test_may.idx/ t "$word" >> /home/pankaj/output_file.txt fi done < /path/to/your/input_words.txt
Key Details:
IFS= read -r word: Safely reads each line into the$wordvariable without trimming whitespace or misinterpreting backslashes (important if your words have special characters).- The
< /path/to/input_words.txtat the end tells the loop to pull words directly from your input file instead of standard input.
If your word list uses spaces (instead of newlines) to separate words, adjust the loop to first convert spaces to newlines:
cat /path/to/your/input_words.txt | tr ' ' '\n' | while IFS= read -r word; do if [ -n "$word" ]; then ./dumpindex /home/pankaj/indri-5.0/Query/indri_test_may.idx/ t "$word" >> /home/pankaj/output_file.txt fi done
2. Append to Output Instead of Overwriting
The fix here is simple: swap the > operator with >>.
>creates a new file (or overwrites an existing one entirely).>>adds new content to the end of an existing file (or creates it if it doesn't exist yet).
How to Run the Script:
- Open a text editor, paste the code above, and replace the placeholder paths with your actual file locations.
- Save the file as
process_words.sh. - Make it executable:
chmod +x process_words.sh - Run it:
./process_words.sh
This will process all 1 million words one by one, appending each result to your output file without overwriting previous entries.
内容的提问来源于stack exchange,提问作者pankaj kashyap
相关产品推荐
相关产品推荐

