linux- xargs,tr,cut,sed

版权声明:Summer https://blog.csdn.net/csdnyanglei/article/details/82216140
#xargs命令从stdin处读取一系列参数,然后使用这些参数来执行指定的命令
#xargs应该紧跟在管道操作符之后
$ cat example.txt|xargs
# -n 分成多行,每行3个
$ cat example.txt|xargs -n3
# -d 默认是以空格作为分割符,-d指定了分隔符号
$ cat example.txt|xargs -n3 -dX
$ cat example.txt|xargs ./cecho.sh
# -I{} 指定替换掉的部分
$ cat example.txt|xargs -I{}   ./cecho.sh -p {} -l

#tr,可以对来自标准输入的内容进行字符替换,字符删除(-d),以及重复字符压缩(-s)
$ echo "HELLO WHO IS THIS"|tr 'A-Z' 'a-z'
hello who is this
$ echo "Hello 123 word 456"|tr -d '0-9'
Hello  word 
$ echo "hello 1 char 2 next 4"|tr -d -c '0-9 \n'
 1  2  4
$ echo "hello 1 char 2 next 4"|tr -d -c '0-9'
124
$ echo "hello 1 char 2 next 4"|tr -c '0-9\n' ' '
      1      2      4
$ echo "GNU is    not    UNIX.   Recursive    right    ?"|tr -s ' '
GNU is not UNIX. Recursive right ?


#cut,能够提取指定位置或列之间的字符,将文本以列的方式来操作。
$ cat test6.txt 
No	Name	Mark	Percent
1	Jack	10	15
2	Tom	20	25
3	Hello	30	35

$ cut -f1 test6.txt 
No
1
2
3

$ cut -f2,4 test6.txt 
Name	Percent
Jack	15
Tom	25
Hello	35

$ cut -f3 --complement test6.txt 
No	Name	Percent
1	Jack	15
2	Tom	25
3	Hello	35
# -d设置文本分隔符号
$ cut -f3 -d ";"  --complement test6.txt 

$ cat test7.txt 
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
$ cut -c2-5 test7.txt 
bcde
bcde
bcde
bcde
bcde
$ cut -c2- test7.txt 
bcdefghijklmnopqrstuvwxyz
bcdefghijklmnopqrstuvwxyz
bcdefghijklmnopqrstuvwxyz
bcdefghijklmnopqrstuvwxyz
bcdefghijklmnopqrstuvwxyz
$ cut -c-5 test7.txt 
abcde
abcde
abcde
abcde
abcde


#sed
$ cat test8.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

$ cat test8.txt |cut -d ':' -f1,2,3,4,5
root:x:0:0:root
bin:x:1:1:bin
daemon:x:2:2:daemon

$ cat test8.txt |cut -d ':' -f1,2,3,4,5|sed 's/:/-UUU: /'
root-UUU: x:0:0:root
bin-UUU: x:1:1:bin
daemon-UUU: x:2:2:daemon

$ cat test8.txt |cut -d ':' -f1,2,3,4,5|sed 's/:/-UUU: /g'
root-UUU: x-UUU: 0-UUU: 0-UUU: root
bin-UUU: x-UUU: 1-UUU: 1-UUU: bin
daemon-UUU: x-UUU: 2-UUU: 2-UUU: daemon

。。。
$ cat test9.txt 
root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
#移除空行
$ sed '/^$/d'  test9.txt 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

$ echo This is an example|sed 's/\w\+/[&]/g'
[This] [is] [an] [example]

$ echo abc |sed 's/a/A/' | sed 's/c/C/'
AbC

猜你喜欢

转载自blog.csdn.net/csdnyanglei/article/details/82216140
今日推荐