linux common commands (V)

Related commands for text processing

  • sort
  • uniq
  • cut
  • comm
  • diff

sort: sort the data in the file, and displays the results on the standard output

Command syntax: sort [option] [file]

Options Option Meaning
-m If a given file is sorted, then merged file
-u After ordering the same line of thought, leaving only one row
-d Lexicographically sorting, comparison only letters, numbers, spaces and tabs meaningful
-f The capital letters and lowercase letters treated equally
-r Sort results by reverse output
-c Check whether a given file has been sorted, if they have not sorted, print an error message and exit with status value 1

Example: The data file cs.log sorted and displayed on the screen

[root@localhost ~]# sort cs.log

Cs.log read the file contents to reverse sort the file and displayed on the screen

[root@localhost ~]# sort -r cs.log

uniq: delete the duplicate rows from the output file

Command syntax: uniq [options] [file]

Options Option Meaning
-c In the display output of each line plus the number of the Bank appear in the file
-d Output only duplicate rows
-D Show all duplicate rows
-u Display only files do not duplicate rows
-s k Skip money when compared to K characters, K is the number
-w K After the contents of the first K characters per line not controls, K is the number
-f K Comparison skip the first K columns, K is the number
-i When case-insensitive comparison

Example: do not duplicate rows of data view file contents cs.log

[root@localhost ~]# cat cs.log
wewe
wewe
uoij
124
[root@localhost ~]# uniq -u cs.log uoij 124 

cut: shows the selected byte, character or field (field) line from each file

Command syntax: cut [options] [file]

Options Option Meaning
-b <list> Select only these bytes specified
-c <list> Select only those characters specified
-d <delimiter> Instead of using the specified character tab exploded night boundary region
-f <list> Separated list of fields specified file is contemplated delimiter (The default tab)
-n Cancel split multi-byte characters. Only used with the -b option. If the last-byte characters within a range of fall have a list of parameters indicated by the -b option, the character will be written out; otherwise change the character will be excluded
-s No printed line contains no delimiter

Examples: display file / etc / passwd user login and user full name field, and this is the first one of the five fields separated by a colon.

[root@localhost ~]# cut -f 1,5 -d: /etc/passwd
root:root
bin:bin
daemon:daemon
adm:adm lp:lp sync:sync shutdown:shutdown halt:halt mail:mail operator:operator games:games ftp:FTP User nobody:Nobody systemd-network:systemd Network Management dbus:System message bus polkitd:User for polkitd sshd:Privilege-separated SSH postfix: chrony: li: 

comm: Progressive compare two files had been ranked order of

Command Syntax: comm [options] [file 1] [File 2]

Options Option Meaning
-1 The output file is not unique to line 1
-2 2 output file is not specific to the line
-3 The output file is not unique to line 3

Example: Compare the file file1 and file2 file contents

[root@localhost ~]# cat file1
a
aa
[root@localhost ~]# cat file2
a
bb

[root@localhost ~]# comm file1 file2
		a
aa
	bb
comm: file 2 is not in sorted order 

Comparison of file1 and file2, only the contents of the display data file file1 and file2 same row

[root@localhost ~]# comm -12 file1 file2
a
comm: file 2 is not in sorted order 

diff: one by one to compare two text files, list the differences between

Command syntax: diff [options] [file 1] [File 2]

Options Option Meaning
-b Ignore trailing spaces, two or more of a string of space characters are considered equal
-c Use the context output format
-r When comparing directories, recursively compare any subdirectories found
-Y In two side by side output format
-W<n> When parallel output format, using the specified column width, n represents a number
-u Using a common output format
-i Ignore case differences in file contents
-w Ignore all whitespace
-a All files are treated as a text file line by line comparison
-B Ignore changes due insert or delete blank lines
-q If only a brief comparison of different output file
-s When two files are the same reports
-t Tab is extended in the output space, the input file protection tab alignment
-x <mode> Exclude files matching pattern
-X <file> When comparing directories, ignore and directories contained in any style specified file matching files and directories

Example: Compare file1 and file2 files, list the differences between

[root@localhost ~]# cat file1
a
aa
[root@localhost ~]# cat file2
a
bb

[root@localhost ~]# diff file1 file2
2c2,3
< aa
---
> bb
> 

Guess you like

Origin www.cnblogs.com/mylive/p/11105684.html