Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 9
    Would head -1 file.txt not work? Commented Jul 7, 2016 at 18:40
  • @steeldriver you need to make it an answer as it is the way to do it with grep, strangely selected tool for the job. Commented Jul 7, 2016 at 19:00
  • 1
    grep is not the best tool for printing the first line of a file. If you simply meant that you wanted to print the first line matched with grep, or if you have some specific use for grep, please let us know what that is. If we had more context, perhaps we could give an answer that would better help you and the community. Commented Jul 7, 2016 at 19:11
  • @MelBurslan In file.txt I have three lines for example : This is a new file. Second line: The name of the file is newFile Third line : I have not created the new line. So by using grep command how can I print the first line only? Also How which command will help me to print both first and last line? Can you guys please tell me the commands for both the questions? Commented Jul 8, 2016 at 10:59
  • If any of the existing answers solves your problem, please consider accepting it via the checkmark. Thank you! Commented Apr 25, 2017 at 10:40