Text Viewing and Processing Commands

Posted by snpo123 on Tue, 25 Jun 2019 20:56:15 +0200

Text view commands: hexdump, od, cat, tac, head, tail, more, less

Text processing commands: tr, cut, wc, sort, uniq, rev, colrm, paste, diff, path


Text View Command

hexdump

View the file and display it in ASCII code or hexadecimal, decimal, octal.

hexdump [options] file...
-b Single byte octal display
-c Single byte character display
-C Hexadecimal sum of output specification ASCII code
-d Two-byte decimal display
-o Two-byte octal display
-x Two-byte hexadecimal display
-e Specified format string
-n Format only the first few bytes of the input file
-s Output from offset
Execution of orders#more ab
ab ab
ab
12
//Execute the command # hexdump-b ab
0000000 141 142 040 141 142 012 141 142 012 061 062 012               
000000c
//Execute the command # hexdump-c ab
0000000   a   b       a   b  \n   a   b  \n   1   2  \n               
000000c
//Execute the command # hexdump-C ab
00000000  61 62 20 61 62 0a 61 62  0a 31 32 0a              |ab ab.ab.12.|
0000000c
//Execute the command # hexdump-o ab
0000000  061141  060440  005142  061141  030412  005062               
000000c

od

Bytes encoded in octal, hexadecimal, or other formats for output files

#od -c ab
0000000   a   b       a   b  \n   a   b  \n   1   2  \n
0000014

cat

cat OPTION... [FILE]...
-n Display line number, including blank lines
-b Display non-empty line numbers, i.e. skip empty lines without showing line numbers
-s Continuous blank lines are displayed as one blank line
-A Display all controls
-E Display line terminator $

cat > f1

type something

After ctrl+d, the content is saved to f1, similar to a simple text editing tool

Cat f1 f2 > f3 // Merge the contents of files f1 and f2 into f3

 

Random generation of 20-bit passwords:

cat /dev/urandom | tr -dc '0-9a-zA-Z' |head -c20

tac

Function with cat, reverse display, that is, the last line is displayed as the first line, the first line is displayed as the last line

 

head

Look at the first few lines of text

- c Specifies # bytes before viewing, and three bytes for a Chinese character

- N specifies the line before viewing, n can be omitted

- v Displays header information for file names

- q does not display header information for file names


tail

Look at the last few lines of text

-c After specifying the view#Bytes
-n After specifying the view#OK, n can be omitted
-f Tracking and displaying new additions to files, often used for log monitoring
-v Display header information for file names
-q Does not display header information for file names

ls /etc/ | head -3  //Display the first three files or directories listed in ls
head -5 a.log
tail -0 -f m.log  View only the new additions
tail -n0 -f /var/log/messages &
who|head -1  Display the first line of command results

cat -n m.log |head -1000|tail -1  Line 1000 to view the results
ifconfig eth0|head -2|tail -1  See eth0 The second line IP Address line

more

Page-by-page view of documents, space page-by-page, b-key up page-by-page, q-key exit

- d Displays page turning and exit hints

 

less

Paging to view files or stdin output allows users to turn pages back and forth. less command is a paging tool used by man command.

/ Search; n Next; N Last


Text Processing Command

tr

Escape compressed character

tr [OPTION]... SET1 [SET2]
-d Delete specified characters
-s Compressing duplicate characters and converting them to corresponding specified characters
tr 'abc' 'xyz'  //Replace all abc characters with xyz
//Enter aaaaab bbccceedddfff to return and see the effect
tr 'abcd' 'xyz'
tr 'a-z' 'A-Z'  //Down-to-uppercase
tr '1-9' 'A-I'  //Digital transliteration
tr -d 'a-d'  Delete Containment'a-d'All characters
tr 'a-z' 'A-Z' < /etc/issue > f1  tr Commands do not change the contents of source files

tr -s  Repeat successive characters as a single character
#more ab
aaaaaaaaaabbbbbbbbbccccccc
#more ab | tr -s 'abc' 'jkl'
jkl

tr -t  Convert the character corresponding to the first character set to the character corresponding to the second character set, and the number of digits corresponds.
#more ab | tr -t 'abc' 'jkl'
jjjjjjjjjjkkkkkkkkklllllll
#more ab | tr -t 'abc' 'jk'
jjjjjjjjjjkkkkkkkkkccccccc

#cat f1
aaa
bbb
ccc
aeeg
#hexdump f1
#hexdump -c f1
tr '\n' ' ' < f1  //Converting newline characters to space display
tr '\n' '\v' < f1 //Converting newline characters to ladder display
tr '\n' '\t' < f1  //Converting newline characters to tab key display
tr '[ac]' '8' < f1  //Converting ac characters to 8

