0% found this document useful (0 votes)
51 views

Lecture 8 - Text Processing

This document discusses various Linux text processing utilities including grep, sed, cut, awk, head, tail, sort, wc, and tee. It provides brief descriptions of each tool and examples of common uses like searching/replacing text, extracting fields from files, counting lines/words, and piping outputs between commands.

Uploaded by

arnold.7800x3d
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Lecture 8 - Text Processing

This document discusses various Linux text processing utilities including grep, sed, cut, awk, head, tail, sort, wc, and tee. It provides brief descriptions of each tool and examples of common uses like searching/replacing text, extracting fields from files, counting lines/words, and piping outputs between commands.

Uploaded by

arnold.7800x3d
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10

CNS 2205

MCS 8302
Network Programming
Text Processing – grep – sed – cut - awk

11/13/23 1
Text Processing Utilities
• Linux offers various utilities for processing text
• awk: A pattern scanning and processing language.
• sed: sed is a stream editor which copies the input
filenames specified (standard input default) to the
standard output, edited according to a script of
commands.
• cut: Remove selected fields from each line of a file.
• cat: cat reads input from files, or from standard
input, and displays it on the standard output.
• sort: A utility to sort and collate files.
• tee:
11/13/23 2
Text Processing Utilities
• uniq: Remove or report adjacent duplicate lines.
• wc: Display a count of lines, words and characters.
• tr: Apply a translation to characters, eg NEWLINE to
SPACE.
• grep, fgrep and egrep: Search a file for a string or regular
expression.
• echo: Echo arguments to the standard output.
• head: Display the first ten lines of a file
• tail: Display the last ten lines of a file.
• more: Browse or page through a text file, one screen at a
time.
• less, more: An Interactive file browser.

11/13/23 3
Text Processing Utilities
• Piping: |
• Pipes are used to pass the output of one program (stdout)
as the input (stdin) to another
• Output redirection: >, >>
• we can write (>) or append (>>) to a file

11/13/23 4
Text Processing Examples
• head, tail (head: first ten lines, tail: last ten line)
• Can be used to read a file or receive input via pipe
• -n option: number of lines
• For tail, -n+K means line K to the end
• head -5 (first five lines)
• tail -7 (last 7 lines)
• tail -n+10 | head -n 5 : lines 10-14

11/13/23 5
Text Processing Examples
• cut
• -d Choose the delimiter between columns (default
tab)
• -f : Fields to print
• -f1,7 : fields 1 and 7
• -f1-4,7,11-13 :fields 1,2,3,4,7,11,12,13
• cut -d ‘.’ –f2 file.txt

11/13/23 6
Text Processing Examples
• wc
• Print line, word, and character (byte) counts for each
file, and totals of each if more tan one file specified
• -l Print only line counts
• -w print only word counts
• -c print character counts only

11/13/23 7
Text Processing Examples
• grep
• Search for lines that contain a word or match a regular
expression
• -i Ignores case
• -v Output lines that do not match
• -E regular expressions • -f : patterns from a file (1 per
line)
• cat /etc/passwd | grep ozy : read the file /etc/passwd and
show all lines that have the word ozy
• grep ozy /etc/bind/ : show all lines that have the word
ozy in all files and directories in the directory /etc/bind
11/13/23 8
Text Processing Examples
• sed – Stream Editor
• Most common use is a string replace
• Can get input from a pipe or from a file
• sed -e “s/search/replace/g”
• This example replaces all occurrences of the word search
with the word replace

11/13/23 9
Text Processing Examples
• awk - is abbreviated from the names of the
developers – Aho, Weinberger, and Kernighan.
• Awk is mostly used for pattern scanning and
processing.
• awk '{print}' employee.txt : This will print every line in the
file
• awk '/manager/ {print}' employee.txt : Print the lines which
match the given pattern i.e. manager
• awk '{print $1,$4}' employee.txt : Split lines into fields that
are delimited by white space. Note fields are stored in
variables $1,…, $n; $0 stores the original string

11/13/23 10

You might also like