How to replace a whole line with sed?

后端 未结 6 1782
故里飘歌
故里飘歌 2020-12-07 10:59

Suppose I have a file with lines

aaa=bbb

Now I would like to replace them with:

aaa=xxx

I can do that as follows:

sed \"s         


        
相关标签:
6条回答
  • 2020-12-07 11:32

    This might work for you:

    cat <<! | sed '/aaa=\(bbb\|ccc\|ffffd\)/!s/\(aaa=\).*/\1xxx/'
    > aaa=bbb
    > aaa=ccc
    > aaa=ffffd
    > aaa=[something else]
    !
    aaa=bbb
    aaa=ccc
    aaa=ffffd
    aaa=xxx
    
    0 讨论(0)
  • 2020-12-07 11:34

    If you would like to use awk then this would work too

    awk -F= '{$2="xxx";print}' OFS="\=" filename
    
    0 讨论(0)
  • 2020-12-07 11:36

    Try this:

    sed "s/aaa=.*/aaa=xxx/g"
    
    0 讨论(0)
  • 2020-12-07 11:36

    You can also use sed's change line to accomplish this:

    sed -i "/aaa=/c\aaa=xxx" your_file_here
    

    This will go through and find any lines that pass the aaa= test, which means that the line contains the letters aaa=. Then it replaces the entire line with aaa=xxx. You can add a ^ at the beginning of the test to make sure you only get the lines that start with aaa= but that's up to you.

    0 讨论(0)
  • 2020-12-07 11:36
    sed -i.bak 's/\(aaa=\).*/\1"xxx"/g' your_file
    
    0 讨论(0)
  • 2020-12-07 11:43

    Like this:

    sed 's/aaa=.*/aaa=xxx/'
    

    If you want to guarantee that the aaa= is at the start of the line, make it:

    sed 's/^aaa=.*/aaa=xxx/'
    
    0 讨论(0)
提交回复
热议问题