Linux Learning (22) Shell Basics (iii) Special symbols, sort, WC, uniq, tee, TR, split

Source: Internet
Author: User


First, special symbols


*: any of any characters


 
[[email protected] ~]# ls 1*
1bak.zip  1.tar  1.zip.bz2

1:
1.txt  2
[[email protected] ~]# ls 1*2


? : An arbitrary character


 
[[email protected] ~]# ls 2?tar
2.tar


#: Notes


[[email protected] ~]# #ls
[[email protected] ~]# 


\: de-intention


[[email protected] ~]# a=3
[[email protected] ~]# echo $a
3
[[email protected] ~]# echo \$a
$a
[[email protected] ~]# echo "\$a"
$a


|: Pipe Symbol



The function of a pipe symbol is to put the output of the previous command as the standard input for the next command:


[[email protected] ~]# ls|wc -l
21
Second, sort


The sort command is used to sort the.



The default is sorted by the first letter of the ASCII code:


[[email protected] ~]# ls|sort
1
1bak.zip
1.tar
1.zip.bz2
2
2.tar
3
33.tar
3.tar.gz
3.zip
4.tar
4.tar.bip2
4.zip
5
5.zip
6
7.txt
8.txt
999.txt
9.txt
[[email protected] ~]# cat 3.txt | sort

[
$
$
%^
3
33
55
6
6
666
dfd
dfdf
e
gr
kQ$
Q$
Q$
t


Sort by Number:


[[email protected] ~]# sort -n 3.txt

[
$
$
%^
dfd
dfdf
e
gr
kQ$
Q$
Q$
t
3
6
6
33
55
666


 


The experiment found that when sorting by numbers, both letters and special characters were treated as 0.



Reverse


 
[[email protected] ~]# sort -r 3.txt
t
Q$
Q$
kQ$
gr
e
dfdf
dfd
666
6
6
55
33
3
%^
$
$
[


Sort by Segment:


 
[[email protected] ~]# sort -t: -nrk3 666.txt
ruanwenwu:x:1000:1000::/home/ruanwenwu:/bin/bash
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
Third, cut


The Cut command is used for cutting. -d Specifies the delimiter,-f Specifies the number of segments, and-c specifies several characters


[[email protected] ~] # cut -d: -f 3,4 666.txt
0: 0
1: 1
2: 2
3: 4
4: 7
5: 0
6: 0
7: 0
8:12
11: 0
12: 100
14:50
99:99
999: 997
192: 192
81:81
59: 996
59:59
89:89
74:74
1000: 1000
[[email protected] ~] # cut -d: -f 3,4 -c1 666.txt
cut: can only specify one type in the list
Try ‘cut --help’ for more information.


The experiment shows that it is not possible to cut both segments and characters.



Intercept characters:


 
[[email protected] ~]# cut -c 2-4 666.txt
oot
in:
aem
dm:
p:x
ync
Four, WC


The WC command is used to count the number of rows (most commonly used), the number of words and the word count.



Wc-l count rows.



Wc-w count the number of words.



Wc-m or wc-c counts the number of letters. These two commands will count the hidden letters.


[[email protected] ~]# vim 777.txt
[[email protected] ~]# wc -c 777.txt 
19 777.txt
[[email protected] ~]# cat 777.txt 
df dd
cc
dd dd dd

[[email protected] ~]# cat -A 777.txt 
df dd$
cc$
dd dd dd$
$
[[email protected] ~]# wc -m 777.txt 
19 777.txt
[[email protected] ~]# wc -w 777.txt 
6 777.txt
Wu, Uniq


The Uniq command is often used in conjunction with the sort command. Because if two identical rows are not together, they cannot be uniq.


 
[[email protected] ~]# uniq 1.txt
3
4
3
5
6
7
87
8


Let's sort it out and uniq it:


 
[[email protected] ~]# sort 1.txt|uniq
3
4
5
6
7
8
87


Uniq-c shows the number of repetitions per line:


 
[[email protected] ~]# sort 1.txt|uniq -c
      2 3
      1 4
      1 5
      1 6
      1 7
      1 8
      1 87
六、tee
Six, Tee


The tee command takes the standard input, redirects it, and prints the standard input.


 
[[email protected] ~]# cat 1.txt|tee 3.txt
dffdf
[[email protected] ~]# cat 1.txt|tee -a 3.txt
dffdf
[[email protected] ~]# cat 1.txt|tee -a 3.txt
dffdf
[[email protected] ~]# cat 1.txt|tee -a 3.txt
dffdf
[[email protected] ~]# cat 3.txt
dffdf
dffdf
dffdf
dffdf
Seven, tr


The purpose of the TR command is to replace the standard input.


[[email protected] ~] # tr ‘a-z’ ‘A-Z’ ‘sdfsdfsgdgfs’
tr: extra operand "sdfsdfsgdgfs"
Try ‘tr --help’ for more information.
[[email protected] ~] # tr ‘a-z’ ‘A-Z’
dfdfdfdfdfdfd
DFDFDFDFDFDFD
Eight, split


Split is a cut file. Split can be cut by row (split-l), or by size (split-b).



First, let's prepare a large file. Merges all the conf files in the system into the a.txt.


" *.conf " -exec Cat {} >> a.txt \;


Cut by line:


[[email protected] ~] # du -sh a.txt
544K a.txt
[[email protected] ~] # wc -l a.txt
21746 a.txt
[[email protected] ~] # mkdir / tmp / test2
[[email protected] ~] # mv a.txt / tmp / test2 /
[[email protected] ~] # cd! $
cd / tmp / test2 /
[[email protected] test2] # ls
a.txt
[[email protected] test2] # ls -l
Total usage 544
-rw-r--r--. 1 root root 554540 November 18 12:37 a.txt
[[email protected] test2] # wc -l a.txt
21746 a.txt
[[email protected] test2] # wc -l 10000 a.txt
wc: 10000: no such file or directory
  21746 a.txt
  21746 Total dosage
[[email protected] test2] # ls
a.txt
[[email protected] test2] # split -l 10000 a.txt
[[email protected] test2] # ls
a.txt xaa xab xac





Cut by Size:


[[email protected] test2]# !find
find / -type f -name "*.conf" -exec cat {} >> a.txt \;
[[email protected] test2]# ls
a.txt
[[email protected] test2]# split -b 100k a.txt
[[email protected] test2]# ls
a.txt  xaa  xab  xac  xad  xae  xaf


Specify the split file prefix:


[[email protected] test2]# split -b 100k a.txt abc.
[[email protected] test2]# ls
abc.aa  abc.ab  abc.ac  abc.ad  abc.ae  abc.af  a.txt
Nine, special symbols





Note: | | Yes, if the previous command was executed, the following will not be executed.


[[email protected] test2]# [ -d ruanwenwu ] || mkdir ruanwenwu
[[email protected] test2]# ls
abc.aa  abc.ab  abc.ac  abc.ad  abc.ae  abc.af  a.txt  ruanwenwu





Linux Learning (22) Shell Basics (iii) Special symbols, sort, WC, uniq, tee, TR, split


Related Article

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.