Example, I have a file with the following contents:
eggs
bacon
cereal
eggs
bacon
cheese
cereal
Output:
eggs
bacon
cereal
In the example, I want to delete lines between the words eggs
and cereal
, but only if it contains cheese
in between.
How could I do this using sed
?
I have been asked to be more specific, so here is basically what I want to do, and I hope this makes it more clear:
WeaponData
{
TextureData
{
"crosshair"
{
"file" "vgui/replay/thumbnails/"
"x" "0"
"y" "0"
"width" "64"
"height" "64"
}
"weapon"
{
"file" "sprites/bucket_bat_red"
"x" "0"
"y" "0"
"width" "200"
"height" "128"
}
"weapon_s"
{
"file" "sprites/bucket_bat_blue"
"x" "0"
"y" "0"
"width" "200"
"height" "128"
}
"ammo"
{
"file" "sprites/a_icons1"
"x" "55"
"height" "15"
}
"crosshair"
{
"file" "sprites/crosshairs" <---
"x" "32"
"y" "32"
"width" "32"
"height" "32"
}
"autoaim"
{
"file" "sprites/crosshairs"
"x" "0"
"y" "48"
"width" "24"
"height" "24"
}
}
}
This is the command I tried:
sed '/"crosshair"/,/}/d' file.txt
That command just deletes from the very first "crosshair"
to the very last }
, regardless of whether it has the line "sprites/crosshairs"
inbetween or not.
I only want to delete from "crosshair"
to }
if between the patterns, the string "sprites/crosshairs"
is found.
However there are other blocks of code on the text file containing "sprites/crosshairs"
.
Such as:
"autoaim"
{
"file" "sprites/crosshairs"
"x" "0"
"y" "48"
"width" "24"
"height" "24"
}
This cannot be removed. I DEEPLY apologize for not explaining this earlier, but I did not think it would be necessary.
don_crissti's suggestion works quite well which is sed '/crosshair/,/\}/{H;/\}/!d;s/.*//;x;/sprites\/crosshairs/d;s/.//;}' infile
The output is this, however:
... "autoaim" { } }
"autoaim"
is partially deleted, but not completely, but as you can see the block containing "sprites/crosshairs"
is removed, as I wanted to do, but autoaim
still cannot be modified