`
dalezhu
  • 浏览: 203796 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

linux常用命令

    博客分类:
  • OS
阅读更多
linux常用命令


(Tools of Shell)sort sort -cfile 查看此文件是否是排序过的
$ sort names
Charlie
Emanuel
Fred
Lucy
Ralph
Tony
Tony
$
-u去掉重复的列
$ sort -u names
Charlie
Emanuel
Fred
Lucy
Ralph
Tony
$
-r逆序
$ sort -r names
Tony
Tony
Ralph
Lucy
Fred
Emanuel
Charlie
$
-o 把排序结果写入文件
$ sort names -o sorted_names
$
$ sort names > sorted_names
$
-n表明按照把第一列当成数字,并按照第一列来排序
$ sort data
-5 11
14 -9
15 6
2 12
23 2
3 33
5 27
$ sort -n data
-5 11
2 12
3 33
5 27
14 -9
15 6
23 2
$
跳过第一列进行排序
$ sort +1n data
14 -9
23 2
15 6
-5 11
2 12
5 27
3 33
$

-t按照指定的字符分隔,
$ sort +2n -t: /etc/passwd 第三列进行排序
root:*:0:0:The Super User:/:/usr/bin/ksh
cron:*:1:1:Cron Daemon for periodic tasks:/:
bin:*:3:3:The owner of system files:/:
uucp:*:5:5::/usr/spool/uucppublic:/usr/lib/uucp/uucico
asg:*:6:6:The Owner of Assignable Devices:/:
sysinfo:*:10:10:Access to System Information:/:/usr/bin/sh
george:*:75:75::/users/george:/usr/lib/rsh
steve:*:203:100::/users/steve:/usr/bin/ksh
pat:*:300:300::/users/pat:/usr/bin/ksh
mail:*:301:301::/usr/mail:
$
( 2005年09月19日, 03:10:09 下午 CST ) Permalink 留言 [0]

(Tools of Shell)grep grep '[A-Z]' list Lines from list containing a capital letter

grep '[0-9]' data Lines from data containing a number

grep '[A-Z]...[0-9]' list Lines from list containing five-character patterns that start with a capital letter and end with a digit

grep '\.pic$' filelist Lines from filelist that end in .pic

$ grep shell *
cmdfiles:shell that enables sophisticated
ed.cmd:files, and is independent of the shell.
ed.cmd:to the shell, just type in a q.
grep.cmd:occurrence of the word shell:
grep.cmd:$ grep shell *
grep.cmd:every use of the word shell.
$

-i表示忽略大小写
grep –i 'the' intro
-v表示查找不包含此字符串的行
$ grep -v 'UNIX' intro Print all lines that don't contain UNIX
Thompson and Dennis Ritchie at Bell Laboratories
in the late 1960s. One of the primary goals in
environment that promoted efficient program
development.
$
-l表示列出包含此字符串的文件名 $ grep -l 'Move_history' *.c List the files that contain Move_history
exec.c
makemove.c
testch.c
$
$ grep -l 'Move_history' *.c | wc -l
3
$
-n打印所包含字符串的行数
$ grep -n 'Move_history' testch.c Precede matches with line numbers
13:GLOBAL MOVE Move_history[100] = {0};
197: Move_history[Number_half_moves-1].from = move.from;
198: Move_history[Number_half_moves-1].to = move.to;
$
( 2005年09月19日, 02:18:34 下午 CST ) Permalink 留言 [0]

(Tools of Shell)tr tr from-chars to-chars tr 'X' 'x' Translate all capital X's to small x's tr '()' '{}' Translate all open parens to open braces, all closed parens to closed braces tr '[a-z]' '[A-Z]' Translate all lowercase letters to uppercase ...
Read More
( 2005年09月19日, 01:41:38 下午 CST ) Permalink 留言 [0]

(Tools of Shell)Sed sed '5d' Delete line 5

sed '/[Tt]est/d' Delete all lines containing Test or test

sed -n '20,25p' text Print only lines 20 through 25 from text

sed '1,10s/unix/UNIX/g' intro Change unix to UNIX wherever it appears in the first 10 lines of intro

sed '/jan/s/-1/-5/' Change the first -1 to -5 on all lines containing jan

sed 's/...//' data Delete the first three characters from each line of data

sed 's/...$//' data Delete the last 3 characters from each line of data

sed -n 'l' text Print all lines from text, showing nonprinting characters as \nn (where nn is the octal value of the character), and tab characters as \t


$ cat intro
The Unix operating system was pioneered by Ken
Thompson and Dennis Ritchie at Bell Laboratories
in the late 1960s. One of the primary goals in
the design of the Unix system was to create an
environment that promoted efficient program
development.

$

$ sed 's/Unix/UNIX/' intro Substitute Unix with UNIX

The UNIX operating system was pioneered by Ken
Thompson and Dennis Ritchie at Bell Laboratories
in the late 1960s. One of the primary goals in
the design of the UNIX system was to create an
environment that promoted efficient program
development.
$

$ sed -n '1,2p' intro Just print the first 2 lines

The UNIX operating system was pioneered by Ken
Thompson and Dennis Ritchie at Bell Laboratories

$

$ sed -n '/UNIX/p' intro Just print lines containing UNIX

The UNIX operating system was pioneered by Ken
the design of the UNIX system was to create an

$

$ sed '1,2d' intro Delete lines 1 and 2

in the late 1960s. One of the primary goals in
the design of the UNIX system was to create an
environment that promoted efficient program
development.

$

$ sed '/UNIX/d' intro Delete all lines containing UNIX

Thompson and Dennis Ritchie at Bell Laboratories
in the late 1960s. One of the primary goals in
environment that promoted efficient program
development.

$
( 2005年09月19日, 01:15:04 下午 CST ) Permalink 留言 [0]

(Tools of Shell)Paste $ cat names
Tony
Emanuel
Lucy
Ralph
Fred
$

$ cat numbers
(307) 555-5356
(212) 555-3456
(212) 555-9959
(212) 555-7741
(212) 555-0040
$

$ paste names numbers Paste them together
Tony (307) 555-5356
Emanuel (212) 555-3456
Lucy (212) 555-9959
Ralph (212) 555-7741
Fred (212) 555-0040
$

$ cat addresses
55-23 Vine Street, Miami
39 University Place, New York
17 E. 25th Street, New York
38 Chauncey St., Bensonhurst
17 E. 25th Street, New York

$ paste names addresses numbers
Tony 55-23 Vine Street, Miami (307) 555-5356
Emanuel 39 University Place, New York (212) 555-3456
Lucy 17 E. 25th Street, New York (212) 555-9959
Ralph 38 Chauncey St., Bensonhurst (212) 555-7741
Fred 17 E. 25th Street, New York (212) 555-0040
$

paste -dchars 用chars来做分隔符,第一个字符分隔第一个和第二个字符串,第二个字符分隔第二个和第三个字符串,依此类推.分隔符要用' '号标记
$ paste -d'+' names addresses numbers
Tony+55-23 Vine Street, Miami+(307) 555-5356
Emanuel+39 University Place, New York+(212) 555-3456
Lucy+17 E. 25th Street, New York+(212) 555-9959
Ralph+38 Chauncey St., Bensonhurst+(212) 555-7741
Fred+17 E. 25th Street, New York+(212) 555-0040
$

$ paste -d'+-' names addresses numbers
Tony+55-23 Vine Street, Miami-(307) 555-5356
Emanuel+39 University Place, New York-(212) 555-3456
Lucy+17 E. 25th Street, New York-(212) 555-9959
Ralph+38 Chauncey St., Bensonhurst-(212) 555-7741
Fred+17 E. 25th Street, New York-(212) 555-0040
$

paste -s filename 是把一个文件里的内容打印在一行里

$ paste -s names Paste all lines from names
Tony Emanuel Lucy Ralph Fred
$ ls | paste -d' ' -s - Paste ls's output, use space as delimiter
addresses intro lotsaspaces names numbers phonebook
$
( 2005年09月19日, 10:58:27 上午 CST ) Permalink 留言 [0]


星期五 2005年09月16日
Unix中的正则表达式 Regular Expression Characters Notation Meaning Example Matches . any character a.. a followed by any two characters ^ beginning of line ^wood wood only...
Read More
( 2005年09月16日, 09:45:37 下午 CST ) Permalink 留言 [0]

(Tools of Shell)Cut cut命令
cut的基本语法是 cut -cchars file chars表明要每行要输出的个数,可以用单个数字,比如-c5的话,就表明输出第五个字符,中间可以用,号间隔,比如-c1,2就输出第一个和第二个字符; 如果中间有-符号,就表明要输出一个范围 比如-c1-5,就输出第一个到第五个字符,如果想输出一直到最后一个字符的话,可以忽略最后一个数字,例如 cut -c5- data

举例说明:

$ who
root console Feb 24 08:54
steve tty02 Feb 24 12:55
george tty08 Feb 24 09:15
dawn tty10 Feb 24 15:55
$

$ who | cut -c1-8 Extract the first 8 characters
root
steve
george
dawn
$

$ who | cut -c1-8 | sort
dawn
george
root
steve
$

$ who | cut -c1-8,18-

root Feb 24 08:54
steve Feb 24 12:55
george Feb 24 09:15
dawn Feb 24 15:55
$
The -d and -f Options $ cat /etc/passwd
root:*:0:0:The Super User:/:/usr/bin/ksh
cron:*:1:1:Cron Daemon for periodic tasks:/:
bin:*:3:3:The owner of system files:/:
uucp:*:5:5::/usr/spool/uucp:/usr/lib/uucp/uucico
asg:*:6:6:The Owner of Assignable Devices:/:
steve:*:203:100::/users/steve:/usr/bin/ksh
other:*:4:4:Needed by secure program:/:
$
/etc/passwd 是一个包含本机器所有用户姓名的控制文件.还有包含类似用户编号,根目录,还有这个用户登陆后运行的程序. -d 和 -f表明要输出的字符串包含特殊字符 用法为: cut -ddchar –ffields file dchar表明这个字符是用来分割这个数据的, fields指定输出的位置,比如说-f1,2,8 -f1-3, -f4- 等等 $ cut -d: -f1 /etc/passwd Extract field 1
root
cron
bin
uucp
asg
steve
other
$

$ cut -d: -f1,6 /etc/passwd Extract fields 1 and 6 root:/
cron:/
bin:/
uucp:/usr/spool/uucp
asg:/
steve:/users/steve
other:/
$
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics