Search code examples
bashksh

Way to find variable=value using sed & awk in bash


How to extract variable=value in a string using sed & awk?

e.g. consider following string,

Hi Folks how are you. I want to extract birthdate=July0808128 from this string.


Solution

  • $ s="birthdate=July0808128"
    
    $ echo "${s%=*}"
    birthdate
    
    $ echo "${s#*=}"
    July0808128
    

    --edit--

    I have file which many lines. out of which 3 lines are like, >Hi Rock how are you. I want to extract birthdate=July0808128 from this string. >Hi Akshay how are you. I want to extract birthdate=July0808128 from this string. I want to extract birthdate=July0808128 out of above string

    grep -Po 'birthdate=\w+' yourfile 
    

    OR

    grep -Po 'birthdate=\w+[0-9]{7}' yourfile
    

    Should work.