Using variable inside of sed -i (regex?) bash

后端 未结 2 1881
遇见更好的自我
遇见更好的自我 2021-01-26 03:47

I\'ve looked at the other sed pages here and i cannot find one that uses -i with a variable in the regex search portion. I am trying to cut out a reque

相关标签:
2条回答
  • 2021-01-26 04:26

    It would seem you want to delete a specific line from a file. You're hopefully getting a single line number in your line variable (but beware - if you have multiple lines that match, you're going to get a list of numbers, and that will cause the rest of your process to explode). The problem you have is that the command you are feeding sed is simply the line number - you are not specifying anything for sed to do with that line number. So, perhaps you want this:

    sed -i "${line}d" file.txt
    

    If I've misunderstood your question, and you're not wanting to delete that line, but simply print it, then replace the d with p...

    0 讨论(0)
  • 2021-01-26 04:29

    I assume, you mean remove line when you say cut out line.

    Try using grep with -v option. I believe you don't need sed here.

    grep -v "$3" ./myresolv.conf
    

    Or if you want to delete line in the file itself

    sed -i.bak "/$3/d" ./myresolv.conf
    

    It's good to use -i with .bak to create backup file before overwirting changes.

    0 讨论(0)
提交回复
热议问题