O'Reilly logo

Linux and Unix Shell Programming by David Tansley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required