How to read output of sed into a variable

前端 未结 4 1767
野性不改
野性不改 2020-12-13 23:28

I have variable which has value \"abcd.txt\".

I want to store everything before the \".txt\" in a second variable, replacing the \".t

4条回答
  •  眼角桃花
    2020-12-13 23:48

    You can use backticks to assign the output of a command to a variable:

    logfile=`echo $a | sed 's/.txt/.log/'`
    

    That's assuming you're using Bash.

    Alternatively, for this particular problem Bash has pattern matching constructs itself:

    stem=$(textfile%%.txt)
    logfile=$(stem).log
    

    or

    logfile=$(textfile/%.txt/.log)
    

    The % in the last example will ensure only the last .txt is replaced.

提交回复
热议问题