Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 使用awk从文件中创建单词索引_Shell_Unix_Awk_Gsub - Fatal编程技术网

Shell 使用awk从文件中创建单词索引

Shell 使用awk从文件中创建单词索引,shell,unix,awk,gsub,Shell,Unix,Awk,Gsub,我正在学习UNIX for school,我应该创建一个命令行,该命令行接受一个文本文件,并生成一个字典索引,显示单词(不包括冠词和介词)及其在文件中出现的行 我在中发现了一个与我类似的问题:问题是当我运行解决方案时 $ awk ' { gsub(/[^[:alpha:] ]/,""); for(i=1;i<=NF;i++) { a[$i] = a[$i] ? a[$i]", "FNR : FNR; } } END { for (i in a) {

我正在学习UNIX for school,我应该创建一个命令行,该命令行接受一个文本文件,并生成一个字典索引,显示单词(不包括冠词和介词)及其在文件中出现的行

我在中发现了一个与我类似的问题:问题是当我运行解决方案时

$ awk '
{
  gsub(/[^[:alpha:] ]/,"");
  for(i=1;i<=NF;i++) {
      a[$i] = a[$i] ? a[$i]", "FNR : FNR;
  }
}
END {
      for (i in a) {
          print i": "a[i];
      }
}' file | sort
如何删除所有特殊字符并排除冠词和介词

$echo这就是这个测试一些测试文本
$ echo This is this test. |                    # some test text
awk '
BEGIN{
    x["a"];x["an"];x["the"];x["on"]            # the stop words
    OFS=", "                                   # list separator to a
}
{
    for(i=1;i<=NF;i++)                         # list words in a line
        if($i in x==0) {                       # if word is not a stop word
            $i=tolower($i)                     # lowercase it
            gsub(/^[^a-z]|[^a-z]$/,"",$i)      # remove leading and trailing non-alphabets
            a[$i]=a[$i] (a[$i]==""?"":OFS) NR  # add record number to list
        }
    }
END {                                          # after file is processed
    for(i in a)                                # in no particular order
        print i ": " a[i]                      # ... print elements in a
}'
this: 1, 1
test: 1
is: 1
awk' 开始{ x[“a”];x[“an”];x[“the”];x[“on”]#停止词 OFS=“,”#将分隔符列表到 } { 对于(i=1;i
$echo),这是本测试。|#一些测试文本
awk'
开始{
x[“a”];x[“an”];x[“the”];x[“on”]#停止词
OFS=“,”#将分隔符列表到
}
{

对于(i=1;i)您正在测试此内容的示例文件是什么?预期输出是什么?您正在测试此内容的示例文件是什么?预期输出是什么?
$ echo This is this test. |                    # some test text
awk '
BEGIN{
    x["a"];x["an"];x["the"];x["on"]            # the stop words
    OFS=", "                                   # list separator to a
}
{
    for(i=1;i<=NF;i++)                         # list words in a line
        if($i in x==0) {                       # if word is not a stop word
            $i=tolower($i)                     # lowercase it
            gsub(/^[^a-z]|[^a-z]$/,"",$i)      # remove leading and trailing non-alphabets
            a[$i]=a[$i] (a[$i]==""?"":OFS) NR  # add record number to list
        }
    }
END {                                          # after file is processed
    for(i in a)                                # in no particular order
        print i ": " a[i]                      # ... print elements in a
}'
this: 1, 1
test: 1
is: 1