Substitution with sed + bash function

后端 未结 7 1999
一向
一向 2021-01-03 02:26

my question seems to be general, but i can\'t find any answers.

In sed command, how can you replace the substitution pattern by a value returned by a simple bash fun

相关标签:
7条回答
  • 2021-01-03 03:07

    Bash function inside sed (maybe for other purposes):

    multi_stdin(){ #Makes function accepet variable or stdin (via pipe)
        [[ -n "$1" ]] && echo "$*" || cat -
    }
    
    sans_accent(){ 
        multi_stdin "$@" | sed '
            y/àáâãäåèéêëìíîïòóôõöùúûü/aaaaaaeeeeiiiiooooouuuu/
            y/ÀÁÂÃÄÅÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜ/AAAAAAEEEEIIIIOOOOOUUUU/
            y/çÇñÑߢÐð£Øø§µÝý¥¹²³ªº/cCnNBcDdLOoSuYyY123ao/
        '
    }
    
    eval $(echo "Rogério Madureira" | sed -n 's#.*#echo & | sans_accent#p')
    

    or

    eval $(echo "Rogério Madureira" | sed -n 's#.*#sans_accent &#p')
    
    Rogerio
    

    And if you need to keep the output into a variable:

    VAR=$( eval $(echo "Rogério Madureira" | sed -n 's#.*#echo & | desacentua#p') )
    echo "$VAR"
    
    0 讨论(0)
提交回复
热议问题