1

Bourne Shell Script で学生データベースの構築に取り組んでいます。以前にファイルに入力されたデータ行を更新できるようにする必要がありますが、その方法がわかりません。これは私が試したことです:

   echo "please enter the name: \c"
   read updateInput

   updateNumber=$(grep -cwi $updateInput database.txt)

   if [ "$updateNumber" -gt "1" ]
   then
          echo "ambiguous input"
   elif [ ! grep -iq "$updateInput" database.txt ]
   then
           echo "record not found"
   else
           lineNumber=$(grep -ni $updateInput database.txt)

           grep -i $updateInput database.txt > tmp
           read first last age course1 course2 < tmp

           echo "record found, enter new value now:"
           echo "name ($first $last): \c" 
           read first last
           echo "age ($age): \c"
           read age
           echo "course-1 ($course1): \c"
           read course1
           while ! fgrep -iwq "$course1" $FILE 
           do
                   echo "course does not exist"
                   echo "course-1: \c"
                   read course1  
           done
           echo "course-2 ($course2): \c"
           read course2
           while ! fgrep -iwq "$course2" $FILE 
           do
                   echo "course does not exist"
                   echo "course-2: \c"
                   read course2  
           done
   fi

しかし、それは明らかにファイルをまったく更新していません。特定の行を取得してそのデータを変更する方法がわかりません。

また、私の elif ステートメントは機能しません。私はそれで何が間違っているのか疑問に思っていました.

みんな、ありがとう。私は Bourne Shell Script の初心者で、これを機能させるのにかなり苦労しました。

4

2 に答える 2

0

sedinplace フラグを指定してコマンドを使用できます-i— よりもはるかに簡単に使用できますed。たとえば、ファイルの 5 行目を変更したい場合は、次のようにします$first|$last|$course

sed -i '' -e s "5s/.*/$first|$last|$course/" database.txt

補足として、awkデータベースから簡単にデータを抽出してレポートを作成するために使用できます。

于 2014-09-13T07:29:25.860 に答える
0

ファイル内の行を更新する場合、あなたの例はあまり明確ではありません。コマンド「sed」を使用できます。これは、ファイルへの検索/置換を実行します。使用方法については、その unix man を確認してください。

すなわち:

sed -i 's/day/night/g' file.txt 

file.txt の「昼」を「夜」に置き換えます

于 2013-04-10T23:44:18.500 に答える