Remove everything before first occurence of specified word

21,224

Solution 1

You simply remove every whitespace character (tabs, spaces, whatever) between the line start and the word "server" (only if there is nothing else but whitespaces) using the following simple command:

sed -i 's/^\s*server/server/' FILENAME

The -i option modifies the file in-place, that means all changes are applied and saved immediately and you don't get any output. If you don't want to rewrite the file and see the modified new version instead, omit the -i option and the command will print it to the STDOUT (standard output stream).

I use the regex ^\s*server to match any number of any kind of whitespaces between the line start and the word "server" inclusive, and let it replace those matches with the word "server" itself.

Solution 2

GNU sed has a special form of range address 0,/pattern/ that lets you match only the first occurrence of pattern in your file e.g.

sed '0,/server/ s/^.*server/server/' /etc/nginx/sites-enabled/default

or (perhaps better, since it anchors the match to the start of line as well)

sed '0,/^.*server/ s//server/' /etc/nginx/sites-enabled/default


Try it first before you add the -i (in-place) flag and/or use a backup -i.bak

Share:
21,224

Related videos on Youtube

ArkadyB
Author by

ArkadyB

Updated on September 18, 2022

Comments

  • ArkadyB
    ArkadyB over 1 year

    How can I remove everything before the first occurence of a specified word using sed?

    What I have tried so far:

    echo $(cat /etc/nginx/sites-enabled/default | sed 's/^*.server/server/') > /etc/nginx/sites-enabled/default
    

    I need to cleanup all the trash before I find the first "server" word.

    Mainly I'd need a sed regex...

    I have a Nginx default file with proxying info that is getting some leading spaces while the script is copying it to a Docker container and therefore it cant be strated. I'd need to delete it with some command.

    File:

           server
    {
    listen 80 default_server;
    location / 
    {
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Connection "";
    proxy_http_version 1.1;
    proxy_pass http://127.0.0.1:5000;
    }
    }
    

    I am not sure what kind of characters are there, so I'd like to remove everything before 'server'.

    • choroba
      choroba about 8 years
      Can you show an example of what needs to be removed?
    • kos
      kos about 8 years
      Also please have a read here and here.
    • kos
      kos about 8 years
      What is there before sever? Spaces, tabulations? How many?
    • ArkadyB
      ArkadyB about 8 years
      No idea, its Docker places there something. When i open it with nano it looks like spaces.. but im not sure.. Is there some way to remove whatever?
    • kos
      kos about 8 years
      Hold on, do you want to remove all the lines before server in the file or all the characters before server in that line?
    • ArkadyB
      ArkadyB about 8 years
      all the characters