Sed and Awk
Sed and Awk
Sed
• Sed is a multipurpose command which performs the work of several
filters
• Sed is non interative stream editor.
• Sed Syntax:
# sed options ‘address action’ file
Sed syntax
# sed options ‘address action’ input_file
By line number
By pattern match
Single-Line address
Range Address
Examples:
• Sed –n ‘1,10p’ /proc/cpuinfo
Single Pattern match address
• use regular expression to match lines
• written between two slashes
• process only lines that match
• may match several lines
• lines may or may not be consecutives
Examples:
• sed ‘/processor/s/:/::/’ /proc/cpuinfo
Range Pattern match address
• Prints line between two springs.
Sed –n ‘/pattern1/,/pattern2/p’ input_file
address1 address2
Example:
print lines that do not contain “obsolete”
sed -e ‘/obsolete/!p’ input-file
Sed options
Example
sed –e ‘1,2p’ –e ‘6,8’ –e ‘$ p’ file_name
sed –i ‘1,2p’ –e ‘6,8’ –e ‘$ p’ file_name
Sed commands
Line Number
• line number command (=) writes the current line number before each
matched/output line
Examples:
• sed '/processor/=' /proc/cpuinfo
• Now try and observe the diffrence:
• sed -n '/processor/=' /proc/cpuinfo
Modify Commands
Modify Commands