Linux常用命令大全之文本处理
命令代码 | 注释说明 |
---|---|
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’ |
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 | 比较两个文件的内容只删除两个文件共有的部分 |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 码农浅知!
评论