cat all lines from file, which are after "KEYWORD"

3,802

Solution 1

You can do this with sed:

sed '1,/^KEYWORD$/d'

This will delete (omit) all lines from the beginning of the stream until "KEYWORD", inclusive.

Solution 2

Use sed, printing from the KEYWORD match to the end of the file, then using tail to remove the KEYWORD line.

sed -n '/KEYWORD/,$p' file | tail -1

Solution 3

An alternative might be grep in combination with the -A flag, e.g.

grep -A10000 KEYWORD file

Where 10000 is just a big number to denoted the amount of lines until the end of the file, which for your practical daily use should be enough.

Otherwise you could use the amount of lines in the file as parameter like this

grep -A$(wc -l file | cut -d' ' -f1) KEYWORD file

But that is most likely overkill (and not easier to remember than the given sed alternative)

Share:
3,802

Related videos on Youtube

Nikola
Author by

Nikola

Updated on September 18, 2022

Comments

  • Nikola
    Nikola over 1 year

    I copied the example from http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first and saved it locally to test.html on my computer. Yes I did make an ajax_info.txt file too, but when I click the button nothing happens, and on w3schools works fine. Any ideas?

    Here is the copied code:

    <html>
    <head>
    <script type="text/javascript">
    function loadXMLDoc()
    {
        var xmlhttp;
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","ajax_info.txt",true);
    xmlhttp.send();
    }
    </script>
    </head>
    <body>
    
    <div id="myDiv"><h2>Let AJAX change this text</h2></div>
    <button type="button" onclick="loadXMLDoc()">Change Content</button>
    
    </body>
    </html>
    
    • Garvin
      Garvin over 12 years
      Ajax doesn't work locally. You need to have a webserver to post to.
    • zequinha-bsb
      zequinha-bsb over 12 years
      I thaught the same thing but ... how about if he DOES have a webserver in his local machine for tests purposes?
    • Joel Coehoorn
      Joel Coehoorn almost 11 years
      I could see this being really useful for log files with, say, something like a time stamp as the keyword.
  • Nikola
    Nikola over 12 years
    yes, the file did have that - Alan's post made me realize my problem. And yes, I know about jQuery, just had to do this example..
  • Bernhard
    Bernhard almost 11 years
    You'd need the | tail -1 trick of @suspectus to omit the first line.
  • Angel Todorov
    Angel Todorov almost 11 years
    You probably mean $ instead of /$/
  • juniper-
    juniper- about 9 years
    For me this didn't work and I had to use sed '1,/KEYWORD/d', instead. The context was a little different, but using 0, would print the contents of the entire file, no matter what KEYWORD was.