unix shell 常用的几个函数

 

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、finger user 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值