Home > OS >  How to replace two lines with a blank line using SED command?
How to replace two lines with a blank line using SED command?

Time:06-16

I want to replace the first two lines with a blank line as below.

Input:

sample
sample
123
234
235
456

Output:

<> blank line
123
234
235
456

CodePudding user response:

Delete the first line, remove all the content from the second line but don't delete it completely:

$ sed -e '1d' -e '2s/.*//' input.txt

123
234
235
456

Or insert a blank line before the first, and delete the first two lines:

$ sed -e '1i\
' -e '1,2d' input.txt

123
234
235
456

Or use tail instead of sed to print all lines starting with the third, and an echo first to get a blank line:

(echo ""; tail  3 input.txt)

Or if you're trying to modify a file in place, use ed instead:

ed -s input.txt <<EOF
1,2c

.
w
EOF

(The c command changes the given range of lines to new content)

CodePudding user response:

Using sed

$ sed '1{N;x}' input_file

123
234
235
456
  • Related