Linux bash command that returns where two strings differ -



Linux bash command that returns where two strings differ -

so, i've been googling around, , searching in more detail on stack overflow, can't seem find easy way of doing this:

i want know in way 2 strings (without whitespace) differ, , print exact difference is.

e.g.:

input 1 > "chocolatecakeflavour" input 2 > "chocolateflavour" output: "cake"

i've tried doing diff , dwdiff, cmp, , other known bash commands popped mind, couldn't exact result.

any ideas?

you can utilize diff fold , awk ths:

s="chocolatecakeflavour" r="chocolateflavour" diff <(fold -w1 <<< "$s") <(fold -w1 <<< "$r") | awk '/[<>]/{printf $2}' cake fold -w1 split input string character character (one in each line) diff difference in both lists (1 char in each line) awk '/[<>]/{printf $2}' suppress < or > diff'e output , print in same line

edit: per op's comments below if strings in different lines of file use:

f=file diff <(fold -w1 <(sed '2q;d' $f)) <(fold -w1 <(sed '3q;d' $f)) | awk '/[<>]/{printf $2}' cake

linux string bash command-line difference

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -