学习Linux命令之 expand split xargs


expand


# expand [-t] file

        将 [tab] 按键转成空格键

选项与参数:

-t     :后面可以接数字。一般来说,一个 tab 按键可以用 8 个空格键取代。


root@localhost:~/shell# cat file1

John Dagget341 King RoadPlymouth

Alice Ford22 East BroadwayRicahmond

root@localhost:~/shell# expand -t 4 file1

John Dagget 341 King Road   Plymouth

Alice Ford  22 East Broadway    Ricahmond

root@localhost:~/shell# expand -t 8 file1

John Dagget     341 King Road   Plymouth

Alice Ford      22 East Broadway        Ricahmond

root@localhost:~/shell# expand -t 10 file1

John Dagget         341 King Road       Plymouth

Alice Ford          22 East Broadway    Ricahmond



split

        

[root@www ~]# split [-bl] file PREFIX

        依据文件大小或行数来分割,就可以将大文件分割成为小文件

选项与参数:

-b :后面可接欲分割成的文件大小,可加单位,例如 b, k, m 等;

-l :以行数来进行分割。

PREFIX :代表前导符的意思,可作为分割文件的前导文字。


root@localhost:~/shell# ll awk 

-rw-r--r-- 1 root Ricardo 40539  5月  8 20:30 awk

root@localhost:~/shell# split -b 15k awk awk  〈--前导文字

root@localhost:~/shell# ll awk*

-rw-r--r-- 1 root Ricardo 40539  5月  8 20:30 awk

-rw-r--r-- 1 root Ricardo 15360  5月  8 20:33 awkaa

-rw-r--r-- 1 root Ricardo 15360  5月  8 20:33 awkab

-rw-r--r-- 1 root Ricardo  9819  5月  8 20:33 awkac


root@localhost:~/shell# split -l 400 awk ssk

root@localhost:~/shell# ll ssk*

-rw-r--r-- 1 root Ricardo 17625  5月  8 20:34 sskaa

-rw-r--r-- 1 root Ricardo 18095  5月  8 20:34 sskab

-rw-r--r-- 1 root Ricardo  4819  5月  8 20:34 sskac


xargs

1. 简介

之所以能用到这个命令,关键是由于很多命令不支持|管道来传递参数,而日常工作中有有这个必要,

所以就有了 xargs 命令,例如:

find /sbin -perm +700 |ls -l

这个命令是错误的

find /sbin -perm +700 |xargs ls -l 这样才是正确的

xargs 可以读入 stdin 的资料,并且以空白字元或断行字元作为分辨,将 stdin 的资料分隔成为 arguments。 因为是以空白字元作为分隔,所以,如果有一些档名或者是其他意义的名词内含有空白字元的时候, xargs 可能就会误判。


2. 选项解释


-0 当 sdtin 含有特殊字元时候,将其当成一般字符,想/'空格等

root@localhost:~/shell# echo "\\"

\

root@localhost:~/shell# echo "\\" | xargs echo



root@localhost:~/shell# echo "\\" | xargs -0 echo

\



-a file 从文件中读入作为 sdtin


root@localhost:~/shell# cat test

John Dagget, 341 King Road, Plymouth, Massachusetts

Alice Ford, 22 East Broadway, Ricahmond, Virginia

root@localhost:~/shell# xargs -a test echo

John Dagget, 341 King Road, Plymouth, Massachusetts Alice Ford, 22 East Broadway, Ricahmond, Virginia


-E flag ,注意有的时候可能会是-e flag 必须是一个以空格分隔的标志,当 xargs 分析到含有 flag 这个

标志的时候就停止。


root@localhost:~/shell# cat test | xargs -E 'King' echo

John Dagget, 341


-p 当每次执行一个 argument 的时候询问一次用户。


root@localhost:~/shell# cat test | xargs -p echo

echo John Dagget, 341 King Road, Plymouth, Massachusetts ?...

echo Alice Ford, 22 East Broadway, Ricahmond, Virginia ?...


-n num 后面加次数,表示命令在执行的时候一次用的 argument 的个数,默认是用所有的。


root@localhost:~/shell# cat help 

test

yes 

no 

sd 

df

root@localhost:~/shell# cat help | xargs -i -n2 echo 

test yes

no sd

df


-t 表示先打印命令,然后再执行。


root@localhost:~/shell# cat help | xargs -t -i -n2 echo 

echo test yes 

test yes

echo no sd 

no sd

echo df 

df


-i 或者是-I,这得看 linux 支持了,将 xargs 的每项名称,一般是一行一行赋值给{},可以用{}代替。


root@localhost:~/shell# ls test*

test

root@localhost:~/shell# ls test| xargs -t -i mv {} {}.bak

mv test test.bak 

root@localhost:~/shell# ls tes*

test.bak


-d delim 分隔符,默认的 xargs 分隔符是回车,argument 的分隔符是空格,这里修改的是 xargs 的分隔


root@localhost:~/shell# cat help 

add rs rc sds

root@localhost:~/shell# cat help | xargs -i -p -d " " echo {}

echo add ?...y

echo rs ?...add

y

echo rc ?...rs

y

echo sds

 ?...rc

y

sds



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章