tr
(abbreviate transliterate(字译,音译))
syntax tr [options] [string1 string2]
translate character from string1 to the corresponding characters in string2(将一组字符转化成另一组字符)
-d delete the characters in string1 from the standard output.
-s squeeze(紧握,挤) out repeated output characters in string1;(如果有多个相同的字符时,把他们压缩成一个字符)
example:
change all lowcase characters in file1 to uppercase.
cat file1 |tr a-z A-Z
remove all 'a','b','c' character in file1
cat file1|tr -d abc to
change all symbol sign to ' ' and all uppercase to lowercase from file1.txt
$tr '!?":;\[\]{},.\t()\n' ' ' <file1.txt|tr 'a-z' 'A-Z'|tr -s ' '
在这个例子中,不能转义单引号,为了转义怎么办呢?
解决方法是在tr中使用预先设定的字符。tr命令能理解一些字符类,标点灯是其中之一。如下如示
tr命令能理解一些字符类
alnum 字母和数字
alpha 字母
blank 空格(水平的)
cntrl 控制字符
digit 数字
graph 可印刷字符,不包括空格
lower 小写字母
print 可印刷字符,包括空格
punct 标点符号
space 空格(水平和垂直)
upper 大写字母
xdigit 十六进制数字
使用带有这些字符类的tr命令的语法是:
tr ' [:classname:] ' 'set2'
classname是上面给出的类的名字。set2是你希望用来转义的字符。如:
$tr ' [:punct:] ' ' '<file1.txt|tr "a-z" "A-Z"|tr -s ' '
apple中的两个p被减少到了一个p
$tr -s apple
aple
多个空格被减少到一个空格
$tr -s ap ple
ap ple
如果指定了多个字符,替换根据特定的字符来进行。
$echo shell programming|tr -s 'lm'
shel programing
the same result above like this:
$echo shell programming|tr -s 'l m'
shel programing
note: don't omit the single quote sign, if omit, the result like this:
shem programing (attending)
$echo apple shell programming|tr -d 'ar' -s 'mp'
ple shell pogming
$echo apple shell programming|tr -d 'l mp'
aesherograing
wc
syntax wc [options] [files]
print counts of characters,words and lines for files,when multiple files
was listed,statistic for each file output on a seperate line with a
cumulative(累积的) total output last.
parameters:
-c print the character count only
-l print the line count only
-w print the word count only
example:
print "file1 and file2"'s lines,words,characters,and the total.
wc file[12]
sort
sort - sort lines of text files
syntax:
sort [OPTION]... [FILE]...
DESCRIPTION
Write sorted concatenation of all FILE(s) to standard output.
-b, --ignore-leading-blanks ignore leading blanks(先空白,再是数字,最后按字母abc...的顺序排序)都是针对第一个字母
-d, --dictionary-order
consider only blanks and alphanumeric characters(先空白,再是数字,最后按字母abc...的顺序排序)都是针对第一个字母
-f, --ignore-case
fold lower case to upper case characters
-g, --general-numeric-sort
compare according to general numerical value
-i, --ignore-nonprinting
consider only printable characters
-M, --month-sort
compare (unknown) < `JAN' < ... < `DEC'
-n, --numeric-sort
compare according to string numerical value(按数值排序)针对数字在第一列的
-r, --reverse
reverse the result of comparisons(颠倒比较结果)
Other options:
-c, --check
check whether input is sorted; do not sort
-k, --key=POS1[,POS2]
start a key at POS1, end it at POS 2 (origin 1)(这个选项使你具有告诉sort关键字在列项中哪里开始和哪里结束的灵活性)基本语法是sort -k start end file 第一列是1,第二列是2,以此类推
-m, --merge
merge already sorted files; do not sort
-o, --output=FILE
write result to FILE instead of standard output
-s, --stable
stabilize sort by disabling last-resort comparison
-S, --buffer-size=SIZE
use SIZE for main memory buffer
-t, --field-separator=SEP use SEP instead of non- to whitespace transi-
tion
-T, --temporary-directory=DIR
use DIR for temporaries, not $TMPDIR or /tmp multiple options
specify multiple directories
-u, --unique
with -c: check for strict ordering
otherwise: output only the first of an equal run
-z, --zero-terminated
end lines with 0 byte, not newline
--help display this help and exit
--version
output version information and exit
POS is F[.C][OPTS], where F is the field number and C the character
position in the field. OPTS is one or more single-letter ordering
options, which override global ordering options for that key. If no
key is given, use the entire line as the key.
SIZE may be followed by the following multiplicative suffixes: % 1% of
memory, b 1, K 1024 (default), and so on for M, G, T, P, E, Z, Y.
With no FILE, or when FILE is -, read standard input.
example:
将文件2.txt中的行按数值进行排序(从小到大)
$echo -e " peach\n peach\n peach\n apple\n apple\n orange">2.txt
$uniq -c 2.txt|sort -n
1 orange
1 peach
2 apple
2 peach
将文件2.txt中的行按数值进行排序(从大到小)
$uniq -c 2.txt|sort -nr
2 peach
2 apple
1 peach
1 orange
对文件1.txt中第二列的数字进行排序
$printf "apple 90\n orange 30\n banana 34\n kiwi 44\n other 23">1.txt
$sort -k 2,2 1.txt
other 23
orange 30
banana 34
kiwi 44
apple 90
uniq
remove duplicate lines from a sorted file(从排好序的文件中删除重复的行)
syntax
uniq [OPTION]... [INPUT [OUTPUT]]
description
Discard(抛弃,丢弃)all but one of successive identical lines from INPUT (or stan-
dard input), writing to OUTPUT (or standard output).(从(标准)输入中删除所有重复的行,并输出)
frequence using options
-c, --count
prefix lines by the number of occurrences
-d, --repeated
only print duplicate lines
-D, --all-repeated[=delimit-method] print all duplicate lines
delimit-method={none(default),prepend,separate} Delimiting is
done with blank lines.
--help display this help and exit
example:
删除文件中重复的行
$echo -e " peach\n peach\n peach\n apple\n apple\n orange">2.txt
$uniq 2.txt
输出:
peach
apple
orange
计算文件中各个重复行的个数(包括空格)如上例中
$uniq -c 2.txt
3 peach
2 apple
1 orange
1(这是一个空白行)
如果你只想打印那没有重复的行,对重复的行不感兴趣,可用-u
$uniq -u 2.txt
orange
如果你只想打印那有重复的行,对非重复的行不感兴趣,可用-d;重复的行只打印一次.
$uniq -d 2.txt
如果你想打印那些所有的重复行,可用-D,所有重复的行都打印。
$uniq -D 2.txt
|