Free Hosting Online for WorkStations

< Previous | Contents | Next >

Searching For Text With less And vim

less and vim both share the same method of searching for text. Pressing the / key fol- lowed by a regular expression will perform a search. If we use less to view our phonelist.txt file:


[me@linuxbox ~]$ less phonelist.txt

[me@linuxbox ~]$ less phonelist.txt


then search for our validation expression:



(232)

298-2265

(624)

381-1078

(540)

126-1980

(874)

163-2885

(286)

254-2860

(292)

108-518

(129)

44-1379

(458)

273-1642

(686)

299-8268

(198)

307-2440

~

~

~

/^\([0-9]{3}\) [0-9]{3}-[0-9]{4}$


less will highlight the strings that match, leaving the invalid ones easy to spot:


(232) 298-2265 (624) 381-1078 (540) 126-1980 (874) 163-2885 (286) 254-2860 (292) 108-518

(129) 44-1379 (458) 273-1642 (686) 299-8268 (198) 307-2440

~

~

~ (END)

(232) 298-2265 (624) 381-1078 (540) 126-1980 (874) 163-2885 (286) 254-2860 (292) 108-518

(129) 44-1379 (458) 273-1642 (686) 299-8268 (198) 307-2440

~

~

~ (END)


vim, on the other hand, supports basic regular expressions, so our search expression would look like this:

/([0-9]\{3\}) [0-9]\{3\}-[0-9]\{4\}

We can see that the expression is mostly the same; however, many of the characters that are considered metacharacters in extended expressions are considered literals in basic ex- pressions. They are only treated as metacharacters when escaped with a backslash. De-


pending on the particular configuration of vim on our system, the matching will be high- lighted. If not, try this command mode command:

:hlsearch

to activate search highlighting.


Note: Depending on your distribution, vim may or may not support text search highlighting. Ubuntu, in particular, supplies a very stripped-down version of vim by default. On such systems, you may want to use your package manager to install a more complete version of vim.



Top OS Cloud Computing at OnWorks: