简体   繁体   English

在Shell脚本中使用SED

[英]Using SED in a shell script

I have two issues that I am seeking help with. 我有两个正在寻求帮助的问题。 I am using Cygwin and a Unix newbie. 我正在使用Cygwin和Unix新手。

1) I have a shell script that executes SED command. 1)我有一个执行SED命令的shell脚本。 In my script called master.sh, I have the following 在我的脚本master.sh中,我有以下内容

    sed -nrf remove.sed < code.tp4 > code.tp5

remove.sed has one line remove.sed有一行

    / INCLUDE /d

When I execute the script via the following command 当我通过以下命令执行脚本时

bash master.sh

I get the following error 我收到以下错误

master.sh: line 12: $'\r':command not found

I have no clue on what is throwing this error. 我不知道是什么引发此错误。

2) My 2nd issue is that the output file "code.tp5" ends up "code.tp5?". 2)我的第二个问题是输出文件“ code.tp5”以“ code.tp5?”结尾。 However, using Windows explorer, the question mark appears to be unprintable characters. 但是,使用Windows资源管理器,问号似乎是无法打印的字符。 The most confusing part of this is that I get no errors when I execute this via the command prompt. 其中最令人困惑的部分是,通过命令提示符执行此操作时,不会出现任何错误。 Any assistance would be appreciated. 任何援助将不胜感激。

You could try: 您可以尝试:

sed -nr s/\\r//;/ INCLUDE /d' < code.tp4 > code.tp5; sed 's/$/\\r$/' code.tp5

The command 's' is for substitution. 命令“ s”用于替换。 '\\r' carriage return. '\\ r'回车。 So s/\\r// will replace carriage return to nothing. 因此, s/\\r//将取代回车符。 After that you will remove the lines that contains the pattern '/ INCLUDE /d'. 之后,您将删除包含模式“ / INCLUDE / d”的行。 And with this: sed 's/$/\\r$/' code.tp5 finally you will add again the carriage return so will became again a DOS text file ( $ means end of line on a regular expression) . 并使用以下代码: sed 's/$/\\r$/' code.tp5最后,您将再次添加回车符,因此将再次成为DOS文本文件( $表示正则表达式的行尾)。 (Unix line endings are only new line, and DOS cas carriage return and newline). (Unix行尾仅是换行,而DOS cas回车和换行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM