Skip to main content
added 120 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

This is not something grep does. The name "grep" itself is an acronym for "globally search a regular expression and print", which is what the ed command g/re/p does (for a given regular expression re).

ed is an interactive line editor from 1969, but it's most likely installed on your system today nonetheless (it's a standard POSIX tool). We got grep from ed, and it can be seen as a shortcut or alias for a specific functionality of ed, and sed, which is "stream-ed", i.e. a (non-interactive) stream editor.

Let's use sed instead:

$ sed -n '1p' file.txt

The 1p string is a tiny sed "script" that prints (p) the line corresponding to the given address (1, the first line). The editing command 1p would (no surprise) do the same thing in the ed editor by the way.

The -n suppresses the output of anything not explicitly printed by the script, so all we get is the first line of the file file.txt.

Alternatively:

$ sed '1q' file.txt

This prints all lines of the file, but quits (q) at line 1 (after printing it). This is exactly equivalent to head -n 1 file.txt.

In the POSIX standard (in the rationale section for the head command) it says (generalizingparaphrasing) that head -n N is much the same as sed 'Nq', i.e. "print every line, but quit at line N". The reason head was included in the standard at all was due to symmetry with tail (andand backwards compatibility with existing Unix implementations).

This is not something grep does. The name "grep" itself is an acronym for "globally search a regular expression and print", which is what the ed command g/re/p does (for a given regular expression re).

ed is an interactive line editor from 1969, but it's most likely installed on your system today nonetheless. We got grep from ed, and it can be seen as a shortcut or alias for a specific functionality of ed, and sed, which is "stream-ed", i.e. a (non-interactive) stream editor.

Let's use sed instead:

$ sed -n '1p' file.txt

The 1p string is a tiny sed "script" that prints (p) the line corresponding to the given address (1, the first line). The editing command 1p would (no surprise) do the same thing in the ed editor by the way.

The -n suppresses the output of anything not explicitly printed by the script, so all we get is the first line of the file file.txt.

Alternatively:

$ sed '1q' file.txt

This prints all lines of the file, but quits (q) at line 1 (after printing it). This is exactly equivalent to head -n 1 file.txt.

In the POSIX standard it says (generalizing) that head -n N is much the same as sed 'Nq', i.e. "print every line, but quit at line N". The reason head was included in the standard at all was due to symmetry with tail (and backwards compatibility with existing Unix implementations).

This is not something grep does. The name "grep" itself is an acronym for "globally search a regular expression and print", which is what the ed command g/re/p does (for a given regular expression re).

ed is an interactive line editor from 1969, but it's most likely installed on your system today nonetheless (it's a standard POSIX tool). We got grep from ed, and it can be seen as a shortcut or alias for a specific functionality of ed, and sed, which is "stream-ed", i.e. a (non-interactive) stream editor.

Let's use sed instead:

sed -n '1p' file.txt

The 1p string is a tiny sed "script" that prints (p) the line corresponding to the given address (1, the first line). The editing command 1p would (no surprise) do the same thing in the ed editor by the way.

The -n suppresses the output of anything not explicitly printed by the script, so all we get is the first line of the file file.txt.

Alternatively:

sed '1q' file.txt

This prints all lines of the file, but quits (q) at line 1 (after printing it). This is exactly equivalent to head -n 1 file.txt.

In the POSIX standard (in the rationale section for the head command) it says (paraphrasing) that head -n N is much the same as sed 'Nq', i.e. "print every line, but quit at line N". The reason head was included in the standard at all was due to symmetry with tail and backwards compatibility with existing Unix implementations.

added 41 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

This is not something grep does. The name "grep" itself is an acronym for "globally search a regular expression and print", which is what the ed command g/re/p does (for a given regular expression re).

ed is an interactive line editor from 1969, but it's most likely installed on your system today nonetheless. From it weWe got grep from ed, whichand it can be seen as a shortcut or alias for a specific functionality of ed, and sed, which is "stream-ed", i.e. a (non-interactive) stream editor.

Let's use sed instead:

$ sed -n '1p' file.txt

The 1p string is a tiny sed "script" that prints (p) the line corresponding to the given address (1, the first line). The editing command 1p would (no surprise) do the same thing in the ed editor by the way.

The -n suppresses the output of anything not explicitly printed by the script, so all we get is the first line of the file file.txt.

Alternatively:

$ sed '1q' file.txt

This prints all lines of the file, but quits (q) at line 1 (after printing it). This is exactly equivalent to head -n 1 file.txt.

In the POSIX standard it says (generalizing) that head -n N is much the same as sed 'Nq', i.e. "print every line, but quit at line N". The reason head was included in the standard at all was due to symmetry with tail (and backwards compatibility with existing Unix implementations).

This is not something grep does. The name "grep" itself is an acronym for "globally search a regular expression and print", which is what the ed command g/re/p does.

ed is an interactive line editor from 1969, but it's most likely installed on your system today nonetheless. From it we got grep, which can be seen as a shortcut or alias for a specific functionality of ed, and sed, which is "stream-ed", i.e. a (non-interactive) stream editor.

Let's use sed instead:

$ sed -n '1p' file.txt

