1. sort - 文本排序
sort
命令用于将文本文件的行排序。默认情况下,sort
命令是按照字符串的字母顺序排序。
sort 的常用命令如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| sort example.txt
sort -u example.txt
sort -n example.txt
sort -n -r example.txt
sort file1 file2
|
2.uniq - 文本去重
uniq
命令用于移除或发现文件中重复的条目。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| uniq example.txt
uniq -c example.txt
uniq -d example.txt
uniq -D example.txt
uniq -u example.txt
uniq -w 3 example.txt
uniq -s 3 example.txt
uniq -f 1 example.txt
|
3.tr - 替换或删除字符
tr
命令主要用于删除文件中控制字符或进行字符转换。使用tr
时要转换两个字符串:字符串 1 用于查询,字符串 2 用于处理各种转换。tr
刚执行时,字符串 1 中的字符被映射到字符串 2 中的字符,然后转换操作开始。
tr
命令的语法如下所示:
1
| tr [OPTION]... SET1 [SET2]
|
常用命令示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| tr '{}' '()' < textfile > newfile
tr '{}' '\[]' < textfile > newfile
tr 'a-z' 'A-Z' < textfile > newfile
tr -cs '[:lower:][:upper:]' '[\n*]' < textfile > newfile
tr -d '\0' < textfile > newfile
tr -s '\n' < textfile > newfile
tr -s '[:space:]' '[#*]'
|
4.grep - 查找字符串
grep
命令用于搜索文本或指定的文件中与指定的字符串或模式相匹配的行。默认情况下,grep
命令只显示匹配的行。
grep
命令的语法如下所示:
1 2
| grep [OPTION]... PATTERN [FILE]... grep [OPTION]... [-e PATTERN | -f FILE] [FILE]...
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| grep blinkfox /etc/passwd
grep -i blinkfox /etc/passwd
grep -r blinkfox /etc/
grep -w blinkfox /etc/
grep -c blinkfox /etc/passwd
grep -n blinkfox /etc/passwd
grep -v blinkfox /etc/passwd
grep --color blinkfox /etc/passwd
|
5.diff - 比较两个文件
diff
命令用于比较两个文件,并找出它们之间的不同。diff
命令的语法如下所示:
1
| diff [OPTION]... from-file to-file
|
常用使用方式如下:
1 2 3 4 5 6 7 8 9 10 11
| diff nsswitch.conf nsswitch.conf.org
diff -w nsswitch.conf nsswitch.conf.org
diff -y nsswitch.conf nsswitch.conf.org
使用 -c 选项,以上下对比的格式输出两个文件的比较结果 diff -c nsswitch.conf nsswitch.conf.org
|
本文章由blinkfox原创,本站仅仅为学习转发