You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 $word variable without trimming whitespace or misinterpreting backslashes (important if your words have special characters).
  • The < /path/to/input_words.txt at 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:

  1. Open a text editor, paste the code above, and replace the placeholder paths with your actual file locations.
  2. Save the file as process_words.sh.
  3. Make it executable:
    chmod +x process_words.sh
    
  4. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:39:08