Наследственный документ без разделителя

Лично я бы сделал все это на Perl:

$ perl -00ne '/^(Student_\d+)/ && $count{$1}++; 
              /Name:\sA/ && $As++; 
              /Status:\s*Pass/ ? $pass++ : $fail++; 
             END{
                print "$_ : $count{$_}\n" for keys(%count); 
                print "Pass: $pass\nFail:$fail\n"; 
                print "Student names starting with A: $As\n"
            }' file 
Student_2 : 1
Student_1 : 1
Student_50 : 1
Pass: 2
Fail:2
Student names starting with A: 2

Если вы настаиваете на отдельных командах для каждой операции, вы можете использовать:

$ awk '/^Student_/{a[$0]++} END{for(s in a){print s,a[s]}}' file 
Student_1 1
Student_2 1
Student_50: 1
$ perl -ne '$pass++ if /:\s*Pass/; $fail++ if /:\s*Fail/;
     END{print "Pass: $pass\nFail: $fail\n"}' file 
Pass: 2
Fail: 2
$ echo "Student names starting with A: $(grep -c "^Name:\s*A" file )"
Student names starting with A: 2
-1
16.06.2018, 21:45
1 ответ

Todos los shells que probé leen un documento aquí -bien incluso sin el terminador, el documento aquí -simplemente terminará al final -del archivo -. Bash da una advertencia sobre esto, pero busybox/dash/ksh y zsh simplemente lo manejan en silencio.

Por ej.

$ cat heredoctest.sh
#!/bin/bash
cat -n <<EOF
foo
bar
$ bash heredoctest.sh
heredoctest.sh: line 4: warning: here-document at line 2 delimited by end-of-file (wanted `EOF')
     1  foo
     2  bar

Pero la muestra particular de código que presentas parece un error. Tal como está escrito, simplemente agregaría la declaración if -a myPath/myFile.append, lo que no parece muy útil. Parece que el contenido del documento aquí -y el terminador simplemente faltan entre caty if.

2
28.01.2020, 05:08

Теги

Похожие вопросы