Cheap VPS host selection
Provide server host evaluation information

How Linux sed locks a row of data for replacement

In the sed tool of Linux, you can lock a line of data for replacement by using address location. Address location can use line number or regular expression to specify the line of the text to be replaced.

The following is an example of using sed to lock a row of data for replacement:

Suppose we have a text file named "data. txt", which contains the following contents:

 apple orange banana grape

Now we want to replace the third line (that is, the line containing "banana") with "pear". We can use the following commands:

 sed '3s/.*/pear/' data .txt

In the above command, '3s' means to replace the third line.'. * ' Means match the entire line of text and replace with "pear". Finally, specify the file to be replaced as "data. txt".

After executing the above command, the output result is as follows:

 apple orange pear grape

As you can see, the original third line "banana" has been successfully replaced with "pear".

Do not reprint without permission: Cheap VPS evaluation » How Linux sed locks a row of data for replacement