Saturday, 31 December 2016

Shell Script 12 : Regular Expression (b)

How to search a string located at the beginning or the end of a line?
jtony@genoa:~/learn/shell$ cat test
specialize
aaaaaaaaaa
tomcat
tom
atom
tom333
jyjtom

specialise

(a) Using a caret (outside of brackets) allows you to designate the “beginning” of a line.
jtony@genoa:~/learn/shell$ grep '^tom' test
tomcat
tom
tom333

(b) To search for the end of a line, use the dollar sign.
jtony@genoa:~/learn/shell$ grep 'tom$' test
tom
atom

jyjtom

(c) To search for both the beginning and the end of a line, i.e., searach lines only contains 'tom'
jtony@genoa:~/learn/shell$ grep '^tom$' test
tom


No comments:

Post a Comment