string Bash 中的字符串差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/454427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String difference in Bash
提问by Ian Kelling
I'm trying to find a way to determine the difference between two strings in my script. I could easily do this with diff or comm, but I'm not dealing with files and I'd prefer not to output them to files, do the compare and read it back.
我试图找到一种方法来确定我的脚本中两个字符串之间的差异。我可以用 diff 或 comm 轻松地做到这一点,但我不处理文件,我不想将它们输出到文件,进行比较并读回。
I see that comm, diff, cmp all allow to pass either two files OR a file and standard input - I guess that's good if I don't want to output two files...but it's still kinda sucks.
我看到 comm、diff、cmp 都允许传递两个文件或一个文件和标准输入 - 如果我不想输出两个文件,我想这很好......但它仍然有点糟糕。
Been digging around thinking I can use grep or regular expressions - but I guess not.
一直在思考我可以使用 grep 或正则表达式 - 但我想不是。
回答by Ian Kelling
Using diff
or com
or whatever you want:
使用diff
或com
或任何你想要的:
diff <(echo "$string1" ) <(echo "$string2")
Greg's Bash FAQ: Process Substitution
Greg 的 Bash 常见问题解答:进程替换
or with a named pipe
或使用命名管道
mkfifo ./p
diff - p <<< "$string1" & echo "$string2" > p
Greg's Bash FAQ: Working with Named Pipes
Greg 的 Bash 常见问题解答:使用命名管道
Named pipe is also known as a FIFO.
命名管道也称为 FIFO。
The -
on its own is for standard input.
在-
自身为标准输入。
<<<
is a "here string".
<<<
是一个“这里的字符串”。
&
is like ;
but puts it in the background
&
就像;
但把它放在背景中
回答by VonC
Reminds me of this question: How can you diff two pipelines in Bash?
让我想起了这个问题:如何在 Bash 中区分两个管道?
If you are in a bash session, you could do a:
如果您在 bash 会话中,您可以执行以下操作:
diff <cmd1 <cmd2
diff <(foo | bar) <(baz | quux)
with <
creating anonymous named pipes -- managed by bash -- so they are created and destroyed automatically, unlike temporary files.
通过<
创建匿名命名管道——由 bash 管理——因此它们会自动创建和销毁,与临时文件不同。
So if you manage to isolate your two different string as part of a command (grep, awk, sed, ...), you can do - for instance - something like:
因此,如果您设法将两个不同的字符串作为命令(grep、awk、sed 等)的一部分进行隔离,您可以执行以下操作 - 例如 - 类似于:
diff < grep string1 myFile < grep string2 myFile
(if you suppose you have in your file lines like string1=very_complicated_value
and a string2=another_long_and_complicated_value'
: without knowing the internal format of your file, I can not recommend a precise command)
(如果您假设您的文件行中有string1=very_complicated_value
和string2=another_long_and_complicated_value'
: 在不知道文件的内部格式的情况下,我不能推荐一个精确的命令)
回答by Johannes Schaub - litb
I prefer cmp
and Process Substitution feature of bash:
我更喜欢cmp
bash 的处理替换功能:
$ cmp -bl <(echo -n abcda) <(echo -n aqcde)
2 142 b 161 q
5 141 a 145 e
Saying on position 2, a b occurs for the first, but a q for the second. At position 5, another difference is happening. Just replace those strings by variables, and you are done.
在位置 2 上说,第一个出现 ab,第二个出现 aq。在位置 5,另一个不同之处正在发生。只需用变量替换这些字符串,就完成了。
回答by Pithikos
Say you have three strings
假设你有三个字符串
a="this is a line"
b="this is"
c="a line"
To remove prefix b from a
从 a 中删除前缀 b
echo ${a#"$b"} # a line
To remove suffix c from a
从 a 中删除后缀 c
echo ${a%"$c"} # this is
回答by Sida Zhou
Another example:
另一个例子:
before="184613 102050 83756 63054"
after="184613 102050 84192 83756 63054"
comm -23 <(tr ' ' $'\n' <<< $after | sort) <(tr ' ' $'\n' <<< $before | sort)
Outputs
输出
84192