How to code sed to delete multiple lines after a keyword until next blank line reached

sed -e example
sed delete line
sed command "-i" option example
sed delete line after match
sed regex
sed /g
sed tutorial
man sed

Can anyone advise me on how to code a sed script that can be used to update a text file as follows:

Each time a line in the file containing the character string 'Header_one' is encountered: - delete the lines that immediately follow the 'Header_one' line until the next blank line is encountered. - but dont delete the 'Header_one' line itself

So as an example, a file containing this:





Would be changed to:





I would be grateful for any guidance or solution. Thanks, James.

Try the below snip

< InputFile sed -e '/Header_one/i Header_one' -e '/Header_one/,/s/d' > outputFile

The idea here is to replace the content between 2 rows and replace it with a header (i.e. Header_one). The second -e part of the codes does delete the data between Header_one and space; while the first -e part replaces it with a new header Header_one.

InputFile and OutputFiles are simple redirections.

You can also look into:

Hope this helps :)

Delete unknown number of lines from * until blank line, To delete from a line containing * to the next empty line, use: sed '/[*]/,/^$/d' filename. Notes: Since * is a regex-active character and we only  For every line that matches pattern here, the code in the {} gets executed. N takes the next line into the pattern space as well, and then d deletes the whole thing before moving on to the next line. This works in any POSIX-compatible sed.

sed is a stream editor, and although one popular version of sed provides an option that appears to make it edit files, I strongly advise against its use. That said, you can produce the output you desire with:

sed -e '/Header_one/i\
' -e '/Header_one/,/^$/d' input

Note that it's a lot cleaner if you don't require keeping that blank line:

sed -e '/Header_one/p' -e '/Header_one/,/^$/d' input

Also: awk '/Header_one/{printf "Header_one\n\n"; next } 1' RS= ORS='\n\n' input

Sed - An Introduction and Tutorial by Bruce Barnett, This is the Grymoire's UNIX/Linux SED editor. This next example will count the number of lines in three files that command changes every line that starts with a "#" into a blank line. for a keyword, remove all comments until you see the second keyword. which quits when the eleventh line is reached. The awk command offers a much simpler solution as compared to sed command. In this example, remove all empty lines using awk: awk 'NF' my_input > my_output. In short, use any one of the following sed syntax to remove all empty lines from a text file under Linux, macOS, *BSD and Unix-like systems: sed '/^ [ [:space:]]*$/d' input > output.

Using Perl

perl -ne ' {  print "Header_one\n" if /Header_one/ ; print if not /Header_one/../^$/ } ' 

Sed One-Liners Explained, Part II: Selective Printing of Certain Lines, This part will explain sed one-liners for selective printing of certain lines. See part one I suggest that you print it before you proceed and keep it in front of you. It will help The "N" command reads the next line of input and appends it to the pattern space. The first one prints a blank line if the file contains just 1 line: sed -e  If you want to only delete the first match, while exiting without changing the file if there are no matches at all, you can use the x command as part of the argument to the g command (to exit the file after the first delete command is performed) and throw in a q! command at the bottom in case the g command doesn't execute for lack of any

sed - 25 examples to delete a line or pattern in a file, Multiple conditions are separated using the ';' operator. However, this wont delete lines containing only some blank spaces. Delete the lines starting from the pattern 'Linux' till the last line: the newline character till the end, which effective deletes the next line after the line containing the pattern Unix. In the following examples, the sed command removes the lines in file that are in a particular position in a file. 1. Delete first line or header line. The d option in sed command is used to delete a line. The syntax for deleting a line is: > sed 'Nd' file. Here N indicates Nth line in a file.

How to delete empty lines using sed command under Linux / UNIX , Explains how to delete (remove) all empty and blank lines with sed How do I delete all empty lines with sed under Linux or Unix-like systems Sed Delete Empty Line Syntax Posted by: Vivek Gite Actually here you might have stopped writting the file after entering 5. awesome, your the first to hit it. The awk script reads: for a line containing bar, get the next line (getline), then skip all subsequent processing (next). The 1 pattern at the end prints the remaining lines. Update. As pointed out in the comment, the above solution did not work with consecutive bar. Here is a revised solution, which takes it into consideration:

Linux sed command help and examples, A sed program consists of one or more sed commands, passed in by one When the end of the script is reached, unless the -n option is in use, the empty string after a newline, and the empty string before a newline. the lines following addr1 until the next line whose input line number is a multiple of N. The x command exits, after writing any changes to the file. Unlike wq which means "write and quit", x only writes to the file if the buffer has been edited. Thus if your file is unaltered, the timestamp will be preserved.

  • welcome to SO, here you are expected to show what you've tried yourself to solve this problem.. if you are new to sed, you can go through for some faqs and learning resources.. after you've some code that you are stuck with, you could ask here for help..… would be a good starting point for this problem