| Home | General Help | Email Help | Web Help | Software | FAQ's | Misc | Contact | ||
| Home » General Help » Viewing and Searching through Files | ||
|
Viewing and Searching
Note: These tutorials assume you have a copy of the tutorial.txt file which can be copied from: /home/wug01/public_html/general/tutorial.txt or alternatively, it can be downloaded from http://unix.cms.gre.ac.uk/general/tutorial.txt and saved in your home area. (Right click and Save As) cat cat (concatenate) The command cat can be used to display the contents of a file on the screen. Type: % cat tutorial.txt As you can see, the file is longer than than the size of the window, so it scrolls past making it unreadable. less The command less writes the contents of a file onto the screen a page at a time. Type: % less tutorial.txt Press the space-bar if you want to see another page, type q if you want to quit reading. As you can see, less is used in preference to cat for long files. head The head command writes the first ten lines of a file to the screen. First clear the screen then type: % head tutorial.txt Then type: % head -5 tutorial.txt What difference did the -5 do to the head command? tail The tail command writes the last ten lines of a file to the screen. Clear the screen and type: % tail tutorial.txt How can you view the last 15 lines of the file? Simple searching using less Using less, you can search though a text file for a keyword (pattern). For example, to search through tutorial.txt for the word tutorial, type: % less tutorial.txt then, still in less (i.e. don't press q to quit), type a slash followed by the word to search /tutorial As you can see, /tutorial finds and highlights the keyword. Type n to search for the next occurrence of the word. grep grep is one of many standard UNIX utilities. It searches files for specified words or patterns. First clear the screen, then type: % grep tutorial tutorial.txt As you can see, grep has printed out each line containing the word tutorial. Or has it???? Try typing: % grep tutorial tutorial.txt The grep command is "case sensitive"; it distinguishes between tutorial and Tutorial. To ignore upper/lower case distinctions, use the -i option, i.e. type: % grep -i tutorial tutorial.txt To search for a phrase or pattern, you must enclose it in single quotes (the apostrophe symbol). For example to search for spinning top, type: % grep -i 'spinning top' tutorial.txt Some of the other options of grep are: -v display those lines that do NOT match -n precede each matching line with the line number -c print only the total count of matched lines Try some of them and see the different results. Don't forget, you can use more than one option at a time, for example, the number of lines without the words tutorial or Tutorial is: % grep -ivc tutorial tutorial.txt wc wc (word count) A handy little utility is the wc command, short for word count. To do a word count on tutorial.txt, type: % wc -w tutorial.txt To find out how many lines the file has, type: % wc -l tutorial.txt Summary cat file _____________ display a file more file _____________display a file a page at a time head file _____________display the first few lines of a file tail file _____________display the last few lines of a file grep 'keyword' file ____ search a file for keywords wc file ______________ count number of lines/words/characters in file |
|||||||||||||||||||||||||||||||||
| Home » General Help » Viewing and Searching through Files | ||
| Home | General Help | Email Help | Web Help | Software | FAQ's | Misc | Contact | ||