Remove the contents of block based on condition [duplicate] - sed

This question already has answers here:
sed multiline delete with pattern
(2 answers)
Closed 4 years ago.
Below sed removes all occurrences of blocks between {content-start} and {content-end}, but want to remove only block contains the sting 'labtest'.
sed -ie '/{content-start.*}/,/{content-end}/d' test.txt
test.txt
{content-start}
abc1
labtest
def1
ghi1
{content-end}
{content-start}
abc2
def2
labtest
ghi2
{content-end}
{content-start}
abc3
def3
ghi3
{content-end}

This might work for you (GNU sed):
sed '/{content-start}/{:a;N;/{content-end}/!ba;/labtest/d}' file
Gather up the lines between the start and end strings then test the collection for the required string and delete if positive.

Related

Removing empty lines with grep as well as [, ], ', and ,? [duplicate]

This question already has answers here:
Combining two sed commands
(2 answers)
Closed 6 months ago.
sed "s/[][,']//g"
I used this, but then I have empty lines getting returned. I know that you can use:
sed '/^[[:space:]]*$/d'
Delete empty lines using sed
However, trying sed "s/[][[:space:]][,']//g" didn't work as well as other stuff I have tried.
sed is a scripting language; combine the two commands to get the effect you seek.
sed "s/[][,']//g;"'/^[[:space:]]*$/d'

How to reverse all words in line with sed? [duplicate]

This question already has answers here:
Reverse input order with sed
(6 answers)
Closed 4 years ago.
For example, we have:
This is the song that doesn`t end
What sed command will turn it into this?
end doesn`t that song the is This
I've found only how to reverse lines in a file (a.k.a. tac):
sed -n '1!G;h;$p'
This might work for you (GNU sed):
sed -r 'G;:a;s/^(\S+)(\s*)(.*\n)/\3\2\1/;ta;s/\n//' file
Append a newline as a delimiter. Split the current line into three and prepend the first word, the following space and the remainder of the line following the newline in that order. Iterate until the pattern matching fails and then remove the introduced newline.
Could you please try following and let me know if this helps you.
awk '{for(i=NF;i>0;i--){printf("%s%s",$i,(i>1?OFS:ORS))}}' Input_file

Sed find replace combination with less sign and slash and more sign [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 7 years ago.
I have a stumbling block with sed replace (Linux shell)
I need to replace
</test>
to
</test1>
tried
sed -i 's/<\/test>/</test1>/g'
and similar variants -but still no luck...so thanks for any hint to try
Try this:
echo '</test>' | sed 's|</test>|</test1>|'
For what you tried, you need to escape the slash in the replacement string:
sed -i 's/<\/test>/<\/test1>/g'
Or change the regex boundary marker character:
sed -i 's%</test>%</test1>%g'

How to pass a variable to sed [duplicate]

This question already has answers here:
Shell variables in sed script [duplicate]
(5 answers)
Closed 8 years ago.
I want to delete words into a line. For example:
I want to delete one word in this line
And I want to delete 'one' to obtain:
I want to delete word in this line
By passing the word through a variable. So far I have got:
WORD=one ; sed -n 's/"$WORD"//g' file.txt > newfile.txt
But, it doesn't do anything. Why not? And how can I make it work?
WORD=one ; sed -e "s/$WORD//g" file.txt > newfile.txt
the key moment is variable expansion. You have to be careful though because shell variable expansion may be sometimes not what you want. In hard cases you have to do something like this:
EXPANDVAR=one; NOEXPANDVAR=another; sed -e 's/'"$EXPANDVAR"'$NOEXPANDVAR//g' file.txt > newfile.txt
In this case sed will replace (remove) pattern one$NOEXPANDVAR , literally.

sed delete line with file path [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
sed: delete using a different delimiter
I can substitute line in file like this
sed "s|$PATH_WITH_SLASH||" file
but I cannot delete it
sed "|$PATH_WITH_SLASH|d" file
The problem is that | character cannot be used for deletion. Why?
If you use other character as address delimiter, you need to use backslash before the first char. So
/address/
or
\|address|