Possible Duplicate:
What are the differences between Perl, Python, AWK and sed?
What is the difference between sed and awk?
也许这不是一个非常具体的问题,但我对grep
、awk
和sed
在Unix/Linux系统管理和文本处理方面的不同感到困惑.
Possible Duplicate:
What are the differences between Perl, Python, AWK and sed?
What is the difference between sed and awk?
也许这不是一个非常具体的问题,但我对grep
、awk
和sed
在Unix/Linux系统管理和文本处理方面的不同感到困惑.
简而言之:
grep
:在文件中搜索特定术语
#usage
$ grep This file.txt
Every line containing "This"
Every line containing "This"
Every line containing "This"
Every line containing "This"
$ cat file.txt
Every line containing "This"
Every line containing "This"
Every line containing "That"
Every line containing "This"
Every line containing "This"
现在awk
和sed
与grep
完全不同.
awk
主要用于数据提取和报告.sed
是流编辑器
示例
$ sed -i 's/cat/dog/' file.txt
# this will replace any occurrence of the characters 'cat' by 'dog'
Awk
$ awk '{print $2}' file.txt
# this will print the second column of file.txt
基本awk
用法:
$ cat file.txt
A 10
B 20
C 60
$ awk 'BEGIN {sum=0; count=0; OFS="\t"} {sum+=$2; count++} END {print "Average:", sum/count}' file.txt
Average: 30
I recommend that you read this book: Sed & Awk: 2nd Ed.
它将帮助您在任何类似unix的环境中成为熟练的sed/awk用户.