1

I have a file as:

$ cat file
.
.
4h
5h
6h
7h
.
.
.
16h
17h
18h
.
.

I want to output only the lines that have '6h' and '7h' but using below awk it selects also '16h' and '17h' :(

awk '/6h/ || /7h/{print}' file

How can I make it select only '6h' and '7h' without the '16h' and '17h'. AWK is not mandatory if any other command does the job more easly.

Alfe
  • 56,346
  • 20
  • 107
  • 159
Alferes
  • 53
  • 5

2 Answers2

3

How about this:

grep '^[67]h' file
Alfe
  • 56,346
  • 20
  • 107
  • 159
-1

With grep exact match operator:

grep -w "6h\|7h" file

From grep manual:

-w, --word-regexp

Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore.

Community
  • 1
  • 1
Zumo de Vidrio
  • 2,021
  • 2
  • 15
  • 33