Using sed with regex to remove text from a file

danihh1

I'm trying to use sed with regex to remove specific patterns from a file. Here is the command I'm using:

sed '/location .*?\/\s+{(?:[\w\W]+?)}\s*(?=(?:location|$))/d'

Here is a sample text I'm testing with:

location / {
                try_files $uri $uri/ /index.php?$args;
}
location ~*  .(jpg|jpeg|png|gif|ico|css|js)$ {
            expires 365d;
        }

        location ~*  .(pdf)$ {
            expires 30d;
        }

        location ~ \.php$
                {
                        try_files $uri =404;
                        fastcgi_split_path_info ^(.+\.php)(/.+)$;
                        include /etc/nginx/fastcgi_params;
                        fastcgi_index index.php;
                        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        include /etc/nginx/nginx_limits.conf;
                        if (-f $request_filename)
                        {
                                fastcgi_pass unix:/usr/local/php73/sockets/rgeoipdsm1.sock;
                        }
                }
        location ~ / {
                        try_files $uri $uri/ /index.php?$args;
        }

        location ^ / {
                        try_files $uri $uri/ /index.php?$args;
        }

        location ~ / {
                        if (-f $request_filename)
                        {
                                fastcgi_pass unix:/usr/local/php73/sockets/rgeoipdsm1.sock;
                        }
                        try_files $uri $uri/ /index.php?$args;
        }

location ~ \.png {

}

location ~ / {
}

What I'm expecting to see after running sed:

location ~*  .(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 365d;
    }
    
    location ~*  .(pdf)$ {
        expires 30d;
    }

        location ~ \.php$
                {
                        try_files $uri =404;
                        fastcgi_split_path_info ^(.+\.php)(/.+)$;
                        include /etc/nginx/fastcgi_params;
                        fastcgi_index index.php;
                        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        include /etc/nginx/nginx_limits.conf;
                        if (-f $request_filename)
                        {
                                fastcgi_pass unix:/usr/local/php73/sockets/rgeoipdsm1.sock;
                        }
                }
    location ~ \.png {

}

But after running sed I get no changes at all, it seems to match nothing. Is it possible to fine-tune this command (perhaps with escape characters?) so it'll remove the required patterns?

Wiktor Stribiżew

You can use

sed '/^[[:blank:]]*location .*\/[[:blank:]]*{[[:blank:]]*$/,/^[[:blank:]]*}[[:blank:]]*$/d' yourfile > newfile

This sed command finds blocks of lines between line matching ^[[:blank:]]*location .*\/[[:blank:]]*{[[:blank:]]*$ and a line matching ^[[:blank:]]*}[[:blank:]]*$, and removes them (with d).

The ^[[:blank:]]*location .*\/[[:blank:]]*{[[:blank:]]*$ regex matches

  • ^ - start of string
  • [[:blank:]]* - zero or more spaces/tabs
  • location - a location string
  • .* - any zero or more chars
  • \/ - a / char
  • [[:blank:]]*{[[:blank:]]* - a `{~ enclosed with zero or more spaces/tabs
  • $ - end of string.

The ^[[:blank:]]*}[[:blank:]]*$ regex matches a string that only contains a single } char eclosed with optional spaces/tabs.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Using sed to remove a block of text

Remove style tag from a text file with regex

Using sed to extract strings from a text file

bash / sed / awk Remove or gsub timestamp pattern from text file

How to remove the suffix of string in text file using shell script and sed?

Remove a single line from a very large file using sed

Remove line from php file using sed

Using CUT (or SED) to remove a column from a text fil

How to remove a script tag from a text file with sed

extract text from file using sed

using sed to remove equals sign from a file

Unable to parse text from a file using sed?

Remove field from file using commands like awk, sed

using sed in a for in loop to remove words from file

file cleanup using sed and regex (remove some but not all newlines)

How to remove a text followed by backslash in file using sed or regex pattern

Remove parameters from configure file using sed

Shell using sed to remove file extension from find results

Grab a number from a text file using regex

remove line comment from file by using regex

Regex to match and replace text in yaml file using sed command

Remove Text from XML File using Bash

sed: remove from regex

Remove specific text from an XML like file using sed

Remove certain pattern from text using regex

sed or other - remove specific html tag text from file

remove specific string from text file using sed

How to remove similar elements from beginnign of each line in a text file using sed or similar?

Remove specific character strings from text file using sed, no change to output file?