cat > f1 Single-line redirection, only after return to submit to save to the file, press Ctrl+d leave
//Use < to enter from a directional standard
cat > f2 < f1  copy f1 Content to f2
cat > f1 << eof Multi-line redirection, defining terminators, customizing, defined here as eof
aaa
bbb
cc
eof  //The last line must end with eof and no spaces should be added

cut

Extracting text by column

- d. Specify a delimiter, default to tab
 - f. Specify the number of columns, which can be # or #, # or #-#.
   # Represents fields, [,]: Represents discrete fields, such as - f 1,3,6;
   #-# Continuous multiple fields, such as - f 1-6; can also be mixed: - f 1-3,7.
- c. Cut by character;
-- output-delimiter=string: Specifies the output separator
more passwd |cut -d: -f1,3,4
more passwd |cut -d: -f1-5
more passwd |cut -d: -f1-3,7
more passwd |cut -d: -f1-3,7 --output-delimiter=,
df |cut -c44-46  //centos6.8
df |cut -c39-42  //centos7.3
df| tr -s " "|cut -d" " -f5|tr -dc "[:digit:]\n"
df|tr -s " " "%"|cut -d% -f5
cut -d: -f1 /etc/passwd
cat /etc/passwd|cut -d: -f7
cut -c2-5 /usr/share/dict/words |more
//Intercept IP:
centos6: ifconfig eth0 | head -2|tail -1|cut -d: -f2| cut -d" " -f1
centos7: ifconfig ens33|head -2|tail -1|cut -dt -f2|cut -d" " -f2
         ifconfig ens33|head -2|tail -1|tr -s " "|cut -d" " -f3

wc

Text data statistics wc=word count

wc f1

Line Number Word Number File Name

-l Row number
-w Number of words
-c Number of bytes
-m Number of Characters
wc -l /usr/share/dict/linux.words
479828 /usr/share/dict/linux.words (2017 3 June 2000 centos7.3)

sort

Text Sorting

Display the collated text in stdout by default in alphabetical order without changing the original file

-b Ignore the space character at the beginning of the line
-d When sorting, only English letters, numbers and space characters are processed, while other characters are ignored.
-f Ignore upper and lower case letters
-n Sort by numerical size
-o Save the sorted results to the specified file
-r Reverse Sorting
-h By visual size
-t Specify field separators
-k Sort by a column of a specified delimiter,Ability to use multiple times
-u unique,Delete duplicate lines in output

sort -t: -k3 -n passwd
cut -d: -f1,3 passwd|sort -t: -k2 -nr
df|tr -s " " "%"|cut -d% -f5 |sort -n | tail -1  //Extraction of maximum partition utilization
df -i|tr -s " " "%"|cut -d% -f5 |sort -n | tail -1  //- i Display inode utilization

uniq

Delete overlapping rows from input. Repetition refers to continuous and identical rows.

-c Display the number of repetitions per line
-d Show only duplicated rows
-u Show only rows that have not been repeated
cut -d"" -f 1 /var/log/httpd/access_log |sort|uniq -c|sort -nr
last|cut -d" " -f1|sort |uniq -c
netstat -nt | tr -s " " |cut -d" " -f5|cut -d: -f1|uniq -c
stat -c %a /tmp  Value of permission to extract directories
#Stat/tmp | head-n4 | tail-n1 | cut-d "/" - F1 | cut-d "(" - F2 // / ibid.)
1777

rev

Reverse display, i.e. mirror display, with the beginning and end of each line aligned


colrm

Delete the specified character column

colrm 3  Delete column 3 characters
colrm 2 5  Delete characters from column 2 to column 5

#more ab
123456
123 456
12aa34567
aa bb cc
#more ab | colrm 3
12
12
12
aa
#more ab | colrm 3 5
126
1256
124567
aa cc

paste

Merge file display, default merge per line, do not change the content of the file

-d Specifies a merge separator, default is tab
-s All rows are combined into one row display
paste -d@ f1 f2
paste -s f1 f2

diff

Compare the differences between the two files

- u Outputs Uniform diff Format Files for Patching

#diff foo.conf-broken foo.conf-works
5c5  Note the difference in line 5
< aa Different content
---
> aaa
diff /etc/passwd /etc/passwd.bak

path

Copy changes to files and patch them (use them carefully)

- b Automatic backup of changed files

#diff -u foo.conf-broken foo.conf-works > foo.path
#path -b foo.conf-broken foo.patch


Topics: ascii Linux less