Position specifiers are characters that are used to specify the position of text within a line. Sometimes these are also called anchor characters. The caret character ( ^ ) is the starting position specifier . It is used to match a text string occurring at the start of a line of text. The dollar sign ( $ ) is the end-position specifier and is used to refer to a line that ends with a particular string.
Table 6-1 shows the uses of position specifiers.
Position Specifier Example | Result of Match |
---|---|
^Miami | Matches word Miami at the start of a line. |
Miami$ | Matches word Miami at the end of a line. |
^Miami$ | Matches a line containing only one word, Miami. |
^$ | Matches a blank line. |
^\^ | Matches a ^ at the beginning of a line. |
\$$ | Matches a $ at the end of a line. |
The dollar sign $ is used to match a string if it occurs at the end of a line. Consider a file with the name myfile having contents as shown below after using the cat command.
$ cat myfile Finally I got it done. The procedure for adding a new template is completed in three steps. 1- Create a new template. 2- Assign this template to a node with this procedure. Action -> Agents -> Assign Templates -> Add -> Enter hostname and template nee -> OK 3- After assignment, the template is still on the ITO server. To install it on the required server, the procedure is: Action -> Agents -> Install/Update SW & Config -> Select Templates, Node name & Force update -> OK If step 3 is successful, a message appears on ITO message browser showing that update process on the node is complete. IMPORTANT =========== The template will not work if the node name specified in it is unknown to ITO server. In our template we specified batch_server which was unknown to ITO server node name in the template. Finally I got out the node name which is more convenient as ITO automatically takes current node name if the name is not specified in the template. Template Options =============== 1- It runs every minute. Scans the file only if it is modified. 2- User initiated action is specified to run restart. 3- A short instruction is provided to run the script. It needs to be modified to make more meaningful. $
Let us use the grep command to find all lines in the file that contain the word node.
$ grep node myfile 2- Assign this template to a node with this procedure. message browser showing that update process on the node The template will not work if the node name specified node name in the template. Finally I got out the node current node name if the name is not specified in the $
You found out that there are five lines in the file containing the word node. Now let us find only those lines that end with this word by using the $ position specifier.
$ grep node$ myfile message browser showing that update process on the node node name in the template. Finally I got out the node $
The position specifiers can be used with any command that deals with text-type data.
The caret character ( ^ ) matches a string at the start of a line. Using the same example of finding the word node, now at the start of a line, enter the following command and watch the result.
$ grep ^node myfile node name in the template. Finally I got out the node $
As another example, you can list all users on your system with login names starting with the letter " m " as follows .
$ grep ^m /etc/passwd
Use of position specifiers is very useful in many cases. To show you one example, ^$ can find blank lines in a file. If you want to count blank lines, you can just pipe output of the grep command to the wc command as in the following.
$ grep ^$ myfile wc -l 5 $
This command will scan myfile and tell you exactly how many blank lines there are in the file. You can use the grep command to take out all blank lines from the file as shown below. The grep -v command reverses the selection and shows those lines that are not empty.
$ grep -v ^$ myfile Finally I got it done. The procedure for adding a new template is completed in three steps. 1- Create a new template. 2- Assign this template to a node with this procedure. Action -> Agents -> Assign Templates -> Add -> Enter hostname and template nee -> OK 3- After assignment, the template is still on the ITO server. To install it on the required server, the procedure is: Action -> Agents -> Install/Update SW & Config -> Select Templates, Node name & Force update -> OK If step 3 is successful, a message appears on ITO message browser showing that update process on the node is complete. IMPORTANT =========== The template will not work if the node name specified in it is unknown to ITO server. In our template we specified batch_server which was unknown to ITO server node name in the template. Finally I got out the node name which is more convenient as ITO automatically takes current node name if the name is not specified in the template. Template Options =============== 1- It runs every minute. Scans the file only if it is modified. 2- User initiated action is specified to run restart. 3- A short instruction is provided to run the script. It needs to be modified to make more meaningful. $
Please note that an "empty line" means a line that doesn't contain any characters. Some lines seem to be empty but actually contain a space or tab character. These lines are not matched by the above command. To match a line that contains space characters, you can use ^[ ]$ , where there is a space character between the two square brackets.
Sometimes the actual string contains one of the position specifiers or meta characters. If you pass this string as-is to a command, the shell will expand the meta character to its special meaning, and you will not get correct results. To instruct the shell not to expand a character to its special meaning, you need to escape that character. For this purpose, you use a backslash ( \ ) before the character. For example, if you want to search for the $ character in a file, you will use the grep \$ command instead of grep $ . If you don't escape the $ character, this command will display all contents of the file.
Please note that \ is also a special character. To match a backslash, you need to use two backslashes \\ in the string.
Top |