创建一个Linux shell脚本程序,对文件中的数字和单词进行排序?

创建一个Linux shell脚本程序,对文件中的数字和单词进行排序?,linux,bash,shell,unix,Linux,Bash,Shell,Unix,我需要创建一个Linux shell脚本程序来读取文本文件(直到EOF),并执行以下操作: 确定每行是包含单词还是数字 记录找到的单词数 将找到的每个单词附加到文本文件中(例如wordsfile.txt) 将找到的每个数字追加到文本文件中(例如numbersfile.txt) 返回以下所有信息 我希望程序读取的文本文件如下所示: 123 apple 456 boy 789 2 WORDS, 3 NUMBERS 我希望输出如下: 123 apple 456 boy 789 2 WORDS

我需要创建一个Linux shell脚本程序来读取文本文件(直到EOF),并执行以下操作:

  • 确定每行是包含单词还是数字
  • 记录找到的单词数
  • 将找到的每个单词附加到文本文件中(例如wordsfile.txt)
  • 将找到的每个数字追加到文本文件中(例如numbersfile.txt)
  • 返回以下所有信息
我希望程序读取的文本文件如下所示:

123
apple
456
boy
789
2 WORDS, 3 NUMBERS
我希望输出如下:

123
apple
456
boy
789
2 WORDS, 3 NUMBERS
另外,我希望wordsfile.txt的内容如下:

apple
boy
123
456
789
以及要读取的numbersfile.txt:

apple
boy
123
456
789
这是我的代码:

#!bin/bash
wordcount=0
numbercount=0
while read line; dp
  for word in $line; do
    $wordcount = $wordcount + 1
    echo word >> /words/wordsfile.txt
  done
  for number in $line; do
    $numbercount = $numbercount + 1
    echo number >> /numbers/numbersfile.txt
  done
  echo $wordcount " WORDS, " $numberscount " NUMBERS"
done
这是我得到的输出:

./assignment6.sh < assignment6file.txt
./assignment6.sh: line 5: 0: command not found
./assignment6.sh: line 6: /words/wordfile.txt: No such file or directory
./assignment6.sh: line 9: 0: command not found
./assignment6.sh: line 10: /numbers/numbersfile.txt: No such file or directory
0  words,  0  numbers
./assignment6.sh: line 5: 0: command not found
./assignment6.sh: line 6: /words/wordfile.txt: No such file or directory
./assignment6.sh: line 9: 0: command not found
./assignment6.sh: line 10: /numbers/numbersfile.txt: No such file or directory
0  words,  0  numbers
./assignment6.sh: line 5: 0: command not found
./assignment6.sh: line 6: /words/wordfile.txt: No such file or directory
./assignment6.sh: line 9: 0: command not found
./assignment6.sh: line 10: /numbers/numbersfile.txt: No such file or directory
0  words,  0  numbers
./assignment6.sh: line 5: 0: command not found
./assignment6.sh: line 6: /words/wordfile.txt: No such file or directory
./assignment6.sh: line 9: 0: command not found
./assignment6.sh: line 10: /numbers/numbersfile.txt: No such file or directory
0  words,  0  numbers
./assignment6.sh: line 5: 0: command not found
./assignment6.sh: line 6: /words/wordfile.txt: No such file or directory
./assignment6.sh: line 9: 0: command not found
./assignment6.sh: line 10: /numbers/numbersfile.txt: No such file or directory
0  words,  0  numbers
/assignment6.sh

我不明白为什么我的代码不起作用。请提供帮助。

Linux有所有这些整洁的实用程序来匹配数字和单词,并对它们进行计数。不需要重新发明轮子

wf=wordsfile.txt
nf=numbersfile.txt
egrep -i '^[a-z]+$' $1 > $wf
egrep '^[0-9.]+$' $1 > $nf
W=`wc -l $wf`
N=`wc -l $nf`
echo $W WORDS, $N NUMBERS
另存为脚本,然后按如下方式运行:

./my-script file-to-read

Bash报告的第一个错误是
第5行:0:command not found
。这意味着它试图调用函数或程序
0
,但找不到它。您没有显式地调用
0
,但该行中的某些内容使Bash无论如何都要尝试。这通常是变量扩展。将第4行上的
dp
更改为
do
后,列出一系列警告;你应该去看看。(不幸的是,我找不到链接到专门检查代码的方法)。