Unix-文件编辑-保存

Unix- File Editing-Saving

我正在完成作业,这些是说明。我以为我快完成了,但我一直收到错误消息,说我没有更改文件中的任何内容或没有保存它。并不是真正在寻找整个问题的解决方案,而是寻找我的问题的指南。感谢任何帮助。

  Create a new directory, ~/UnixCourse/editingAsst.

    Copy into that directory the file ~cs252/Assignments/editingAsst/mtswe10.txt

    Use one of the three editors discussed in this language to edit the file ~/UnixCourse/editingAsst/mtswe10.txt

    In line 597, change the semicolon (;) to a period, and capitalize the first letter of the word immediately after it.

    Somewhere within that file is the string “man men”. Change it to “many men”.

    Replace all occurrences in the file of the word “elephant” by the word “wombat” (replacing the case variant “Elephant” by “Wombat”).

    Save your changed file, and exit the editor. (Do not rename the file when you save it. It should still be named mtswe10.txt.)

    Use the diff and grep commands to check your changes and make sure that you have made all the changes that you were supposed to and no others.

    Give the command

    ~cs252/bin/editingAsst

这是我的解决方案:

mkdir ~/UnixCourse/editingAsst
cp ~cs252/Assignments/editingAsst/mtswe10.txt ~/UnixCourse/editingAsst
sed -i '597 s/\([;]\)/./' ~/UnixCourse/editingAsst/mtswe10.txt
sed -i '' '597 s/(\. [a-z])/\U&\E/' ~/UnixCourse/editingAss/mtswe10.txt
sed -i '' ' s/man men/many men/' ~/UnixCourse/editingAsst/mtswe10.txt
sed -i '' ' s/Elephant/Wombat/g' ~/UnixCourse/editingAsst/mtswe10.txt
sed -i '' ' s/elephant/wombat/g' ~/UnixCourse/editingAsst/mtswe10.txt

.

所以直到最后一步我才收到任何错误,当我尝试保存作业时它给出了错误。 sed -i '' '334 s/(\. [a-z])/\U&\E/' ~/UnixCourse/editingAss/mtswe10.txt 也发出警告,但它确实接受了。

提前致谢!

我认为,问题在于除第一个 sed 命令之外的所有命令都有额外的 '' 参数。 sed 将第一个参数视为脚本,将第二个参数视为文件名。因此,第二个命令有一个空脚本和一个它无法处理的复杂文件名。如果你去掉那些额外的参数,它应该可以工作。

mkdir ~/UnixCourse/editingAsst
cp ~cs252/Assignments/editingAsst/mtswe10.txt ~/UnixCourse/editingAsst
sed -i '597 s/\([;]\)/./' ~/UnixCourse/editingAsst/mtswe10.txt
sed -i '597 s/(\. [a-z])/\U&\E/' ~/UnixCourse/editingAss/mtswe10.txt
sed -i 's/you are conquered/ye are conquered/' ~/UnixCourse/editingAsst/mtswe10.txt
sed -i 's/Elephant/Wombat/g' ~/UnixCourse/editingAsst/mtswe10.txt
sed -i 's/elephant/wombat/g' ~/UnixCourse/editingAsst/mtswe10.txt

您可以在不使用 -i 的情况下在管道中完成所有这些操作:

cp ~cs252/Assignments/editingAsst/mtswe10.txt ~/UnixCourse/editingAsst
sed '597 s/\([;]\)/./' ~/UnixCourse/editingAsst/mtswe10.txt |
   sed -597 s/(\. [a-z])/\U&\E/' |
   sed 's/you are conquered/ye are conquered/' |
   sed 's/Elephant/Wombat/g' |
   sed 's/elephant/wombat/g' > tmp.txt
mv tmp.txt ~/UnixCourse/editingAsst/mtswe10.txt