How to Replace a line in the same file by SED in Unix Shell scripting?

后端 未结 4 1551
无人及你
无人及你 2021-01-21 20:59

In Reference to this question After getting the line identifier matching in first and second file I need to replace the line in first file with the line of second file.For that

4条回答
  •  清酒与你
    2021-01-21 21:43

    Sed is designed to work in a pipeline - hence the name "Stream EDitor". Instead, you could use an ex script to edit the file in place. Ex is the line-based text editor on which vi was originally based (not as old as ed, the old bear skins and stone knives text editor, but almost). A simple example that you can modify for your purpose might be as follows:

    ex t1 << EOF
    $lineNum
    s/^.*$/$newline/
    w
    q
    EOF
    

    This script first goes to the line indicated by $lineNum, substitutes the whole line beginning (^) to end ($) with the contents of $newline, then writes and quits. These commands are surrounded by "<" and "EOF", which constitute the "here" document, essentially setting up the scripted commands as stdin.

提交回复
热议问题