In the use of Linux system, we often need to perform some operational analysis of the file, such as sorting the text, to remove weight, to intercept some of the rules of the field. Cut,sort,uniq is easy to use.
1.sort: Generally there are three modes, such as sorting text, checking files, merging files.
常用方法及参数sort [option] [file] -b,#忽略前空格或者制表符 -d, #根据字典顺序排序,仅比较数字,字母和空字符 -f, #忽略大小写 -i, #仅仅比较可打印文件 -n, #根据算术值比较,空格,十进制数字等。 -R, #根据哈希值随机排序 -r, #颠倒排序结果 -u,#删除重复的行,只保留一个文件
[[email protected] home]# cat text1122343335144115678A9111011[[email protected] home]# sort text #默认比较首个字符的hash值来排序11110111111223333444556789A[[email protected] home]# sort -n text #按照数值排列A11122334556789101111113344[[email protected] home]# sort -nr text #倒序44331111111098765543322111A[[email protected] home]# sort -R text #随机排序4478111922111111A5533331064
2.cut command: Sometimes files are arranged in a regular order, we need to take out a column or a few columns of data, you can use the Cut command from the vertical direction to select the part.
常用方法及参数:cut options [file] -d #自定义分隔符 -c #只选择指定字符 -f #只选择列表中指定的文本列,文本列用列号表示,多个列用逗号隔开 -s #不输出不包含列分隔符的行
例子:查看/etc/passwd文件,取出所有的用户以及对应的uid,gid以及家目录[[email protected] home]# cut -d":" -f 1,3,4,7
The 3.uniq:uniq command is used to check and delete duplicate columns in a text file
-c # 在每列旁边显示该行重复出现的次数。 -d #仅显示重复出现的行列。 -u #仅显示出一次的行列 --help #显示帮助。 --version #显示版本信息。
[[email protected] home]# sort -n textA11122334556789101111113344[[email protected] home]# sort -n text|uniq -c #显示出现的次数 1 A 3 1 2 2 2 3 1 4 2 5 1 6 1 7 1 8 1 9 1 10 3 11 1 33
生成一个55以内的随机数[[email protected] home]# seq 55|sort -R|head -14
Sort,cut,uniq of shell Learning file operation