文本操作 [sort]() [uniq]() [comm]() [cmp]() [diff]() [tr]() [sed]() [awk]() [perl]() [cut]() [paste]() [column]() [pr]() 排序 对文本文件的行进行排序 如其名称所示,这个命令用于对文件进行排序。字母排序和数字排序?可以做到。对特定列进行排序?也可以。优先级多排序顺序?可以。随机排序?唯一性?几乎所有排序需求都能通过这个强大的命令满足。
对文本文件的行进行排序
如其名称所示,这个命令用于对文件进行排序。字母排序和数字排序?可以做到。对特定列进行排序?也可以。优先级多排序顺序?可以。随机排序?唯一性?几乎所有排序需求都能通过这个强大的命令满足。
-R 随机排序 -r 反转排序顺序 -o 将排序结果重定向到指定的文件名,非常有用,可以原地排序文件 -n 数值排序 -V 版本排序,识别文本中的数字 -h 人类可读的数字排序,如 4K、3M 等 -k 通过键排序 -u 唯一排序 -b 在排序时忽略行首的空白字符 -t 使用 SEP 替换非空白到空白的转换
sort dir_list.txt 显示标准输出上排序后的文件 sort -bn numbers.txt -o numbers.txt 数值排序 numbers.txt(忽略行首空白字符)并用排序后的输出覆盖文件 sort -R crypto_keys.txt -o crypto_keys_random.txt 随机排序并写入新文件 shuf crypto_keys.txt -o crypto_keys_random.txt 也可以使用 du -sh * | sort -h 按人类可读格式对当前目录中的文件/目录大小进行排序
$ cat ip.txt 6.2 : 897 : bar 3.1 : 32 : foo 2.3 : 012 : bar 1.2 : 123 : xyz
$ # -k3,3 means from 3rd column onwards to 3rd column $ # for ex: to sort from 2nd column till end, use -k2 $ sort -t: -k3,3 ip.txt 2.3 : 012 : bar 6.2 : 897 : bar 3.1 : 32 : foo 1.2 : 123 : xyz
$ # -n option for numeric sort, check out what happens when -n is not used $ sort -t: -k2,2n ip.txt 2.3 : 012 : bar 3.1 : 32 : foo 1.2 : 123 : xyz 6.2 : 897 : bar
$ # more than one rule can be specified to resolve same values $ sort -t: -k3,3 -k1,1rn ip.txt 6.2 : 897 : bar 2.3 : 012 : bar 3.1 : 32 : foo 1.2 : 123 : xyz
报告或忽略重复的行
此命令更具体于识别重复项。通常需要已排序的输入,因为比较仅针对相邻行进行
$ echo -e 'Blue\nRed\nGreen\nBlue\nRed\nBlack\nRed' > colors.txt $ uniq colors.txt Blue Red Green Blue Red Black Red $ echo -e 'Blue\nRed\nGreen\nBlue\nRed\nBlack\nRed' | sort > sorted_colors.txt $ uniq sorted_colors.txt Black Blue Green Red $ uniq -d sorted_colors.txt Blue Red $ uniq -cd sorted_colors.txt 2 Blue 3 Red $ uniq -u sorted_colors.txt Black Green
逐行比较两个已排序的文件
没有选项时,它会以三列的形式输出:文件1独有的行、文件2独有的行和两个文件共有的行
$ echo -e 'Brown\nRed\nPurple\nBlue\nTeal\nYellow' | sort > colors_1.txt $ echo -e 'Red\nGreen\nBlue\nBlack\nWhite' | sort > colors_2.txt $ # the input files viewed side by side $ paste colors_1.txt colors_2.txt Blue Black Brown Blue Purple Green Red Red Teal White Yellow examples $ # 3 column output - unique to file1, file2 and common $ comm colors_1.txt colors_2.txt Black Blue Brown Green Purple Red Teal White Yellow $ # suppress 1 and 2 column, gives only common lines $ comm -12 colors_1.txt colors_2.txt Blue Red $ # suppress 1 and 3 column, gives lines unique to file2 $ comm -13 colors_1.txt colors_2.txt Black Green White $ # suppress 2 and 3 column, gives lines unique to file1 $ comm -23 colors_1.txt colors_2.txt Brown Purple Teal Yellow
逐字节比较两个文件
适用于比较二进制文件。如果两个文件相同,则不显示任何输出(退出状态为0)
如果有差异,则会显示第一个差异——行号和字节位置(退出状态为1)
选项 -s 允许抑制输出,这对于脚本很有用
$ cmp /bin/grep /bin/fgrep /bin/grep /bin/fgrep differ: byte 25, line 1
逐行比较文件
适用于比较文本文件的旧版本和新版本
所有差异都会打印出来,如果文件太长可能不太理想
转换或删除字符。
UNIX 中的 tr 命令是一个命令行实用程序,用于转换或删除字符。它支持包括大写转小写、压缩重复字符、删除特定字符和基本查找替换在内的多种转换。它可以与 UNIX 管道一起使用,以支持更复杂的转换。
tr 命令的语法如下:
tr OPTION... SET1 [SET2]
tr 接受两组字符,通常是长度相同的字符,并将第一组字符替换为第二组字符中的相应字符。
一个 SET 实际上是一串字符,包括特殊转义字符。
echo 'clean this up' | tr -d 'up' clean this
要压缩指定集合中的重复字符实例,请使用 -s 选项。这会移除重复的字符实例。在以下示例中,一个带有过多空格的字符串会被压缩以去除它们。
echo 'too many spaces here' | tr -s '[:space:]' too many spaces here
tr 工具适用于简单的查找和替换操作,在这种操作中,一个字符应替换为另一个字符。以下示例将下划线替换为空格。
echo "some_url_that_I_have" | tr "_" "-" some-url-that-I-have
流编辑器,用于过滤和转换文本
- d 删除模式空间 - p 打印模式空间 - s 查找和替换
默认情况下,sed 对所有输入内容进行操作。可以通过行号范围、搜索模式或两者混合来细化此操作
使用 Sed 进行常见查找和替换示例
以下是翻译后的Markdown文件内容,确保翻译自然流畅,并且保留了所有注释、代码块、URL和路径:
模式扫描和文本处理语言
awk 得名于其作者Alfred Aho、Peter Weinberger和Brian Kernighan。
从每行中移除部分
对于具有明确分隔符的列操作,cut命令非常方便
ls -l | cut -d' ' -f1 首列 ls -l
-d 选项指定分隔符字符,在这种情况下是单个空格字符(默认分隔符是制表符)-f 选项指定要打印的字段,用逗号分隔,在这种情况下是字段1cut -d':' -f1 /etc/passwd 打印 /etc/passwd 文件的第一列cut -d':' -f1,7 /etc/passwd 打印 /etc/passwd 文件的第一列和第七列,用冒号字符分隔cut -d':' --output-delimiter=' ' -f1,7 /etc/passwd 使用空格作为分隔符打印第一列和第七列合并文件中的行
paste list1.txt list2.txt list3.txt > combined_list.txt 将三个文件按列合并到一个文件中,条目由制表符分隔paste -d':' list1.txt list2.txt list3.txt > combined_list.txt 条目由冒号字符分隔而不是制表符
pr 命令以使用多字符分隔符$ # joining multiple files $ paste -d, <(seq 5) <(seq 6 10) 1,6 2,7 3,8 4,9 5,10 $ paste -d, <(seq 3) <(seq 4 6) <(seq 7 10) 1,4,7 2,5,8 3,6,9 ,,10
$ seq 5 | paste - - 1 2 3 4 5 $ # specifying different output delimiter, default is tab $ seq 5 | paste -d, - - 1,2 3,4 5, $ # if number of columns to specify is large, use the printf trick $ seq 5 | paste $(printf -- "- %.s" {1..3}) 1 2 3 4 5
$ seq 10 | paste -sd, 1,2,3,4,5,6,7,8,9,10 $ # for multiple character delimiter, perl can be used $ seq 10 | perl -pe 's/\n/ : / if(!eof)' 1 : 2 : 3 : 4 : 5 : 6 : 7 : 8 : 9 : 10
列出数据
$ cat dishes.txt North alootikki baati khichdi makkiroti poha South appam bisibelebath dosa koottu sevai West dhokla khakhra modak shiro vadapav East handoguri litti momo rosgulla shondesh $ column -t dishes.txt North alootikki baati khichdi makkiroti poha South appam bisibelebath dosa koottu sevai West dhokla khakhra modak shiro vadapav East handoguri litti momo rosgulla shondesh
转换文本文件以便打印
$ pr sample.txt 2016-05-29 11:00 sample.txt Page 1 This is an example of adding text to a new file using cat command. Press Ctrl+d on a newline to save and quit. Adding a line of text at end of file
$ # single column to multiple column, split vertically $ # for example, in command below, output of seq is split into two $ seq 5 | pr -2t 1 4 2 5 3 $ # different output delimiter can be used by passing string to -s option $ seq 5 | pr -2ts' ' 1 4 2 5 3 $ seq 15 | pr -5ts, 1,4,7,10,13 2,5,8,11,14 3,6,9,12,15
-a 选项拆分$ seq 5 | pr -2ats' : ' 1 : 2 3 : 4 5
$ seq 15 | pr -5ats, 1,2,3,4,5 6,7,8,9,10 11,12,13,14,15
```bash $ # 使用 $ 扩展表示由转义字符(如 \t 表示制表符)定义的字符 $ seq 5 | pr -3ts$'\t' 1 3 5 2 4 $ # 或者省略 -s 的参数,因为制表符是默认值 $ seq 5 | pr -3ts 1 3 5 2 4
$ seq 74 | pr -36ats, 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36 37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72 73,74 $ seq 74 | pr -37ats, pr: 页面宽度太窄 $ # (37-1)*1 + 37 = 73 $ seq 74 | pr -Jw 73 -37ats, 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37 38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74 $ # (3-1)*4 + 3 = 11 $ seq 6 | pr -Jw 10 -3ats'::::' pr: 页面宽度太窄 $ seq 6 | pr -Jw 11 -3ats'::::' 1::::2::::3 4::::5::::6
$ pr -mts', ' <(seq 3) <(seq 4 6) <(seq 7 9) 1, 4, 7 2, 5, 8 3, 6, 9
声明:
本文件灏天文库团队进行了翻译。尽管我们力求准确,但请注意,翻译可能包含错误或不准确之处。原文档以其原始语言为准。我们不对因使用此翻译而产生的任何误解或误译负责。