1. Basic Linux Text Processing command

Source: Internet
Author: User

Liunx Text Processing Basic command is not bypassed by operations engineers, must be familiar with the key commands, they are combined like a door, is the path to the text of the Three Musketeers (Grep,sed,awk) and Shell script programming.


Updated to: 1, Sort 2, tr 3, cut 4, Uniq 5, WC 6, tee 7, paste

1. Sort:
#sort命令主要用于排序, you can sort on the content of the text file in the behavior unit.
Synopsis:sort [OPTION] ... [FILE] ...
Common options: The default is to sort ASCII codes without the option.
-F: Ignore case when sorting;
-N: sorted by numerical size;
-O </path/to/file>: sorted result output file (if you want to modify this file directly, use-O, redirect and Tee command will empty the original file content);
-R: Reverse display;
-T < delimited character;: Specifies the separator character to use when sorting;
-K Choose which interval to sort by;
e.g:
# cat/etc/passwd |sort-n-k3-t:-o/backup/passwd.bak
#以: A delimiter, a third interval, to sort the values and output the results to/backup/passwd.bak
Polkitd:x:997:996:user for Polkitd:/:/sbin/nologin
SYSTEMD-NETWORK:X:998:997:SYSTEMD Network Management:/:/sbin/nologin
SYSTEMD-BUS-PROXY:X:999:998:SYSTEMD Bus Proxy:/:/sbin/nologin
Simpletime:x:1000:1000:simpletime:/home/simpletime:/bin/bash
Allen:x:1001:0::/home/allen:/bin/bash
Nfsnobody:x:65534:65534:anonymous NFS User:/var/lib/nfs:/sbin/nologin


2, TR
#tr命令可以对来自标准输入的字符进行替换, compress, and delete. Strong role.
SYNOPSIS:TR [OPTION] ... SET1 [SET2]
Common options:
-C or--complerment: Replace the SET1 character SET2
-D or--delete: Delete all characters belonging to SET1;
-S or--squeeze-repeats: delete consecutive and repeated characters;
e.g:
# echo "Hello Word" |tr "A-Z" "A-Z"
HELLO WORD

# echo H1e3l3l4o23w323o43r54d6|tr-d "[[:d Igit:]]"
Helloword

# echo H1e3l3l4o23w323o43r54d6|tr-d-C "[[:d Igit:]]"
13342332343546[[email protected] ~]#

# echo H1e3l3l4o23w323o43r54d6|tr-d-C "[[:d igit:]]\n]
13342332343546

# echo H1e3l3l4o23w323o43r54d6|tr-d-C "[[:d igit:]]\n" |tr-s "[1-9]"
134232343546


3. Cut
#cut命令用来显示行中的指定部分, delete the specified field in the file.
Synopsis:cat [OPTION] ... [FILE] ...
Common options:
-D: Specify delimiter
-F: Specify display field range
-C: Display only characters in the specified range
--complement: Reverse Display
e.g:
# Cat passwd|cut-d:-f3-4
89:89
38:38
72:72
1000:1000
1001:0

#cat passwd|cut-d:-f3-4--complement
Postfix:x::/var/spool/postfix:/sbin/nologin
Ntp:x::/etc/ntp:/sbin/nologin
Tcpdump:x::/:/sbin/nologin
Simpletime:x:simpletime:/home/simpletime:/bin/bash
Allen:x::/home/allen:/bin/bash

# Cat Passwd|cut-c2-
Ostfix:x:89:89::/var/spool/postfix:/sbin/nologin
Tp:x:38:38::/etc/ntp:/sbin/nologin
Cpdump:x:72:72::/:/sbin/nologin
Impletime:x:1000:1000:simpletime:/home/simpletime:/bin/bash
Llen:x:1001:0::/home/allen:/bin/bash


4, Uniq
#uniq可以用来去除排序过程中文件的重复行并统计重复行次数, but duplicate rows must be contiguous, so they are often used in combination with sort.
Synopsis:uniq [OPTION] ... [INPUT [OUTPUT]]
Common options:
-I: Ignoring the difference between uppercase and lowercase characters;
-C: to Count;
-U: only display unique rows;
e.g:
# sort Helloword |uniq

Gee
Hello Word

# sort Helloword |uniq-c
1
2 Gee
2 Hello Word

# sort Helloword |uniq-d
Gee
Hello Word

# sort Helloword |uniq-u
Blank Lines #空白行


5. WC
#用于统计文件里有多少个单词, how many lines, how many characters
SYNOPSIS:WC [OPTION] ... [FILE] ...
Common options:
-L: Count rows
-W: Count the number of words
-C: Statistics of characters
e.g:
# WC/ETC/PASSWD
#行 #单词 2323 #字符/etc/passwd
# WC/ETC/PASSWD-LW
88/etc/passwd


6. Tee
#读取标准输入的数据, output the content to a standard output device, and save it as a file
synopsis:tee [OPTION] ... [FILE] ...
Common options:
-A or--append: Append file instead of overwrite
e.g:
# free-m|tee/memory
Total used free shared buff/cache available
mem:977 353 96 13 528 376
swap:2047 0 2047

# Free-m|tee-a Memory |cat Memory
Total used free shared buff/cache available
mem:977 353 96 13 528 376
swap:2047 0 2047
Total used free shared buff/cache available
mem:977 353 96 13 528 376
swap:2047 0 2047


7, paste
#paste指令会把每个文件以列对列的方式, a column of columns is merged
Common options:
-S or--serial: Let each file appear in a single row instead of in a single column.
-D or--delimiters: Specifies the spacer, replacing the default skip character with the specified spacer;
e.g:
# Cat A B C
A1
A2
A3
B1
B2
C1
C3

# paste a b C
A1 B1 C1
A2 B2 C3
A3

# paste a B c-s
A1 A2 A3
B1 B2
C1 C3

# paste a B c-s-D:
A1:a2:a3
B1:b2
C1:c3



This article is from the Linux OPS blog, so be sure to keep this source http://allenyang.blog.51cto.com/10991027/1787996

1. Basic Linux Text Processing command

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.