The 1p string is a tiny sed "script" that prints (p) the line corresponding to the given address (1, the first line). The editing command 1p would (no surprise) do the same thing in the ed editor by the way.

The -n suppresses the output of anything not explicitly printed by the script, so all we get is the first line of the file file.txt.

Alternatively:

$ sed '1q' file.txt

This prints all lines of the file, but quits (q) at line 1 (after printing it). This is exactly equivalent to head -n 1 file.txt.

In the POSIX standard it says (generalizing) that head -n N is much the same as sed 'Nq', i.e. "print every line, but quit at line N". The reason head was included in the standard at all was due to symmetry with tail (and backwards compatibility with existing Unix implementations).

This is not something grep does. The name "grep" itself is an acronym for "globally search a regular expression and print", which is what the ed command g/re/p does (for a given regular expression re).

ed is an interactive line editor from 1969, but it's most likely installed on your system today nonetheless. We got grep from ed, and it can be seen as a shortcut or alias for a specific functionality of ed, and sed, which is "stream-ed", i.e. a (non-interactive) stream editor.

Let's use sed instead:

$ sed -n '1p' file.txt

The 1p string is a tiny sed "script" that prints (p) the line corresponding to the given address (1, the first line). The editing command 1p would (no surprise) do the same thing in the ed editor by the way.

The -n suppresses the output of anything not explicitly printed by the script, so all we get is the first line of the file file.txt.

Alternatively:

$ sed '1q' file.txt

This prints all lines of the file, but quits (q) at line 1 (after printing it). This is exactly equivalent to head -n 1 file.txt.

In the POSIX standard it says (generalizing) that head -n N is much the same as sed 'Nq', i.e. "print every line, but quit at line N". The reason head was included in the standard at all was due to symmetry with tail (and backwards compatibility with existing Unix implementations).

added 366 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

This is not something grep does. The name "grep" itself is an acronym for "globally search a regular expression and print", which is what the ed command g/re/p does.

ed is an interactive line editor from 1969, but it's most likely installed on your system today nonetheless. From it we got grep, which can be seen as a shortcut or alias for a specific functionality of ed, and sed, which is "stream-ed", i.e. a (non-interactive) stream editor.

Let's use sed instead:

$ sed -n '1p' file.txt

The 1p string is a tiny sed "script" that prints (p) the line corresponding to the given address (1, the first line). The editing command 1p would (no surprise) do the same thing in the ed editor by the way.

The -n suppresses the output of anything not explicitly printed by the script, so all we get is the first line of the file file.txt.

You could useAlternatively:

$ sed '1q' file.txt

This prints all lines of the file, but quits (q) at line 1 (after printing it). This is exactly equivalent to head -n 1 file.txt too, which is doing pretty much the same thing, but with the difference that it quits right after outputting the first line, whereas sed consumes its input to the end.

In the POSIX standard it says (generalizing) that head -n N is much the same as sed Nq'Nq', i.e. "print every line, but quit at line N". The reason head was included in the standard at all was due to symmetry with tail (and backwards compatibility with existing Unix implementations).

This is not something grep does. The name "grep" itself is an acronym for "globally search a regular expression and print", which is what the ed command g/re/p does.

ed is an interactive line editor from 1969, but it's most likely installed on your system today nonetheless. From it we got grep, which can be seen as a shortcut or alias for a specific functionality of ed, and sed, which is "stream-ed", i.e. a (non-interactive) stream editor.

Let's use sed instead:

$ sed -n '1p' file.txt

The 1p string is a tiny sed "script" that prints (p) the line corresponding to the given address (1, the first line). The editing command 1p would (no surprise) do the same thing in the ed editor by the way.

The -n suppresses the output of anything not explicitly printed by the script, so all we get is the first line of the file file.txt.

You could use head -n 1 file.txt too, which is doing pretty much the same thing, but with the difference that it quits right after outputting the first line, whereas sed consumes its input to the end.

In the POSIX standard it says (generalizing) that head -n N is much the same as sed Nq, i.e. "print every line, but quit at line N". The reason head was included in the standard at all was due to symmetry with tail.

This is not something grep does. The name "grep" itself is an acronym for "globally search a regular expression and print", which is what the ed command g/re/p does.

ed is an interactive line editor from 1969, but it's most likely installed on your system today nonetheless. From it we got grep, which can be seen as a shortcut or alias for a specific functionality of ed, and sed, which is "stream-ed", i.e. a (non-interactive) stream editor.

Let's use sed instead:

$ sed -n '1p' file.txt

The 1p string is a tiny sed "script" that prints (p) the line corresponding to the given address (1, the first line). The editing command 1p would (no surprise) do the same thing in the ed editor by the way.

The -n suppresses the output of anything not explicitly printed by the script, so all we get is the first line of the file file.txt.

Alternatively:

$ sed '1q' file.txt

This prints all lines of the file, but quits (q) at line 1 (after printing it). This is exactly equivalent to head -n 1 file.txt.

In the POSIX standard it says (generalizing) that head -n N is much the same as sed 'Nq', i.e. "print every line, but quit at line N". The reason head was included in the standard at all was due to symmetry with tail (and backwards compatibility with existing Unix implementations).

added 366 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading
deleted 5 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading