Bash中的字符串差异 [英] String difference in Bash
问题描述
我正在尝试找到一种方法来确定脚本中两个字符串之间的差异.我可以使用diff或comm轻松地做到这一点,但是我不处理文件,我更不想将它们输出到文件中,进行比较并读回.
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.
我看到comm,diff,cmp都允许传递两个文件或一个文件和标准输入-我想如果我不想输出两个文件,那很好,但是还是有点烂.
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.
热衷于思考我可以使用grep或正则表达式-但我想不是.
Been digging around thinking I can use grep or regular expressions - but I guess not.
推荐答案
使用diff
或com
或任何您想要的东西:
Using diff
or com
or whatever you want:
diff <(echo "$string1" ) <(echo "$string2")
Greg的Bash常见问题解答:流程替代
Greg's Bash FAQ: Process Substitution
或使用命名管道
mkfifo ./p
diff - p <<< "$string1" & echo "$string2" > p
Greg的Bash常见问题解答:使用命名管道
Greg's Bash FAQ: Working with Named Pipes
命名管道也称为FIFO.
Named pipe is also known as a FIFO.
-
本身用于标准输入.
The -
on its own is for standard input.
<<<
是此处字符串".
&
类似于;
,但将其放在背景中
&
is like ;
but puts it in the background
这篇关于Bash中的字符串差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!