相关疑难解决方法(0)

sed错误:unterminated's'命令

我在sed命令下运行

sed -i s/abc=.*$/abc=def ghi/g hpq_sf_attach_wf_param.txt
Run Code Online (Sandbox Code Playgroud)

它给了我错误:

sed: -e expression #1, char 17: unterminated `s' command
Run Code Online (Sandbox Code Playgroud)

我注意到它是由于def和之间的空间ghi.知道怎么解决吗?

unix sed

5
推荐指数
2
解决办法
2万
查看次数

如何在bash函数参数中保留尾随空格?

请考虑以下bash脚本:

#!/bin/bash

function foo {
  echo -n $1
  echo $2
}

foo 'Testing... ' 'OK' # => Testing...OK
# Whitespace --^                      ^
# Missing whitespace -----------------^
Run Code Online (Sandbox Code Playgroud)

第一个参数中的尾随空格发生了什么?怎么能保存它?

bash whitespace function

5
推荐指数
1
解决办法
1355
查看次数

unix中"$ a"和$ a之间的区别是什么?

例如:

#!/bin/sh
a=0
while [ "$a" -lt 10 ]
   b="$a"
   while [ "$b" -ge 0 ] do
      echo -n "$b "
     b=`expr $b - 1`
   done
   echo
   a=`expr $a + 1`
done*
Run Code Online (Sandbox Code Playgroud)

上面提到的脚本给出了三角形的答案,而没有双引号,它在diff行上一个接一个地落下.

unix shell

5
推荐指数
2
解决办法
2600
查看次数

针对多个sed替换优化shell脚本

我有一个文件,其中包含替换对的列表(大约100个),用于sed替换文件中的字符串.

对如下:

old|new
tobereplaced|replacement
(stuffiwant).*(too)|\1\2
Run Code Online (Sandbox Code Playgroud)

我目前的代码是:

cat replacement_list | while read i
do
    old=$(echo "$i" | awk -F'|' '{print $1}')    #due to the need for extended regex
    new=$(echo "$i" | awk -F'|' '{print $2}')
    sed -r "s/`echo "$old"`/`echo "$new"`/g" -i file
done
Run Code Online (Sandbox Code Playgroud)

我不禁想到有一种更优化的方式来进行替换.我尝试先转动循环以首先运行文件的行,但结果却要贵得多.

有没有其他方法来加速这个脚本?

编辑

感谢所有快速回复.在选择答案之前,让我尝试各种建议.

要澄清一件事:我还需要子表达式/组功能.例如,我可能需要的一个替换是:

([0-9])U|\10  #the extra brackets and escapes were required for my original code
Run Code Online (Sandbox Code Playgroud)

有关改进的一些细节(待更新):

  • 方法:处理时间
  • 原文:0.85s
  • cut而不是awk:0.71s
  • anubhava的方法:0.18s
  • chthonicdaemon的方法:0.01s

bash shell sed

5
推荐指数
1
解决办法
2245
查看次数

bash - 用引号括起所有数组元素或参数

我想在bash中编写一个函数,将参数转发给cp命令.例如:输入

<function> "path/with whitespace/file1" "path/with whitespace/file2" "target path"
Run Code Online (Sandbox Code Playgroud)

我希望它实际上做到:

cp "path/with whitespace/file1" "path/with whitespace/file2" "target path"
Run Code Online (Sandbox Code Playgroud)

但相反,现在我正在实现:

cp path/with whitespace/file1 path/with whitespace/file2 target path
Run Code Online (Sandbox Code Playgroud)

我尝试使用的方法是将所有参数存储在数组中,然后只需将cp命令与数组一起运行.像这样:

