➜ linux_commands cat test1
hello world!
oh my god!
你是
ttt
fff
gagds
➜ linux_commands wc -c test1
47 test1
➜ linux_commands wc -l test1
8 test1
➜ linux_commands wc -m test1
43 test1
➜ linux_commands wc -w test1
10 test1
➜ linux_commands wc -l test1 hello.txt
8 test1
7 hello.txt
15 total
➜ linux_commands cat hello.txt
hi world
hi boys
she is saying hi
hi hello
HELLO everyone
➜ linux_commands paste -s hello.txt
hi world hi boys she is saying hi hi hello HELLO everyone
➜ linux_commands paste -s -d "#" hello.txt
hi world#hi boys##she is saying hi#hi hello##HELLO everyone
➜ linux_commands paste -s -d "\n" hello.txt (等同于cat hello.txt)
hi world
hi boys
she is saying hi
hi hello
HELLO everyone
➜ linux_commands cat test1
hello world!
oh my god!
你是
ttt
fff
gagds
➜ linux_commands cat test4
this is test4
oh my god
hey man
➜ linux_commands paste test1 test4
hello world! this is test4
oh my god
oh my god! hey man
你是
ttt
fff
gagds
➜ linux_commands paste -d "#" test1 test4
hello world!#this is test4
#
#oh my god
oh my god!#hey man
你是#
ttt#
fff#
gagds#
➜ linux_commands ls | paste - - -
diff.txt hello.txt input.txt
ls.cmd regex.txt test1
test3 test4 test5
test6 test7 tt
ut
➜ linux_commands sed = test1 | paste -s -d '\t\n' - -
1 hello world!
2
3
4 oh my god!
5 你是
6 ttt
7 fff
8 gagds
➜ linux_commands cut -c 3-5
123456 (第一次输入)
345 (切割得345)
qw (第二次输入)
(由于长度<3,输出空)
^C
➜ linux_commands
➜ linux_commands cut -c 3-5 test1
llo
my
t
f
gds
➜ linux_commands cut -c 3- test1
llo world!
my god!
t
f
gds
➜ linux_commands cut -d':' -f2
aa:bb:cc
bb
aa
aa
aa:
^C
➜ linux_commands
➜ linux_commands cut -s -d':' -f2
aa
aa:bb
bb
^C
➜ linux_commands
➜ linux_commands cut -d';' -f2,3
aa;bb;cc;dd
bb;cc
aa
aa
^C
➜ linux_commands
➜ linux_commands cut -b 2-4
asdf
sdf
^C
➜ linux_commands cut -n -b 2-4
晚上去吃饭
晚
^C
➜ linux_commands
➜ linux_commands cat hello.txt
hi world
hi boys
she is saying hi
hi hello
HELLO everyone
➜ linux_commands tr h o < hello.txt
oi world
oi boys
soe is saying oi
oi oello
HELLO everyone
➜ linux_commands tr 'hi' 'oh' < hello.txt <=> tr 'h' 'o' < hello.txt | tr 'i' 'h'
oh world
oh boys
soe hs sayhng oh
oh oello (可以看到是对h和i分别替换)
HELLO everyone
➜ linux_commands tr hi hello < hello.txt
he world (只替换了hi->he)
he boys
she es sayeng he
he hello
HELLO everyone
➜ linux_commands tr boys men < hello.txt
hi werld
hi menn (当boys的长度>men,超出的部分以最后一个字母n补充)
nhe in naning hi
hi helle
HELLO evernene
➜ linux_commands tr -d hi < hello.txt
world
boys
se s sayng
ello
HELLO everyone
➜ linux_commands tr "[:lower:]" "[:upper:]" < hello.txt
HI WORLD
HI BOYS
SHE IS SAYING HI
HI HELLO
HELLO EVERYONE
➜ linux_commands echo "Hello my friend" | tr -s ' ' '\n' (多个空格替换成换行)
Hello
my
friend
➜ linux_commands echo "22aa" | tr -d '[0-9]'
aa
➜ linux_commands echo "22aa" | tr -d -c '[0-9]'
22%
➜ linux_commands cat hello.txt
hi world
hi boys
hello boys
HELLO everyone
she is saying hi
hi boys
➜ linux_commands sort hello.txt
HELLO everyone
hello boys
hi boys
hi boys
hi world
she is saying hi
➜ linux_commands sort -r hello.txt
she is saying hi
hi world
hi boys
hi boys
hello boys
HELLO everyone
➜ linux_commands sort -R hello.txt
she is saying hi
hi boys
hi boys
HELLO everyone
hi world
hello boys
➜ linux_commands sort --ignore-case hello.txt
hello boys
HELLO everyone
hi boys
hi boys
hi world
she is saying hi
➜ linux_commands sort -u hello.txt
HELLO everyone
hello boys
hi boys
hi world
she is saying hi
➜ linux_commands sort -t' ' -k 2 hello.txt (按空格后的字符串排序)
hello boys
hi boys
hi boys
HELLO everyone
she is saying hi
hi world
➜ linux_commands ls -lh | sort -n -k 5
total 376
-rw-r--r-- 1 qiushye staff 10B Apr 9 12:13 input.txt
-rw-r--r-- 1 qiushye staff 23B Apr 30 12:21 regex.txt
-rw-r--r-- 1 qiushye staff 68B May 14 12:34 hello.txt
drwxr-xr-x 4 qiushye staff 128B Apr 4 22:17 ut
-rw-r--r-- 1 qiushye staff 161B Apr 4 22:43 diff.txt
-rw-r--r-- 1 qiushye staff 168K May 15 12:11 commodity.txt (文件最大但数值不是最大)
dr-xr-xrwx 6 eric staff 192B Mar 22 21:24 tt
drwxr-xr-x 8 qiushye staff 256B May 15 12:12 temp
➜ linux_commands ls -lh | sort -h -k 5
total 376
-rw-r--r-- 1 qiushye staff 10B Apr 9 12:13 input.txt
-rw-r--r-- 1 qiushye staff 23B Apr 30 12:21 regex.txt
-rw-r--r-- 1 qiushye staff 68B May 14 12:34 hello.txt
drwxr-xr-x 4 qiushye staff 128B Apr 4 22:17 ut
-rw-r--r-- 1 qiushye staff 161B Apr 4 22:43 diff.txt
dr-xr-xrwx 6 eric staff 192B Mar 22 21:24 tt
drwxr-xr-x 8 qiushye staff 256B May 15 12:12 temp
-rw-r--r-- 1 qiushye staff 168K May 15 12:11 commodity.txt
➜ linux_commands sort -u hello.txt -o hello_sorted.txt
➜ linux_commands cat hello_sorted.txt
HELLO everyone
hello boys
hi boys
hi world
she is saying hi
➜ linux_commands sort -c hello_sorted.txt
➜ linux_commands sort -c hello.txt
sort: hello.txt:2: disorder: hi boys
➜ linux_commands cat hello.txt
hi world
hi boys
hi world
hello boys
HELLO everyone
hi world
hi boys
➜ linux_commands sort hello.txt| uniq
HELLO everyone
hello boys
hi boys
hi world
➜ linux_commands sort hello.txt| uniq -u
HELLO everyone
hello boys
➜ linux_commands sort hello.txt| uniq -d
hi boys
hi world
➜ linux_commands sort hello.txt| uniq -c
1 HELLO everyone
1 hello boys
2 hi boys
3 hi world
➜ linux_commands sort hello.txt| uniq -c | sort -nr
3 hi world
2 hi boys
1 hello boys
1 HELLO everyone
➜ linux_commands cat hello.txt (最后两行新加的)
hi world
hi boys
hi world
hello boys
HELLO everyone
hi world
hi boys
oh boys
Hello everyone
➜ linux_commands sort hello.txt| uniq -i
HELLO everyone
hello boys
hi boys
hi world
➜ linux_commands sort hello.txt| uniq -f 1
HELLO everyone
hello boys
hi world
oh boys
shell命令的参数来源包括标准输入和命令行参数,有些命令支持标准输入,如cat, grep; 但有些命令不支持,只能指定命令行参数, xargs的作用就是将标准输入转化成命令需要的参数。
➜ temp ls | cat
one
test1
test3
test4
test5
test6
test7
three
two
➜ temp ls | echo
➜ temp xargs
a
vb
bbb
a vb bbb
➜ temp xargs echo
a
vb
bbb
a vb bbb
➜ temp echo "one two three" | xargs mkdir
➜ temp ls
one three two
➜ temp echo "aa\nbb" | xargs mkdir
➜ temp ls
aa bb one three two
➜ temp ls | xargs -p echo
echo aa bb one three two?...y
aa bb one three two
➜ temp ls | xargs -t echo
echo aa bb one three two
aa bb one three two
➜ ut ls (ut是目录名)
test2 test3
➜ ut find . -type f -print0 | xargs -0 rm
➜ ut
➜ linux_commands find . -name "hello*" | xargs grep hello
./hello_sorted.txt:hello boys
./hello.txt:hello boys
➜ linux_commands xargs -L 1 find . -name (指定1行作为find . -name的参数)
"*.txt" (第一次输入)
./hh.txt
./regex.txt
./diff.txt
./input.txt
./hello_sorted.txt
./hello.txt
./commodity.txt
"hello*" (第二次输入)
./hello_sorted.txt
./hello.txt
➜ linux_commands echo {0..9} | xargs -n 2 echo
0 1
2 3
4 5
6 7
8 9
➜ temp ls
input.txt three two
➜ temp cat input.txt| xargs -I name sh -c 'echo name; mkdir name' (name作为参数传递)
aa
bb
cc
➜ temp ls
aa bb cc input.txt three two