unix shell 常用的几个函数

来源:互联网 发布:vb.net 中cell.sum 编辑:程序博客网 时间:2024/05/05 12:17
 

1、tee

Copy standard input to each FILE, and also to standard output.

-a, --append   append to the given FILEs, do not overwrite

If a FILE is -, copy again to standard output.

last | tee ./last.last | cut -d " " -f1 | sort | uniq

last | cut -d " " -f1 | sort | uniq | tee last.last  //把结果打印到屏幕上,同时写到文件last.last中。

2、dos2unix,unix2dos

The Dos2unix package includes utilities "dos2unix" and "unix2dos" to convert plain text files in DOS or MAC format to UNIX format and vice versa.

主要工作是转换换行符不同。

winodw:CRLF(\r\n)

linux:LF(\n)

MAC:CR

3、col

filter reverse line feeds from input

cat -A test //显示所有标记

^I tab

^M CRLF

1)说明

col [-xb]

选项与参数:

-x:将 tab键转换成对等的空白键

-b:在文字内有反斜线 (/)时,仅保留反斜线最后接的那个字符

2)示例

cat prime.cpp | col -x | cat -A | more

4、join

join lines of two files on a common field

join [OPTION]... FILE1 FILE2

join [-ti12] file1 file2

选项与参数:

-t:join预设以空格符分隔数据,并且比对『第一个字段』的数据,如果两个字段相同,则将两笔资料(行)联成一行,且第一个字段放在第一个!

-i:忽略大小写的差异;

-1:这个是数字的 1,代表『第一个文件要用那个字段来分析』的意思;

-2:代表『第二个档案要用那个字段来分析』的意思。

join -t ':' /etc/passwd /etc/shadow

join -t ':' -1 4 /etc/passwd -2 3 /etc/group

5、paste

paste [-d] file1 file2

选项与参数:

-d  :后面可以接分隔字符。预设是以 [tab]来分隔的!

-   :如果 file部分写成 -,表示来自 standard input数据

6、fingeruser information lookup program

     finger [-option] [user ...]

     finger [-l] [user@host ...]

7、参数代换,xargs

build and execute command lines from standard input

xargs 可以读入 stdin数据,并且以空格符或断行字符作为分辨,将stdin 数据分隔成为arguments

1)说明

xargs [-0epn] command

選項與參數:

-0  :如果输入的 stdin含有特殊字符,例如 `, \,空白键等等字符时,这个 -0参数可以将他还原成一般字符。

-e  :这个是 EOF (end of file)的意思。后面可以接一个字符串,当 xargs分析到这个字符串时,就会停止继续工作!

-p  :在执行每个指令的 argument时,都会询问使用者的意思;

-n  :后面接次数,每次 command指令执行时,要使用几个参数的意思。

xargs后面没有接任何的指令时,预设是以 echo来进行输出!

2)示例

cut -d ":" -f1 /etc/passwd | tail -n 5 | xargs finger

3)使用xargs 的原因是, 很多指令其实并不支持管道命令,因此我们可以透过xargs 来提供该指令引用standard input 之用

find ./ -name prime.cpp | ls -l //输出并不是我们所想,因为ls不支持管道

find ./ -name prime.cpp | xargs ls -l 

参考

1http://linux.vbird.org/linux_basic/0320bash.php

原创粉丝点击