Searching and Editing Text Files


Search and edit text files using the following commands:

  • grep: The command grep allows you to search inside a number of files for a particular search pattern and then print matching lines. For example, grep blah file will search for the text "blah" in the file and then print any matching lines.

  • sed: The sed (or Stream EDitor) command allows search and replace of a particular string in a file. For example, if you wanted to find the string "cat" and replace it with "dog" in a file named pets, you would type sed s/cat/dog/g.

Both grep and sed are extremely powerful programs. There are many excellent tutorials available on using them, but here are a few good Web sites to get you started:

  • http://pegasus.rutgers.edu/~elflord/unix/grep.html

  • http://pegasus.rutgers.edu/~elflord/unix/sed.html

  • www.itworld.com/Comp/2378/swol-1199-unix101/

Three other commands that are useful for dealing with text include:

  • cat: The cat command, short for concatenate, is useful for viewing and adding to text files. The simple cat FILENAME will display the contents of the file. Using cat filename file adds the contents of the first file to the second.

  • nano: Nano is a simple text editor for the command line. To open a file, use nano filename. Commands listed at the bottom of the screen are accessed via ctrl+letter name.

  • less: The less command is used for viewing text files as well as standard output. A common usage is to pipe another command through less to be able to see all the output, such as ls | less.



The Official Ubuntu Book
The Official Ubuntu Book
ISBN: 0132435942
EAN: 2147483647
Year: 2006
Pages: 133

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net