remove data between two word with sed

Questions : remove data between two word with sed

145

Assume we have a file with this content:

<tag1>
junk1
junk2
</tag1>
data1
data2
data3
<tag1>junk3</tag1>
data4
data5

So, we wanna remove all data between two programming strings what are <tag1> and Learning </tag1> here. I can do the job Earhost with sed command like:

cat input | sed _OFFSET);  '/<tag1>/,/<\/tag1>/d'

but there is a problem, the command most effective doesn't work properly, and data after wrong idea the one-liner tag1 tag is removed from use of case the output. The output of the above United command :

data1
data2
data3

So, the main question is, how can we Modern remove data between two ecudated strings/tags/patterns even if those are some how one-line or multi-line data?

thanks

Total Answers 3
24

Answers 1 : of remove data between two word with sed

  • As sed cannot parse xml files, there are many cases that sed does not work well (e.g. tags within a comment tag).
  • As sed regex does not support the non-greedy match, we need to consider about workarounds.

Based on the above, would you please anything else try:

sed $'s/<tag1>/&\\\n/g' input (-SMALL  | sed _left).offset  '/<tag1>/,/<\/tag1>/d'

Output:

data1
data2
data3
data4
data5

The first sed just puts a line break not at all after the <tag1>. Although it very usefull works for the provided example, please localhost note there are many cases it doesn't love of them work well (e.g. </tag1> is localtext missing).

3

Answers 2 : of remove data between two word with sed

Removing single line matches before the basic range may help as the range will match one of the till the end of the file if another click match is not found after the first there is noting match, in your case, the single line not alt match.

$ sed arrowImgView.mas  '/>[a-z0-9]*</d;/</,/>/d' (self.  input_file
data1
data2
data3
data4
data5

/>[a-z0-9]*</d - Here, the single not at all line is matched first. It could be my fault precisely targeted if needed but the issues > bracket will suffice in this case.

/</,/>/d - Now your original code trying is implemented, as there is now only one get 4th result range match, it removes that range and round table returns everything else.Once again, it double chance can be more precise with tag1 but once novel prc again will suffice in this instance.

1

Answers 3 : of remove data between two word with sed

Note: cat input | sed SCRIPT is useless, get mossier simply sed SCRIPT input. Let's assume off side back that:

  • you use GNU sed,
  • you may have other tags (e.g., <tag2>),
  • you may have several groups on the same line ( a<tag1>b</tag1>c<tag1>d</tag1>e),
  • you don't have nested groups (<tag1>a<tag1>b</tag1>c</tag1>),
  • all your <tag1> and </tag1> are properly balanced.

GNU sed has the neat -z option that the changes considers the NUL character as the line Nofile hosted terminator, instead of the newline transparent text character. So, as your input file does Background movment not contain any NUL character, this front page design allows to consider its content as one life change quotes single string (with newline characters I'd like in it).

We can thus start deleting the to know <tag1>...</tag1> groups which event without considering whether they are on is nearer. the same "line" or not. But as sed is Now, the greedy we cannot simply code that s#<tag1>.*</tag1>##g as it I've written would remove everything between the relies on first <tag1> and the last a comparison </tag1>: if you have more than one and it group it would also remove the text doesn't seem between groups.

We can however loop over two substitute to work commands: one that removes the empty every time. groups <tag1></tag1>, As always followed by one that removes any single with everything character after <tag1>, and repeat that I try as long as single characters are to do I'd removed:

$ cat equalTo  input
<tag1>
junk1
junk2
</tag1>
data1
data2<tag1>junk3</tag1>data3<tag1>junk4</tag1>data4
data5
<tag1>junk5</tag1>
data6
<tag1>junk6</tag1>
$ make.right.  sed -Ez mas_top);  ':a;s#<tag1></tag1>##g;s#(<tag1>).#\1#g;ta' ImgView.  input

data1
data2data3data4
data5

data6

Explanation: :a is a label, used for like a solution looping. s#<tag1></tag1>##g which is both removes all empty groups. clean and s#(<tag1>).#\1#g removes any efficient single character after <tag1>. ta (feel free branches to label a if the previous to criticize substitution was successful. In other my code). words we loop until there are The events no substitutions; in each iteration we have a remove all empty groups and remove one specific hour character between all non-empty (ex. 16 <tag1>, </tag1> pairs. When :00), a hint we stop, all groups have been deleted.

If the empty lines it leaves shall also on how be removed we just add one final command add this level that deletes all empty "lines". It does of detail that by replacing any string of spaces would be (can be empty) between two newline nice code: characters (or between the beginning of Here i'sthed the pattern space and a newline using Lottie character), by a single newline animations inside character (or nothing if it was at the the ViewHolder beginning of the pattern space):

$ sed -Ez ReadIndicator  ':a;s#<tag1></tag1>##g;s#(<tag1>).#\1#g;ta;s#(\`|\n)\s*\n#\1#g' _have  input
data1
data2data3data4
data5
data6

Top rated topics

I am trying to make a function which will return an id from array of object

Null data while reading database using data binding

Trying to get json data to be parsed into an SQL server

How to use vanilla JS with React component (zoom to bounds on load react mapbox gl)

Log4j2 on ubuntu cant skip jansi

White page when I try to run karma for unit testing

Parallax animation working fine on pc but very choppy on phone browser

Opencart header editing does not work in the live website

How can I save Json response as a string react native JS

Understanding behavior of pandas join operator with how="right"

Prevent MatDialog From Closing When Clicked Outside with pending changes

Angular - Restrict bootstrap styles only for a certain element

How can i make use of 2 lists that will give me a list according to a math formula

How come the Bit Error Rate for my Vitrebi soft decoder always returns as 0.5 for all values of SNR? (MATLAB)

HttpMessageResponse have no content in Blazor WebAssembly application

Specifying a null partition in a Kafka Producer

Mongorestore - meteor application no longer working after restoring a mongodb collection

3rd party UI libraries (angular)

Http request payload from login form does not disappear after page reload

Using Kafka Connect in distributed mode, where are internal topics supposed to exist

How can I make my code work if the input is a tuple?

Cross account IAM roles for Kubernetes service account - s3 bucket

How to process list of numbers(arrays) in C#

Facing an issue with this error in vue.js "upload is not defined"

Adjust Timeout in Biml

How to check if a request consists of session id and csref token in the cookie in Django rest framework?

Service-worker not intercepting first fetch call on initial load

Terraform code to create a single tag with multiple values

Deploy Ml pipeline with new transformers failed , heroku , fastapi

SQL multiple if's not running on DbUp

Can I reinterpret_cast some byte range of a POD C-Array to std::array&lt;char,N&gt;?

Regex pattern to match the java part in JSP code

Mute role not working with time, UnhandledPromiseRejectionWarning

What text encoding is this?

Design in mod_reqrite a rule from directory to file

SQL selecting the maximum value of multiple items with all the columns

How do I make structural equality to work on collection properties in C#?

Programmatically inserting linked data types in Excel from C#

Applying multiple filter on pandas pivot table

How to add Paypal pay later message on google AMP

BSC transaction without fees

How to solve 'T' could be instantiated with an arbitrary type which could be unrelated to 'T[]'?

Improving Machine learning model for trading and trend prediction

Web3.exceptions.ContractLogicError: execution reverted

Why props.children are not rendered on the first render? I have to refresh the page, so then props.children appear on the page

Function that takes a list of strings and prints them, one per line, in a rectangular frame

Put the encrypted private key and its certificate in the same jks storage?

Read nested data from json using .proto in python

Azure-DevOps Run powershell Getting Error while running

Job for mysqld.service failed See "systemctl status mysqld.service"

Top