2017-06-29 4 views
1

入力ファイル、偶数の出力ファイル、奇数の出力ファイルの3つの引数をとります。偶数、奇数、または非数字のファイルの内容をチェックし、適切に管理します。

入力を確認し、数字を適切なファイルに入れることです。 入力に他の数字以外の文字を入れることができます。

最後に、奇数、偶数、およびその他の文字がいくつ見つかったかをユーザーに知らせる必要があります。でものための

1 2 3 4 52 
9 2 32 
1 2 a 
9 2 

サンプル出力ファイル:

2 4 52 
2 32 
2 
2 

と奇数のサンプル出力ファイル:

1 3 
9 
1 
9 

そして、ここでは私の現在のソリューションです:ここで

は、入力ファイルのサンプルです

#!/bin/bash 
in=$1 
even=$2 
odd=$3 

regex='^[0-9]+$' 

num_even=0 
num_odd=0 
non_num=0 

while read -r line || [[ -n "$line" ]]; do 
    for number in $line; do 
     number=`echo "$number" | xargs` 
     if [[ $number =~ $regex ]]; then 
      if [[ $(($number % 2)) == 0 ]]; then 
       printf "$number " >> $even 
       num_even=$(($num_even+1)) 
      else 
       printf " $number" >> $odd 
       num_odd=$(($num_odd+1)) 
      fi 
     else 
      non_num=$(($non_num+1)) 
     fi 
    done 
    printf "\n" >> $even 
    printf "\n" >> $odd 
done <$in 

echo "Even numbers: " $num_even 
echo "Odd numbers: " $num_odd 
echo "Non num characters: " $non_num 

これはうまくいきますが、私はBashにとってとても新しいので、何ができたらいいか分かります。ありがとうございました! :)

+2

質問のこのタイプはcodereview.stackexchange.comためのより適切です。 – chepner

+0

@chepner、ありがとう、私はそのページを知らなかった。 – MuchaZ

+0

私は、特にあなたが出発しているなら、私には見えます。 – kabanus

答えて

0

コンプレックスAWK溶液:

awk '{ even=odd=""; 
     for(i=1;i<=NF;i++) { # iterating though the fields of the line 
      if ($i!~/^[0-9]+$/) { a["other"]++; continue } # processing non-digit value 
      if ($i%2) { odd=(odd=="")? $i:odd FS $i; a["odd"]++; } 
      else { even=(even=="")? $i:even FS $i; a["even"]++; } 
     } 
     if (even!="") print even >> "even.txt"; # redirecting resulting `even` values into respective file 
     if (odd!="") print odd >> "odd.txt" 
    } 
    END{ 
     print "Even numbers: "a["even"]; 
     print "Odd numbers: "a["odd"]; 
     print "Non numeric characters: "a["other"] 
    }' input 

出力:

Even numbers: 7 
Odd numbers: 5 
Non numeric characters: 1 

even.txt内容:

2 4 52 
2 32 
2 
2 

odd.txt内容:

1 3 
9 
1 
9 
1

試してみてください。また、ここではawkを持つもう一つのアプローチ。

EDIT:

awk '{ 
     for(i=1;i<=NF;i++)    {   ###Starting a for loop here starting from i variable value from 1 to till NF value. 
     if($i%2==0 && $i ~ /[0-9]/)  {   ###Checking condition here if value of a field if it is completly dived by 0 and having digits in it. 
     count_even++;        ###Counting even numbers here and incrementing it by 1 each time. 
     printf("%s%s",$i,i==NF?"":" ") > "even_file";### printing the value of that field and printing either space, #if it is not the last field and print NULL if last field of the line. 
             } 
     else if($i%2!=0 && $i ~ /[0-9]/){   ###Checking condition if value of any field is null completly divided by 2 and it is having digits too in it. 
     count_odd++;        ###Counting odd numbers here and incrementing it by 1 each time. 
     printf("%s%s",$i,i==NF?"":" ") > "odd_file";###printing field value with space or null depending on its field number for last field it will be null. 
             } 
     else if($i !~ /[0-9]/)   { 
     count_non_digit++;       ###Counting non-digit values and incrementing them each time by 1. 
             } 
             }; 
     print "" > "even_file";      ###Printing a new line into file named even_file. 
     print "" > "odd_file";      ###Printing a new line into file named odd_file. 
    } 
     END        { 
     print "Even numbers: ",count_even;   ###Printing the even count here. 
     print "Odd numbers: ",count_odd;   ###Printing the odd count here. 
     print "Non numeric characters: ",count_non_digit; ###Printing the non-digit count here. 
             } 
    ' Input_file         ###Mentioning Input_file here. 
関連する問題