function func {
    argumentsArray=( "$@" )
    cp ${argumentsArray[@]}
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,它没有像我之前提到的那样传输引号,因此副本失败了.

linux bash

5
推荐指数
1
解决办法
1141
查看次数

如何检查Makefile中的目录是否“不存在”?如果 [ !-d ... ] 始终返回 true

我想添加几行,以便如果某个目录(由 Makefile 变量 DIR1 设置)不存在,则 make 过程可能会出错。这是添加的部分,但即使我 DIR1 设置的目录存在,也会出错。例如,我创建了名为“dir1”的目录,但出现错误。怎么了?

DIR1 = dir1

.PHONY : check_dir

$(info DIR1 = $(DIR1))

check_dir:  
    if [ ! -d $(DIR1) ]; then \
        $(error "DIR1 does not exist! check Makefile!"); \
    fi
Run Code Online (Sandbox Code Playgroud)

这是运行结果。(我dir1在当前目录中有一个名为的目录)。

> ckim@chan-ubuntu:~/testmake/test1$ make  
> DIR1 = dir1 Makefile:8:
> *** "DIR1 does not exist! check Makefile!".  Stop.
Run Code Online (Sandbox Code Playgroud)

makefile

5
推荐指数
1
解决办法
4333
查看次数

用于计算文件中Word出现次数的Shell脚本

让我们以下面的内容为例

    This file is a test file 
    this file is used to count the word 'file' in this test file
    there are multiple occurrences of word file in some lines in this test file
Run Code Online (Sandbox Code Playgroud)

我想在上面的内容中计算"文件"这个词.

我正在使用下面的shell命令

   cat $filename |  sed "s/_/new/g" | sed "s/$word/_/g" | tr -c -d _ |wc -c
Run Code Online (Sandbox Code Playgroud)

那还好还是有更好的想法..?

shell

4
推荐指数
3
解决办法
3万
查看次数

我想在字符串中嵌入单引号

通常,为了在字符串中嵌入引号,\使用(反斜杠)。

反斜杠在 Bash 脚本中是否有不同的含义?

我的下面的代码不起作用:未包含引号,并报告了以下错误:

recursive_merge.sh: line 7: unexpected EOF while looking for matching `''
recursive_merge.sh: line 14: syntax error: unexpected end of file
Run Code Online (Sandbox Code Playgroud)

我没有解释。14号线根本不存在。

 #!/bin/bash
#############this file should be in the directory directly upper than p0x. sphnum.txt should also be at the same directory
for i in 02 03 04 05 06 07 09 10 11 12 13 14 15 16 17 20 21 22 23 24 25; do
 x=$(grep $i sphnum.txt |cut -c5-6)
 y=$(grep $i sphnum.txt …
Run Code Online (Sandbox Code Playgroud)

linux bash sh

4
推荐指数
1
解决办法
3075
查看次数

echo 中的 Makefile 变量赋值错误

我有这样的文件目标

foo: 
    i = 1
    @echo $(i)
Run Code Online (Sandbox Code Playgroud)

当我像这样运行 make 文件时:

$ make foo 
Run Code Online (Sandbox Code Playgroud)

i = 1
make: i: 命令未找到
Makefile:2: 目标 'foo' 的配方失败
make: [foo] 错误 127

但是如果我不给分配空间(即)

i=1
Run Code Online (Sandbox Code Playgroud)

然后没有错误但没有输出,不打印 i 的值

makefile

4
推荐指数
2
解决办法
2639
查看次数

如何将应用程序链接到 usr/local/bin 以便我可以在终端中全局调用它。苹果电脑

我安装了一个新的代码编辑器。Sublime 3. 我希望能够从命令终端打开 Sublime,这样我就可以使用类似的命令

Editor .
Run Code Online (Sandbox Code Playgroud)

打开我所在目录的代码项目。

为此,我尝试通过以下方式将 Sublime 应用程序链接到我的“usr/local/bin”。

ln -s /Applications/Sublime Text.app /usr/local/bin
ln -s /Applications/Sublime Text /usr/local/bin
Run Code Online (Sandbox Code Playgroud)

“Sublime”现在显示在“/usr/local/bin”目录中,但是以下命令不起作用。

cd /path/code_folder
Sublime . 
Sublime Text.app . 
Sublime Text . 
Run Code Online (Sandbox Code Playgroud)

他们都回来了

-bash: Sublime: command not found
Run Code Online (Sandbox Code Playgroud)

macos bash ls path

4
推荐指数
1
解决办法
3934
查看次数

标签 统计

bash ×5

shell ×3

linux ×2

makefile ×2

sed ×2

unix ×2

function ×1

ls ×1

macos ×1

path ×1

sh ×1

whitespace ×1