String difference in Bash

后端 未结 5 899
借酒劲吻你
借酒劲吻你 2020-12-04 06:31

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 pr

相关标签:
5条回答
  • 2020-12-04 07:06

    Using diff or com or whatever you want:

    diff  <(echo "$string1" ) <(echo "$string2")
    

    Greg's Bash FAQ: Process Substitution

    or with a named pipe

    mkfifo ./p
    diff - p <<< "$string1" & echo "$string2" > p
    

    Greg's Bash FAQ: Working with Named Pipes

    Named pipe is also known as a FIFO.

    The - on its own is for standard input.

    <<< is a "here string".

    & is like ; but puts it in the background

    0 讨论(0)
  • 2020-12-04 07:06

    Reminds me of this question: How can you diff two pipelines in Bash?

    If you are in a bash session, you could do a:

    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.

    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:

    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)

    0 讨论(0)
  • 2020-12-04 07:07

    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
    

    Original answer here

    0 讨论(0)
  • 2020-12-04 07:21

    I prefer cmp and Process Substitution feature of 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.

    0 讨论(0)
  • 2020-12-04 07:28

    Say you have three strings

    a="this is a line"
    b="this is"
    c="a line"
    

    To remove prefix b from a

    echo ${a#"$b"}  # a line
    

    To remove suffix c from a

    echo ${a%"$c"}  # this is
    
    0 讨论(0)
提交回复
热议问题