mirror of
https://github.com/jbranchaud/til
synced 2026-01-04 23:58:01 +00:00
Add Grab The First Line Of A File as a sed til
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
|
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
|
||||||
|
|
||||||
_1112 TILs and counting..._
|
_1113 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -959,6 +959,7 @@ _1112 TILs and counting..._
|
|||||||
- [Equivalence Classes Of Repetition MetaChars](sed/equivalence-classes-of-repetition-metachars.md)
|
- [Equivalence Classes Of Repetition MetaChars](sed/equivalence-classes-of-repetition-metachars.md)
|
||||||
- [Extract Value From Command Output With Sed](sed/extract-value-from-command-output-with-sed.md)
|
- [Extract Value From Command Output With Sed](sed/extract-value-from-command-output-with-sed.md)
|
||||||
- [Grab All The Method Names Defined In A Ruby File](sed/grab-all-the-method-names-defined-in-a-ruby-file.md)
|
- [Grab All The Method Names Defined In A Ruby File](sed/grab-all-the-method-names-defined-in-a-ruby-file.md)
|
||||||
|
- [Grab The First Line Of A File](sed/grab-the-first-line-of-a-file.md)
|
||||||
- [OSX sed Does Regex A Bit Different](sed/osx-sed-does-regex-a-bit-different.md)
|
- [OSX sed Does Regex A Bit Different](sed/osx-sed-does-regex-a-bit-different.md)
|
||||||
- [Output Only Lines Involved In A Substitution](sed/output-only-lines-involved-in-a-substitution.md)
|
- [Output Only Lines Involved In A Substitution](sed/output-only-lines-involved-in-a-substitution.md)
|
||||||
- [Reference A Capture In The Regex](sed/reference-a-capture-in-the-regex.md)
|
- [Reference A Capture In The Regex](sed/reference-a-capture-in-the-regex.md)
|
||||||
|
|||||||
29
sed/grab-the-first-line-of-a-file.md
Normal file
29
sed/grab-the-first-line-of-a-file.md
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
# Grab The First Line Of A File
|
||||||
|
|
||||||
|
You can grab the first line of a file with `sed` using either the `p` (print)
|
||||||
|
command or the `d` (delete) command.
|
||||||
|
|
||||||
|
First, the _print_ command can be told to print the line matching the line
|
||||||
|
number `1`. That combined with the `-n` flag, which suppresses all lines not
|
||||||
|
explicitly printed, will print just the first line in the file.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ sed '1 p' README.md
|
||||||
|
# TIL
|
||||||
|
```
|
||||||
|
|
||||||
|
Second, the _delete_ command can be told to delete all lines that aren't the
|
||||||
|
first (`1`) line.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ sed '1! d' README.md
|
||||||
|
# TIL
|
||||||
|
```
|
||||||
|
|
||||||
|
The `1` will match on the first line. By following it with `!`, that will
|
||||||
|
negate it so that it represents all lines except `1`.
|
||||||
|
|
||||||
|
See `man sed` for more details.
|
||||||
|
|
||||||
|
Note: there are more efficient ways, not using `sed`, to get the first line in
|
||||||
|
a file. This is an exercise in using and understanding some `sed` features.
|
||||||
Reference in New Issue
Block a user