文本处理 ============================ :: cat file1 file2 ... | command <> file1_in.txt_or_file1_out.txt # general syntax for text manipulation using PIPE, STDIN and STDOUT cat file1 | command( sed, grep, awk, grep, etc...) > result.txt # 合并一个文件的详细说明文本,并将简介写入一个新文件中 cat file1 | command( sed, grep, awk, grep, etc...) >> result.txt # 合并一个文件的详细说明文本,并将简介写入一个已有的文件中 grep Aug /var/log/messages # 在文件 '/var/log/messages'中查找关键词"Aug" grep ^Aug /var/log/messages # 在文件 '/var/log/messages'中查找以"Aug"开始的词汇 grep [0-9] /var/log/messages # 选择 '/var/log/messages' 文件中所有包含数字的行 grep Aug -R /var/log/* # 在目录 '/var/log' 及随后的目录中搜索字符串"Aug" sed 's/stringa1/stringa2/g' example.txt # 将example.txt文件中的 "string1" 替换成 "string2" sed '/^$/d' example.txt # 从example.txt文件中删除所有空白行 sed '/ *#/d; /^$/d' example.txt # 从example.txt文件中删除所有注释和空白行 echo 'esempio' | tr '[:lower:]' '[:upper:]' # 合并上下单元格内容 sed -e '1d' result.txt # 从文件example.txt 中排除第一行 sed -n '/stringa1/p' # 查看只包含词汇 "string1"的行 sed -e 's/ *$//' example.txt # 删除每一行最后的空白字符 sed -e 's/stringa1//g' example.txt # 从文档中只删除词汇 "string1" 并保留剩余全部 sed -n '1,5p;5q' example.txt # 查看从第一行到第5行内容 sed -n '5p;5q' example.txt # 查看第5行 sed -e 's/00*/0/g' example.txt # 用单个零替换多个零 cat -n file1 # 标示文件的行数 cat example.txt | awk 'NR%2==1' # 删除example.txt文件中的所有偶数行 echo a b c | awk '{print $1}' # 查看一行第一栏 echo a b c | awk '{print $1,$3}' # 查看一行的第一和第三栏 paste file1 file2 # 合并两个文件或两栏的内容 paste -d '+' file1 file2 # 合并两个文件或两栏的内容,中间用"+"区分 sort file1 file2 排序两个文件的内容 sort file1 file2 | uniq # 取出两个文件的并集(重复的行只保留一份) sort file1 file2 | uniq -u # 删除交集,留下其他的行 sort file1 file2 | uniq -d # 取出两个文件的交集(只留下同时存在于两个文件中的文件) comm -1 file1 file2 # 比较两个文件的内容只删除 'file1' 所包含的内容 comm -2 file1 file2 # 比较两个文件的内容只删除 'file2' 所包含的内容 comm -3 file1 file2 # 比较两个文件的内容只删除两个文件共有的部分