grep命令过滤空格开头的注释行?

怎么使用grep命令过滤掉以空格开头的行,比如我想把这些注释都给删除掉再输出

        location /ci/ {
                # proxy_pass http://47.92.112.6:20005;
                proxy_pass http://47.92.112.6:8055/;
                proxy_set_header Host $host:$server_port;
                proxy_set_header X-Forwarded-Proto $remote_addr;
                proxy_set_header X-Real-IP         $remote_addr;

                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                 try_files $uri $uri/ =404;
                #try_files $uri $uri/ /index.html;
        }

        # pass PHP scripts to FastCGI server
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
        #       fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}

我使用grep "^\s*[^# \t].*$" default并没有起到效果

阅读 3.2k
1 个回答

So easy:

grep -v '^\s*#'

Output:

        location /ci/ {
                proxy_pass http://47.92.112.6:8055/;
                proxy_set_header Host $host:$server_port;
                proxy_set_header X-Forwarded-Proto $remote_addr;
                proxy_set_header X-Real-IP         $remote_addr;

                 try_files $uri $uri/ =404;
        }


更进一步,如果想删除所有的注释部分(包括行内#后面的内容),可以使用sed '/^\s*#/d; s/\s*#.*//'

Input:

        location /ci/ {
                # proxy_pass http://47.92.112.6:20005;
                proxy_pass http://47.92.112.6:8055/; # Should be removed
                proxy_set_header Host $host:$server_port;  #           ---- Should be removed
                proxy_set_header X-Forwarded-Proto $remote_addr;
                proxy_set_header X-Real-IP         $remote_addr;

                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                 try_files $uri $uri/ =404;
                #try_files $uri $uri/ /index.html;
        }

        # pass PHP scripts to FastCGI server
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
        #       fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}

Output:

        location /ci/ {
                proxy_pass http://47.92.112.6:8055/;
                proxy_set_header Host $host:$server_port;
                proxy_set_header X-Forwarded-Proto $remote_addr;
                proxy_set_header X-Real-IP         $remote_addr;

                 try_files $uri $uri/ =404;
        }


撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题