7.2. Matching a string or character at the beginning of a line with a ^

Using the ^ allows you to match words or characters at the beginning of the line only. We could for example do a ls -l and match only directories, which we can do because if the first character of a ls -l is a ‘d’ then it’s a directory.

^d drwxrwxrw- – match -rw-rw-rw- – no match drwx-rwxr-x – match -rwx-r-x-r-x – no match 

Returning to our code example, using ^001, this would match 001 at the beginning of the line:

1234XC9088 – no match 4523XX9001 – no match 0011XA9912 – match 9931XC3445 – no match 

We can also combine various pattern matching, for example,

^...4XC.... 

would return:

1234XC9088 – match 4523XX9001 – no match 0011XA9912 – no match 9931XC3445 – no match 

Get Linux and Unix Shell Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.