Monday, October 24, 2022

[SOLVED] Bash - remove specific textblock from file

Issue

I want to remove a specific block of text from a file. I want to find the start of the text block to remove, and remove everything until a specific pattern is found.

Example string to search in:

\n---\n# Source: app/templates/deployment.yaml\n# template file\napiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: component and then follow many more characters with various special characters -- / ending with another \n---\n that I dont want to remove

I want to remove everything, starting from this string match \n---\n# Source: app/templates/deployment.yaml\n# template file\napiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: component

So basically, find pattern \n---\n# Source: app/templates/deployment.yaml\n# template file\napiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: component and remove everything until I match the next \n---\n

Expected output here would be:

\n---\n that I dont want to remove

Things I tried with sed:

sed 's/\n---\n# Source: app/templates/deployment.yaml\n# template file\napiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: component.*\n---\n//g'

Things I tried with grep:

echo $string  | grep -Ewo "\\\n---\\\n# Source: app/templates/deployment.yaml\\\n# template file\napiVersion: apps/v1\\\nkind: Deployment\nmetadata:\\\n name: component"

Nothing really works. Is there any bash wizard that can help?


Solution

Using literal strings to avoid having to escape any characters and assuming your target string only exists once in the input:

$ cat tst.sh
#!/usr/bin/env bash

awk '
    BEGIN {
        begStr  = ARGV[1]
        endStr  = ARGV[2]
        ARGV[1] = ARGV[2] = ""
        begLgth = length(begStr)
    }
    begPos = index($0,begStr) {
        tail = substr($0,begPos+begLgth)
        endPos = begPos + begLgth + index(tail,endStr) - 1
        print substr($0,1,begPos-1) substr($0,endPos)
    }
' \
    '\n---\n# Source: app/templates/deployment.yaml\n# template file\napiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: component' \
    '\n---\n' \
    "${@:--}"

$ ./tst.sh file
\n---\n that I dont want to remove


Answered By - Ed Morton
Answer Checked By - Katrina (WPSolving Volunteer)