1

File contains

Today is 1 nice day
This is a perfect day
Tomorrow is 1/2 cool 1/2 hot day
What is Toy Story
Tonight is a dark night

How do I find lines
starts with "To" and
end with "day"

None of these commands are working

awk '/^To[*]day$/ {print}' file

awk '/^To*day$/   {print}' file

awk '/^To.day$/   {print}' file

awk '/^To[.]day$/ {print}' file

THANKS!

Timothy Martin
  • 8,447
  • 1
  • 34
  • 40
Rina
  • 11
  • 2

2 Answers2

4

In regular expressions, . matches any character and * is a qualifier that you append to something to say 0 or more of that something. So .* is any number (0 or more) of characters.

So, like Angelo said, you could do:

awk '/^To.*day$/'

The beginning of the record (^) followed by To followed by any number of characters (.*) followed by day followed by the end of the record ($).

Alternatively, you could write it:

awk '/^To/ && /day$/'

That would also match on records where what's between To and day contains non-characters. Another difference would be in cases like when you replace To/day with abc/cde which would match on abcde.

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
2
awk '$0 ~ /^To.*day$/  {print}' file

or just

awk '/^To.*day$/  {print}' file

or even (as the default action is to print the record):

awk '/^To.*day$/' file
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
Angelo
  • 1,871
  • 2
  • 13
  • 26