Skip to main content
added 401 characters in body
Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165

Let's take this as a test file:

$ cat testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep
1.2.3.4 5.6.7.8 9.10.11     Bad: Missing 1
1.2.3.4 5.6.7.8 9.10.11.12. Bad: Extra period

Using grep

To select lines with exactly nine periods:

$ grep -E '^([^.]*\.){9}[^.]*$' testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep

[^.]*\. matches any number of non-period characters followed by a ([^.]*\.){9} matches exactly nine sequences of zero or more non-period characters followed by a period. The ^ at the beginning requires that the regex match starting at the beginning of the line. The [^.]*$ means that, between the end of the nine sequences and the end of the line, only non-period characters are allowed.

Using sed

$ sed -En '/^([^.]*\.){9}[^.]*$/p' testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep

The -n option tells sed not to print unless we explicitly ask it to. The p following the regex explicitly asks sed to print those lines which match the regex.

Using awk

$ awk '/^([^.]*\.){9}[^.]*$/' testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep

Or, using awk's ability to define a character to separate fields (hat tip: Jeff Schaller):

$ awk -F. 'NF==10' testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep

Let's take this as a test file:

$ cat testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep
1.2.3.4 5.6.7.8 9.10.11     Bad: Missing 1
1.2.3.4 5.6.7.8 9.10.11.12. Bad: Extra period

Using grep

To select lines with exactly nine periods:

$ grep -E '^([^.]*\.){9}[^.]*$' testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep

Using sed

$ sed -En '/^([^.]*\.){9}[^.]*$/p' testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep

The -n option tells sed not to print unless we explicitly ask it to. The p following the regex explicitly asks sed to print those lines which match the regex.

Using awk

$ awk '/^([^.]*\.){9}[^.]*$/' testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep

Or, using awk's ability to define a character to separate fields (hat tip: Jeff Schaller):

$ awk -F. 'NF==10' testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep

Let's take this as a test file:

$ cat testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep
1.2.3.4 5.6.7.8 9.10.11     Bad: Missing 1
1.2.3.4 5.6.7.8 9.10.11.12. Bad: Extra period

Using grep

To select lines with exactly nine periods:

$ grep -E '^([^.]*\.){9}[^.]*$' testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep

[^.]*\. matches any number of non-period characters followed by a ([^.]*\.){9} matches exactly nine sequences of zero or more non-period characters followed by a period. The ^ at the beginning requires that the regex match starting at the beginning of the line. The [^.]*$ means that, between the end of the nine sequences and the end of the line, only non-period characters are allowed.

Using sed

$ sed -En '/^([^.]*\.){9}[^.]*$/p' testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep

The -n option tells sed not to print unless we explicitly ask it to. The p following the regex explicitly asks sed to print those lines which match the regex.

Using awk

$ awk '/^([^.]*\.){9}[^.]*$/' testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep

Or, using awk's ability to define a character to separate fields (hat tip: Jeff Schaller):

$ awk -F. 'NF==10' testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep
added 228 characters in body
Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165

Let's take this as a test file:

$ cat testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep
1.2.3.4 5.6.7.8 9.10.11     Bad: Missing 1
1.2.3.4 5.6.7.8 9.10.11.12. Bad: Extra period

Using grep

To select lines with exactly nine periods:

$ grep -E '^([^.]*\.){9}[^.]*$' testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep

Using sed

$ sed -En '/^([^.]*\.){9}[^.]*$/p' testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep

The -n option tells sed not to print unless we explicitly ask it to. The p following the regex explicitly asks sed to print those lines which match the regex.

Using awk

$ awk '/^([^.]*\.){9}[^.]*$/' testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep

Or, using awk's ability to define a character to separate fields (hat tip: Jeff Schaller):

$ awk -F. 'NF==10' testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep

Let's take this as a test file:

$ cat testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep
1.2.3.4 5.6.7.8 9.10.11     Bad: Missing 1
1.2.3.4 5.6.7.8 9.10.11.12. Bad: Extra period

Using grep

To select lines with exactly nine periods:

$ grep -E '^([^.]*\.){9}[^.]*$' testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep

Using sed

$ sed -En '/^([^.]*\.){9}[^.]*$/p' testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep

Using awk

$ awk '/^([^.]*\.){9}[^.]*$/' testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep

Let's take this as a test file:

$ cat testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep
1.2.3.4 5.6.7.8 9.10.11     Bad: Missing 1
1.2.3.4 5.6.7.8 9.10.11.12. Bad: Extra period

Using grep

To select lines with exactly nine periods:

$ grep -E '^([^.]*\.){9}[^.]*$' testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep

Using sed

$ sed -En '/^([^.]*\.){9}[^.]*$/p' testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep

The -n option tells sed not to print unless we explicitly ask it to. The p following the regex explicitly asks sed to print those lines which match the regex.

Using awk

$ awk '/^([^.]*\.){9}[^.]*$/' testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep

Or, using awk's ability to define a character to separate fields (hat tip: Jeff Schaller):

$ awk -F. 'NF==10' testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep
Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165

Let's take this as a test file:

$ cat testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep
1.2.3.4 5.6.7.8 9.10.11     Bad: Missing 1
1.2.3.4 5.6.7.8 9.10.11.12. Bad: Extra period

Using grep

To select lines with exactly nine periods:

$ grep -E '^([^.]*\.){9}[^.]*$' testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep

Using sed

$ sed -En '/^([^.]*\.){9}[^.]*$/p' testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep

Using awk

$ awk '/^([^.]*\.){9}[^.]*$/' testfile
1.2.3.4 5.6.7.8 9.10.11.12  Keep