如果我从脚本中运行这些命令:

#my.sh
PWD=bla
sed 's/xxx/'$PWD'/'
...
$ ./my.sh
xxx
bla

这很好。

但是,如果我运行:

#my.sh
sed 's/xxx/'$PWD'/'
...
$ ./my.sh
$ sed: -e expression #1, char 8: Unknown option to `s' 

我在教程中读到,要从shell中替换环境变量,你需要停止,并“引用”$varname部分,这样它就不会直接被替换,这就是我所做的,并且只有在变量刚刚定义之前才有效。

我如何才能让sed识别$var作为一个环境变量,因为它是在shell中定义的?

到目前为止,我已经能够找出如何在文件的开头添加一行,但这并不完全是我想要的。我将用一个例子来说明:

文件内容

some text at the beginning

结果

<added text> some text at the beginning

它很相似,但是我不想用它创建任何新的行…

如果可能的话,我希望用sed来做这件事。

我想在任何现有的#include之前,用一个额外的include指令更新大量的c++源文件。对于这类任务,我通常使用带有sed的小型bash脚本来重写文件。

我如何得到sed替换一个文件中字符串的第一次出现,而不是替换每一次出现?

如果我用

sed s/#include/#include "newfile.h"\n#include/

它替换所有#includes。

我们也欢迎为达到同样目的而提出的其他建议。

出于某种原因,我似乎找不到一个直截了当的答案,我现在有点时间紧张。如何使用sed命令在匹配特定字符串的第一行之后插入选择的文本行呢?我有……

CLIENTSCRIPT="foo"
CLIENTFILE="bar"

我想在CLIENTSCRIPT=行之后插入一行,导致…

CLIENTSCRIPT="foo"
CLIENTSCRIPT2="hello"
CLIENTFILE="bar"

我有一个支持iOS8及以后版本的应用程序,内置在Xcode 7中,我使用XIB作为启动屏幕(我没有启动图像)。视图包含一个带有应用版本的UILabel,以及两个带有图像的UIImageViews,这两个图像都出现在images中。xcassets:标志和启动图像。

当我启动应用程序时,UILabel和logo图像显示正确,但如果我在带有iOS9的iPad Air 2上运行应用程序,启动图像就不能显示。我在运行iOS8的Air和Mini上进行了测试,在iPad 2、iPad Air和iPad Air 2的iOS9模拟器上进行了测试,图像在所有这些模拟器上都显示正确。

我运行了一些基本的故障排除,看看我是否能找出发生了什么,但我还没能解决它,我能看到的图像之间的唯一区别是,工作和一个失败的是,当我添加到资产…

以下是我所知道的:

The UIImageView for the splash image is in the correct place, at the correct size. I can tell this because I set its background color to green just to make sure. The view is there, but the image does not appear. So I'm assuming that the view is not to blame. Setting the UIImageView for the splash image to also use the logo image makes the logo image appear in the correct place for the view. This also leads me to assume that the view is not to blame. The UIImage that I am using in the splash image view is used elsewhere in the app and appears fine in those other views (the logo image is also used elsewhere in the app and appears fine). So I'm assuming that the image is valid and having it appear in other views is not a problem. I've confirmed that the settings of the UIImages for the logo and splash in the xcassets file are the same. They are set to Universal, Any width and height, multiple scale factors, rendered as default. There is one difference - the logo has 1x, 2x and 3x scales while the splash image only has 1x and 2x, but I have also tried using UIImages with only 1x, and 1x and 2x values in the UIView and they work (if they were added to the project some time ago). Adding another image of a different size or format (PNG and JPG) to my xcassets and using that UIImage in the UIImageView for the splash image also fails to display. Adding another UIImageView to the XIB file and allocating it a UIImage that was already in the xcassets works, the image appears in the loading screen. Copying and renaming the image files used for the logo and adding them to the project then using that UIImage in the splash view also fails to display. I have tried cleaning the project, restarting the development machine, and deleting the app from the Air 2 and reinstalling it just in case that was a problem.

这最后三个步骤使我相信,在项目文件生命周期的某一点之后添加的图像存在一些问题。当我昨天更新到Xcode 7时,splash图像最初是在Xcode 6中添加的,但logo图像(也添加到Xcode 6中)是在几个月前添加的。

我已经查看了logo图像和splash图像的json文件,它们似乎具有相同的格式。我还浏览了pbxproj文件,寻找不同之处,但我找不到任何不同之处。

所以我想知道是否有人知道为什么我在Air 2上添加的这些新图像在启动屏幕上不显示?我一直在阅读的其他有关未出现图像的问题似乎都与Launch images有关,或与XIB文件中具有关联类的图像有关,这两个问题在这里似乎都不相关。

被迫对当前客户端使用CVS,并且更改了远程回收的地址。在本地代码中更改远程地址的唯一方法是递归搜索和替换。

然而,使用sed命令,我希望工作:

find ./ -type f -exec sed -i "s/192.168.20.1/new.domain.com/" {} \;

我得到一个错误的每个文件:

sed: 1: ".//file/path ...": invalid command code .

我尝试在sed匹配/替换中转义句点,但这并不能解决任何问题。

被迫对当前客户端使用CVS,并且更改了远程回收的地址。在本地代码中更改远程地址的唯一方法是递归搜索和替换。

然而,使用sed命令,我希望工作:

find ./ -type f -exec sed -i "s/192.168.20.1/new.domain.com/" {} \;

我得到一个错误的每个文件:

sed: 1: ".//file/path ...": invalid command code .

我尝试在sed匹配/替换中转义句点,但这并不能解决任何问题。

我运行这个命令,在我的站点根目录下的所有文件中找到并替换'apple'与'orange'的所有出现:

find ./ -exec sed -i 's/apple/orange/g' {} \;

但它不通过子目录。

这个命令有什么问题?

下面是find ./的输出:

./index.php
./header.php
./fpd
./fpd/font
./fpd/font/desktop.ini
./fpd/font/courier.php
./fpd/font/symbol.php

在Linux和Mac上是否都可以调用sed在没有备份的情况下进行就地编辑?虽然OS X附带的BSD sed似乎需要sed -i "…,但GNU sed Linux发行版通常会将引号解释为空的输入文件名(而不是备份扩展名),并且需要sed -i…。

是否有适用于这两种风格的命令行语法,以便我可以在两个系统上使用相同的脚本?

我想从文件中删除一个或多个特定的行号。如何使用sed做到这一点?