3.文件查看及内容处理

1.catnode

功能:链接文件和标准输出打印linux

语法:cat [OPTION]... [FILE]...sql

经常使用选项:docker

-A:查看全部,至关于-vET选项,能够列出特殊字符shell

-b:打印非空行行号vim

-E:显示结尾的断行字节$windows

-n:打印全部行号centos

-T:将tab键以^I显示出来bash

-v:显示非打印字符,也就是特殊字符oracle

示例: 

[root@localhost tmp]# cat /etc/issue
CentOS release 6.6 (Final)
Kernel \r on an \m

[root@localhost tmp]# tac /etc/issue

Kernel \r on an \m
CentOS release 6.6 (Final)
说明:cat跟tac打印效果不同,一个是正序显示,一个是倒叙显示
同时打印输出两个文件内容:
[root@localhost ~]# cat test1 test2
welcome to shell world
this is centos 6.6
[root@localhost ~]# cat test1 
welcome to shell world
[root@localhost ~]# cat test2
this is centos 6.6
新建文件并添加内容:
[root@localhost ~]# cat > 1 写完按ctrl+c结束保存
1 2 3 4
^C
[root@localhost ~]# cat 1
1 2 3 4
[root@localhost ~]# cat > 2 << EOF 这样写是当输入完成时,输入EOF就是结束文档添加内容,并保存
> 1234
> this is
> EOF

2. tac

功能:链接文件和倒序打印文件

语法: tac [OPTION]... [FILE]...

经常使用选项

跟cat差很少

示例上面已经有了


3.more

功能:crt模式看文件浏览过滤
语法:more [-dlfpcsu] [-num] [+/pattern] [+linenum] [file ...]
用法:
须要按键操做浏览过程
空白键 (space):向下翻一页;
Enter:向下翻一行;
/字串:在这个显示的内容当中,向下搜寻字串这个关键字;
f:快速翻页
q:退出more浏览
说明:more虽然能够浏览比cat更多的文件内容,但有缺点,不能往上翻页,只能往下翻页,直到结束。

4.less

功能:更多显示跟浏览
按键操做:
空白键:向下翻动一页;
[pagedown]:向下翻动一页;
[pageup]:向上翻动一页;
/字串:向下搜寻字串的功能;
?字串:向上搜寻字串的功能;
n:重复前一个搜寻 (与 / 或 ? 有关!)
N:反向的重复前一个搜寻 (与 / 或 ? 有关!)
q:对出less浏览

5.head

功能:输出文件的开头部分

语法:head [OPTION]... [FILE]...
经常使用选项:
-n:接数字,显示到前几行
示例:
不接参数默认显示前10行

[root@localhost ~]# head /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
[root@localhost ~]# head /etc/passwd|wc -l
10
[root@localhost ~]# head -n 3 /etc/passwd  #打印前三行
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@localhost ~]# head -3 /etc/passwd #直接带数字也是能够的
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin


6.tail

功能:输出文件的最后部分

语法:tail [OPTION]... [FILE]...

经常使用选项:

-n:接数字,从最后开始显示到几行

-f:动态打印文件信息,后边接数字,ctrl+c结束

示例:默认显示后10行 

[root@localhost ~]# tail  /etc/passwd
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
yyl:x:500:500::/home/yyl:/bin/bash
[root@localhost ~]# tail -n 1 /etc/passwd
yyl:x:500:500::/home/yyl:/bin/bash
[root@localhost ~]# tail -1 /etc/passwd
yyl:x:500:500::/home/yyl:/bin/bash

7.cut

功能:截取命令
语法:cut OPTION... [FILE]...
经常使用选项:
-b 以字节为单位进行分割
-c:以字符为单位进行分割
-d:自定义分隔符,默认为制表符
-f:与-d一块儿使用,指定显示那个区域
-n:取消分割多字节字符,和-b一块儿使用

[root@localhost test]# who
root     pts/1        2017-05-26 18:36 (192.168.19.1)
root     pts/2        2017-05-27 09:32 (192.168.19.1)
[root@localhost test]# who |cut -b 3 #以字节分隔,显示第三个字节
o
o
[root@localhost test]# who |cut -c 3 #以字符分隔,显示第三个字符,每一个字母便是字节,又是字符
o
o
[root@localhost test]# who |cut -d" " -f 1
root
root
[root@localhost test]# who |cut -n -b 1-4
root
root
[root@localhost test]# ifconfig eth1 |grep Bcast|cut -d":" -f 2 |cut -d" " -f 1 #生产示例,分割打印ip地址
192.168.19.20

8.split
功能:切割文件
语法: split [OPTION]... [INPUT [PREFIX]]
经常使用选项:
-<行数>或-l<行数>  指定每多少行就要切成一个小文件。
-b<字节>  指定每多少字就要切成一个小文件。支持单位:m,k
-C<字节>  与-b参数相似,但切割时尽可能维持每行的完整性。
-d 以数字为后缀
-a length 指定数字后缀的长度
--help  显示帮助。
示例:

[root@localhost test]# cat split1
this is line1
this is line2
this is line3
this is line4
this is line5
this is line6
按每一个文件1行分割,并按字母顺序命名文件
[root@localhost test]# split -1 split1
[root@localhost test]# ll
total 36
-rw-r--r-- 1 root root  84 May 27 14:35 split1
-rw-r--r-- 1 root root  14 May 27 14:35 xaa
-rw-r--r-- 1 root root  14 May 27 14:35 xab
-rw-r--r-- 1 root root  14 May 27 14:35 xac
-rw-r--r-- 1 root root  14 May 27 14:35 xad
-rw-r--r-- 1 root root  14 May 27 14:35 xae
-rw-r--r-- 1 root root  14 May 27 14:35 xaf
[root@localhost test]# cat xaa
this is line1
[root@localhost test]# cat xab
this is line2

9.paste

功能:合并文件
语法:
经常使用选项:
经常使用选项:
-d  指定分隔符,默认是tab键
-s  将文件内容平行,tab键分隔
示例:

[root@localhost test]# cat name.txt town.txt
M.Golls 12 Hidd Rd
P.Heller The Acre
P.Willey 132 The Grove
T.Norms 84 Connaught Rd
K.Fletch 12 Woodlea
M.Golls Norwich NRD
P.Willey Galashiels GDD
T.Norms Brandon BSL
K.Fletch Mildenhall MAF
K.Firt Mitryl Mdt
[root@localhost test]# paste name.txt town.txt  #跟join有区别,join是相同的只显示一个,最paste所有显示
M.Golls 12 Hidd Rd M.Golls Norwich NRD
P.Heller The Acre P.Willey Galashiels GDD
P.Willey 132 The Grove T.Norms Brandon BSL
T.Norms 84 Connaught Rd K.Fletch Mildenhall MAF
K.Fletch 12 Woodlea K.Firt Mitryl Mdt
[root@localhost test]# paste -s name.txt town.txt  #一行显示了
M.Golls 12 Hidd Rd P.Heller The Acre P.Willey 132 The Grove T.Norms 84 Connaught Rd K.Fletch 12 Woodlea
M.Golls Norwich NRD P.Willey Galashiels GDD T.Norms Brandon BSL K.Fletch Mildenhall MAF K.Firt Mitryl Mdt
[root@localhost test]# paste -d"\n" name.txt town.txt  合并并换行显示
M.Golls 12 Hidd Rd
M.Golls Norwich NRD
P.Heller The Acre
P.Willey Galashiels GDD
P.Willey 132 The Grove
T.Norms Brandon BSL
T.Norms 84 Connaught Rd
K.Fletch Mildenhall MAF
K.Fletch 12 Woodlea
K.Firt Mitryl Mdt


10.sort

功能:文本文件排序
语法:sort [OPTION]... [FILE]...
          sort [OPTION]... --files0-from=F
经常使用选项:
-b 忽略每行前面开始的空格字符。
-c 检查文件是否已按顺序排序。
-d 排序时,处理英文字母、数字及空格字符外,忽略其余的字符
-f 忽略大小写
-M 根据月份比较排序,如:DEC FEb
-h  单位换算,也叫人性化显示
-n 数字比较排序
-o 将结果输出到文件
-t 指定分隔符
-k n,m 根据关键字排序,从第n字段开始,m字段结束
-r 倒序排序
-u 去重复行
-T 指定临时文件目录,默认在/tmp+<起始栏位>-<结束栏位>  # 以指定的栏位来排序,范围由起始栏位到结束栏位的前一栏位。
默认是对整列排序,依照ASCII值比较
示例:

[root@localhost test]# cat seq.txt
banana
apple
pear
orange
[root@localhost test]# sort seq.txt
apple
banana
orange
pear
[root@localhost test]# seq 5 |shuf
3
2
4
5
1
[root@localhost test]# seq 5 |shuf|sort
1
2
3
4
5
-t -k -n选项
[root@localhost scripts]# cat oldboy.txt
48 Oct 3bc1997 lpas 68.00 lvx2a 138
484 Jan 380sdf1 usp 78.00 deiv 344
483 nov 7pl1998 usp 37.00 kvm9d 644
320 aug der9393 psh 83.00 wiel 293
231 jul sdf9dsf sdfs 99.00 werl 223
230 nov 19dfd9d abd 87.00 sdiv 230
219 sept 5ap1996 usp 65.00 lvx2c 189
216 Sept 3zl1998 usp 86.00 kvm9e 234
[root@localhost scripts]# sort -t " " -k 5 -n oldboy.txt
483 nov 7pl1998 usp 37.00 kvm9d 644
219 sept 5ap1996 usp 65.00 lvx2c 189
48 Oct 3bc1997 lpas 68.00 lvx2a 138
484 Jan 380sdf1 usp 78.00 deiv 344
320 aug der9393 psh 83.00 wiel 293
216 Sept 3zl1998 usp 86.00 kvm9e 234
230 nov 19dfd9d abd 87.00 sdiv 230
231 jul sdf9dsf sdfs 99.00 werl 223
-r选项
[root@localhost scripts]# seq 5 |sort -r
5
4
3
2
1
-u选项
[root@localhost test]# cat seq.txt
banana
apple
pear
orange
pear
orange
[root@localhost test]# sort -u seq.txt #去重复并排序
apple
banana
orange
pear
-0选项
[root@localhost test]# sort -r seq.txt  >> seq1.txt
[root@localhost test]# cat seq1.txt
pear
pear
orange
orange
banana
apple
[root@localhost test]# sort -u seq.txt -o seq2.txt
[root@localhost test]# cat seq2.txt
apple
banana
orange
pear
-n选项
sort排序有时候回到10比2小的状况,这是由于怕排序先比较1和2,显然1小,就将10放在2的前面了。
[root@localhost test]# cat number.txt
1
10
19
11
2
5
[root@localhost test]# sort number.txt  #按字符排序
1
10
11
19
2
5
[root@localhost test]# sort -n number.txt
1
2
5
10
11
19
sort -n                          # 按数字排序
sort -nr                         # 按数字倒叙
sort -u                          # 过滤重复行
sort -m a.txt c.txt              # 将两个文件内容整合到一块儿
sort -n -t' ' -k 2 -k 3 a.txt    # 第二域相同,将从第三域进行升降处理
sort -n -t':' -k 3r a.txt        # 以:为分割域的第三域进行倒叙排列
sort -k 1.3 a.txt                # 从第三个字母起进行排序
sort -t" " -k 2n -u  a.txt       # 以第二域进行排序,若是遇到重复的,就删除

11.uniq

功能:去除重复行
经常使用选项:
-c  打印出现的次数,只能统计相邻的
-d  只打印重复行
-u   只打印不重复行
-D  只打印重复行,而且把全部重复行打印出来
-f n 忽略第n个字段
-i  忽略大小写
-s n 忽略前N个字符
-w   比较不超过前N个字符
示例:

[root@localhost test]# cat seq.txt
banana
apple
pear
orange
pear
orange
[root@localhost test]# sort seq.txt
apple
banana
orange
orange
pear
pear
[root@localhost test]# sort seq.txt|uniq
apple
banana
orange
pear
[root@localhost test]# sort seq.txt|uniq -c  #打印重复的次数
      1 apple
      1 banana
      2 orange
      2 pear
[root@localhost test]# sort seq.txt|uniq  -u #打印不重复行
apple
banana
[root@localhost test]# sort seq.txt|uniq  -d  #打印重复行
orange
pear
[root@localhost test]# sort seq.txt|uniq  -d -c  #打印重复行并出现的次数
      2 orange
      2 pear
[root@localhost test]# sort seq.txt|uniq  -w 1  #根据字符去重
apple
banana
orange
pear


12.wc

功能:统计文件行数、字节、字符数
经常使用选项:
-c  打印文件字节数
-m  打印文件字符数
-l  打印多少行
示例:

[root@localhost test]# wc -l /etc/passwd
22 /etc/passwd
[root@localhost test]# wc -c /etc/passwd
973 /etc/passwd
[root@localhost test]# wc -m /etc/passwd
973 /etc/passwd

13.iconv

功能:将文件内容字符集转成其余字符集

经常使用选项:

-l  列出全部已知的编码字符集

-f  编码原始文本

-t  输出的编码格式

-o  输出到文件

-s  不输出警告

示例: 

将文件内容转换UTF8:
# iconv -f gbk -t utf8 old.txt -o new.txt
将csv文件转换GBK:
# iconv -f utf8 -t gbk old.txt -o new.txt
解决邮件乱码:
# echo $(echo "content" | iconv -f utf8 -t gbk) | mail -s "$(echo "title" | iconv -f utf8 -t gbk)" dst@163.com


14.dos2unix/unix2dos

功能:dos2unix是将Windows格式文件转换为Unix、Linux格式的实用命令。Windows格式文件的换行符为\r\n ,而Unix&Linux文件的换行符为\n. dos2unix命令其实就是将文件中的\r\n 转换为\n。而unix2dos则是和dos2unix互为孪生的一个命令,它是将Linux&Unix格式文件转换为Windows格式文件的命令。
语法格式:dos2unix [options] [-c convmode] [-o file ...] [-n infile outfile ...]unix2dos [options] [-c convmode] [-o file ...] [-n infile outfile ...]

此命令参数是Red Hat Enterprise Linux Server release 5.7下dos2unix命令参数,不一样版本Linux的dos2nnix命令参数有可能不一样。

参数

长参数

描叙

-h


显示命令dos2unix联机帮助信息。

-k


保持文件时间戳不变

-q


静默模式,不输出转换结果信息等

-V


显示命令版本信息

-c


转换模式

-o


在源文件转换,默认参数

-n


保留本来的旧档,将转换后的内容输出到新档案.默认都会直接在原来的文件上修改,

示例: 

1: 查看dos2unix命令的帮助信息
[root@DB-Server myscript]# man dos2unix
[root@DB-Server myscript]# dos2unix -h
dos2unix Copyright (c) 1994-1995 Benjamin Lin
        Copyright (c) 1998      Bernd Johannes Wuebben (Version 3.0)
        Copyright (c) 1998      Christian Wurll (Version 3.1)
Usage: dos2unix [-hkqV] [-c convmode] [-o file ...] [-n infile outfile ...]
-h --help        give this help
-k --keepdate    keep output file date
-q --quiet      quiet mode, suppress all warnings
                  always on in stdin->stdout mode
-V --version    display version number
-c --convmode    conversion mode
convmode        ASCII, 7bit, ISO, Mac, default to ASCII
-l --newline    add additional newline in all but Mac convmode
-o --oldfile    write to old file
file ...        files to convert in old file mode
-n --newfile    write to new file
infile          original file in new file mode
outfile          output file in new file mode

2: dos2unix filename 将Windows格式文本转换为Unix&Linux格式文件
  1: [root@DB-Server myscript]# cat -v test.sh
  2: . /home/oracle/.bash_profile^M
  3: echo ' '^M
  4: date^M
  5: echo ' '^M
  6: ^M
  7: sqlplus test/test @/home/oracle/scripts/test.sql^M
  8: ^M
  9: echo ' '^M
  10: date^M
  11: echo ' '^M
  12: [root@DB-Server myscript]# dos2unix test.sh
  13: dos2unix: converting file test.sh to UNIX format ...
  14: [root@DB-Server myscript]# cat -v test.sh
  15: . /home/oracle/.bash_profile
  16: echo ' '
  17: date
  18: echo ' '
  19:
  20: sqlplus test/test @/home/oracle/scripts/test.sql
  21:
  22: echo ' '
  23: date
  24: echo ' '

3: dos2unix 能够一次转换多个文件
  1: dos2unix filename1 filename2 filename3

4: 默认状况下会在源文件上进行转换,若是须要保留源文件,那么可使用参数-n dos2unix -n oldfilename newfilename
  1: [root@DB-Server myscript]# dos2unix -n dosfile linuxfile
  2: dos2unix: converting file dosfile to file linuxfile in UNIX format ...
  3: [root@DB-Server myscript]# cat -v dosfile
  4: it is a windows dos file^M
  5: you should convert to unix&linux format^M
  6: [root@DB-Server myscript]# cat -v linuxfile
  7: it is a windows dos file
  8: you should convert to unix&linux format
  9: [root@DB-Server myscript]#
clip_image001

5:保持文件时间戳不变
  1: [root@DB-Server myscript]# ls -lrt dosfile
  2: -rw-r--r-- 1 root root 67 Dec 26 11:46 dosfile
  3: [root@DB-Server myscript]# dos2unix dosfile
  4: dos2unix: converting file dosfile to UNIX format ...
  5: [root@DB-Server myscript]# ls -lrt dosfile
  6: -rw-r--r-- 1 root root 65 Dec 26 11:58 dosfile
  7: [root@DB-Server myscript]# dos2unix -k dosfile
  8: dos2unix: converting file dosfile to UNIX format ...
  9: [root@DB-Server myscript]# ls -lrt dosfile
  10: -rw-r--r-- 1 root root 65 Dec 26 11:58 dosfile

6:静默模式格式化文件
  1: [root@DB-Server myscript]# unix2dos -q dosfile
  3: [root@DB-Server myscript]#
  
dos2unix的下载地址为http://sourceforge.net/projects/dos2unix/ ,能够从上面下载最新版本的dos2unix、unix2dos等命令工具以及相关文档


15.diff
功能:逐行比较两个文本文件,列出其不一样之处

语法格式:diff [options] file1 file2

经常使用选项:

-a:将全部文件看成文本文件来处理。

-b或–ignore-space-change  忽略空格形成的不一样。

-B或–ignore-blank-lines  忽略空行形成的不一样。

-c:使用纲要输出格式。

-H:利用试探法加速对大文件的搜索。

-I:忽略大小写的变化。

-n –rcs:输出RCS格式。

-N或–new-file  在比较目录时,若文件A仅出如今某个目录中,会显示:Only in目录;文件A若使用-N参数,则diff会将文件A与一个空白的文件比较。

-r或–recursive  比较子目录中的文件。

-u,-U<列数>或–unified=<列数>  以合并的方式来显示文件内容的不一样。
示例: 

diff /usr/xu mine
把目录/usr/xu 中名为mine的文件与当前目录中的mine文件进行比较。
[root@localhost scripts]# cat 1.txt
1
2
3
4
5
6
7
8
9
10
[root@localhost scripts]# cat 2.txt
7
8
9
10
11
12
13
14
15
[root@localhost scripts]# diff 1.txt 2.txt
1,6d0
< 1
< 2
< 3
< 4
< 5
< 6
10a5,9
> 11
> 12
> 13
> 14
> 15

16.vimdiff

功能:比较两个及以上文件的差别

语法格式:vimdiff [options] file1 file2 [file3 [file4]]

示例:

[root@localhost scripts]# vimdiff 1.txt 2.txt
2 files to edit

2146ebf9-4211-442e-9848-d38c989ecc2d.png 

图形化比较文件跟vim -o 1.txt 2.txt相似

4.17.rev

功能:反向打印文件的每一行
经常使用选项:
示例:

[root@localhost ~]# cat 123
123
my name is yyl
[root@localhost ~]# tac 123 #上下反转
my name is yyl
123
[root@localhost ~]# rev 123 #:左右反转
321
lyy si eman ym


18.join

功能:链接两个文件

语法: join [OPTION]... FILE1 FILE2

经常使用选项:

-a <1或2> 除显示原来输出的内容外,还显示指定文件中没有相同的栏位,默认不显示

-i  忽略大小写

-o  按照指定文件栏位显示

-t  使用字符做为输入和输出字段分隔符

-1  链接文件1的指定栏位

-2  链接文件2的指定栏位

示例: 

[root@localhost test]# cat name.txt town.txt  #一个是名字和街道地址,一个是名字和城镇
M.Golls 12 Hidd Rd
P.Heller The Acre
P.Willey 132 The Grove
T.Norms 84 Connaught Rd
K.Fletch 12 Woodlea
M.Golls Norwich NRD
P.Willey Galashiels GDD
T.Norms Brandon BSL
K.Fletch Mildenhall MAF
K.Firt Mitryl Mdt
[root@localhost test]# join name.txt town.txt  #合并两个文件
M.Golls 12 Hidd Rd Norwich NRD
P.Willey 132 The Grove Galashiels GDD
join: file 1 is not in sorted order
join: file 2 is not in sorted order
T.Norms 84 Connaught Rd Brandon BSL
K.Fletch 12 Woodlea Mildenhall MAF


19.tr

功能:替换或删除字符,能够说是sed的简化

命令格式:tr [OPTION]... SET1 [SET2]

经常使用选项:

-c  保留SET1的字符,其余都替换为SET2,字符为ASCII

-d  删除SET1中全部字符

-s  删除SET1中重复出现的字符

-t  将SET1用SET2转换,默认

字符范围指定set1或set2的内容时,只能使用单字符或字符串范围或列表。

[a-z] a-z内的字符组成的字符串。

[A-Z] A-Z内的字符组成的字符串。

[0-9] 数字串。

\octal 一个三位的八进制数,对应有效的ASCII字符。

[O*n] 表示字符O重复出现指定次数n。所以[O*2]匹配OO的字符串。

tr中特定控制字符的不一样表达方式

速记符含义八进制方式

\a Ctrl-G  铃声\007

\b Ctrl-H  退格符\010

\f Ctrl-L  走行换页\014

\n Ctrl-J  新行\012

\r Ctrl-M  回车\015

\t Ctrl-I  tab键\011

\v Ctrl-X  \030

示例: 

[root@localhost test]# cat xaa |tr -c s 2  #除过s之外的全部字符串空格都替换为2了
222s22s2222222
[root@localhost test]# cat xaa |tr -d s  #删除了s字符再打印
thi i line1
[root@localhost test]# echo 111111222223333565656 |tr -s '[0-9]' #只有相邻重复的才会删除
123565656
[root@localhost test]# echo 111111222223333565656 |tr -s '[0-9]' '[a-z]'  #去重复在替换
bcdfgfgfg
[root@localhost test]# echo 111111222223333565656 |tr -t '[0-9]' '[a-z]'
bbbbbbcccccddddfgfgfg
其余示例:
一、将文件file中出现的"abc"替换为"xyz":
# cat file | tr "abc" "xyz" > new_file
【注意】这里,凡是在file中出现的"a"字母,都替换成"x"字母,"b"字母替换为"y"字母,"c"字母替换为"z"字母。而不是将字符串"abc"替换为字符串"xyz"。
二、使用tr命令“统一”字母大小写
(小写 --> 大写)
# cat file | tr [a-z] [A-Z] > new_file
(大写 --> 小写)
# cat file | tr [A-Z] [a-z] > new_file
三、把文件中的数字0-9替换为a-j
# cat file | tr [0-9] [a-j] > new_file
四、删除文件file中出现的"Snail"字符
# cat file | tr -d "Snail" > new_file
【注意】这里,凡是在file文件中出现的'S','n','a','i','l'字符都会被删除!而不是牢牢删除出现的"Snail”字符串。
五、删除文件file中出现的换行'\n'、制表'\t'字符
# cat file | tr -d "\n\t" > new_file
不可见字符都得用转义字符来表示的,这个都是统一的。
六、删除“连续着的”重复字母,只保留第一个
# cat file | tr -s [a-zA-Z] > new_file
七、删除空行
# cat file | tr -s "\n" > new_file
八、删除Windows文件“形成”的'^M'字符
# cat file | tr -d "\r" > new_file
或者
# cat file | tr -s "\r" "\n" > new_file
【注意】这里-s后面是两个参数"\r"和"\n",用后者替换前者
九、用空格符\040替换制表符\011
# cat file | tr -s "\011" "\040" > new_file
十、把路径变量中的冒号":",替换成换行符"\n"
# echo $PATH | tr -s ":" "\n"


20.vi/vim

vi和vim:文本编辑器(加强版vi,如今基本各类linux版本都带有vim)
一、vi编辑器是全部类unix下的标准编辑器。
二、vim是vi的升级版本,大部分类unix系统都默认安装了vim编辑器
三、vim具备程序编辑能力,能够经过颜色来辨别语法的正确性,方便程序设计

vim工具三种模式:
1,通常普通模式:底行模式(末行模式)
2,编辑模式(插入模式):i I o O a A r R
3,命令行模式:   : /  ?

移动相关:
--ctrl+b   向上翻页   =  page up
--ctrl+f    向下翻页=  page down
--ctrl+d   向下移动半页
--ctrl+u   向上移动半页
--Ctrl+ww 在窗口间切换--Ctrl+w +or-or=  增减高度
--G          移动到页末 =shift +g
--gg        移动到页头
--0          移动到行头 = home
--$          移动到行末 = end
--n          (表明数字)+回车   向下移动N行  
--h          向左移动
--l           向右移动
--k          向上移动
--j           向下移动
gconf-editor 配置编辑器

/etc/vimrc 配置文件路径

vim +24 file 打开文件定位到指定行

vim file1 file2 打开多个文件

vim -O2 file1 file2 垂直分屏

vim -on file1 file2 水平分屏 

:sp filename          # 上下分割打开新文件 

:vs filename          # 左右分割打开新文件

:set nu                # 打开行号

:set nonu              # 取消行号

 :nohl                  # 取消高亮

:set paste            # 取消缩进

:set autoindent        # 设置自动缩进

:set ff                # 查看文本格式

 :set binary            # 改成unix格式

 :200                  # 跳转到200  1 文件头

dd                    # 删除当前行 并复制 可直接p粘贴

11111dd          # 删除11111行,可用来清空文件

r                      # 替换单个字符

R                      # 替换多个字符

u                      # 撤销上次操做

 *                      # 全文匹配当前光标所在字符串

$                      # 行尾

0                      # 行首

X                      # 文档加密v =                    # 自动格式化代码

Ctrl+v                # 可视模式

Ctrl+v I ESC          # 多行操做

Ctrl+v s ESC          # 批量取消注释

删除,复制,粘贴

--x           向后删除一个字符 = delete
--X          往前删除一个字符 = backspace
--dd        直接删除光标所在行ndd  (n表明数字,删除n行)
--yy        复制光标所在行nyy(n表明数字,复制n行)
--p         粘贴
--u         回退上一次操做     按一次u只能回退一次,一直按u,撤销到文件最后一次保存的状态 |ctrl+r 撤销撤销
--ctrl+r  重作上一次操做

进入编辑模式:
i       当前insert
I       行头insert
a      向后一个字符insert
A     行末insert
o     下一行insert
O    上一行insert
r      替换光标所在的字符
R     从光标处向后一直替换
ESC 从编辑模式回退到通常模式


查找与替换:
-- /word                          查找单词      n  向下查找   N 向上查找
-- :1,$s/old/new/g       从第一行到最后一行都用new替换old
--:%s/root/hello             替换每一行第一次出现的关键字
--:%s/root/uplook/g      全文搜索替换
--:%s/root/uplook/gc    交互式搜索替换
--:11s/nologin/haha/g   在指定行搜索替换
-- :%s/old/new/g           从第一行到最后一行都用new替换old
-- :n1,n2s/old/new/g     从n1行到n2行用new替换old
-- :1,$s/old/new/gc从 第一行到最后一行都用new替换old,多加了一个c就能够要求用户确认哪些换哪些不换
--/关键字                       n向下匹配|N向上匹配
--?关键字                     n向上匹配|N向下匹配

若是搜索的关键字里有特殊字符:
自定义分割符:
--:%s#/sbin/nologin#hello#g
反斜杠进行转义:
--:%s/\/bin\/bash/\/sbin\/nologin/g
--:w /tmp/aaa.txt       文件另存为
--:1,5w 888.txt           保存文件的前5行
--:r filename              读取另外一个文件的内容到当前文件(默认光标所在行的下面)
--:set nu                    显示行号(临时)
--:n(表明数字)+回车直接把光标定位到n行
--:w保存 (write)
--:q退出(quit)
--:wq保存并退出    =shift+zz  
--+!表示强制保存或者退出

若是你但愿vim打开时默认就有行号,能够这样作
# vim /etc/vimrc
--set number --在这个配置文件空的地方加上这一句就能够了
--:set number    或者 : set nu   把每一行都加上行号
--:set nonumber  或者 : set nonu去掉每一行前的行号

总结:
vim file——>命令行模式(yy/p/P/u/dd/G/gg..)——>编辑模式(i/I/a/A/o/O/r/R)——>底行模式(按“Esc”——>退出到命令行模式——>再按冒号“:”|搜索替换等)


21.nl

功能:打印文件行号

语法: nl [OPTION]... [FILE]...

经常使用选项:

-b        指定行号显示方式,主要有两种:

-b a:不管是否为空行,一样列出行号(同cat -n)

-b t:空行不打印行号。


-n:行号列出方法,主要有三种:

-n ln:左对齐

-n rn:右对齐,不加0

-n rz:右边显示,加0

-w :  行号栏位在左边占用的宽度

示例: 

[root@docker-node5 ~]# nl /etc/issue
     1 \S
     2 Kernel \r on an \m
 [root@docker-node5 ~]# nl -b a /etc/issue
     1 \S
     2 Kernel \r on an \m
     3
[root@docker-node5 ~]# nl -b t /etc/issue
     1 \S
     2 Kernel \r on an \m

[root@docker-node5 ~]# nl -n ln /etc/issue
1      \S
2      Kernel \r on an \m
       
[root@docker-node5 ~]# nl -n rn /etc/issue
     1 \S
     2 Kernel \r on an \m
       
[root@docker-node5 ~]# nl -n rz /etc/issue
000001 \S
000002 Kernel \r on an \m
       
[root@docker-node5 ~]# nl -w 5 /etc/issue
    1 \S
    2 Kernel \r on an \m
      
[root@docker-node5 ~]# nl -w 1 /etc/issue
1 \S
2 Kernel \r on an \m
  
[root@docker-node5 ~]# nl -w 2 /etc/issue
 1 \S
 2 Kernel \r on an \m

22.nano

功能:简单文件编辑器

下面介绍下用法,具体功能本身体会,我本人喜欢用vim

用法:

光标控制

移动光标:使用用方向键移动。

选择文字:按住鼠标左键拖到。

复制、剪贴和粘贴

复制一整行:Alt+6

剪贴一整行:Ctrl+K

粘贴:Ctrl+U

选定复制:先ctrl+6或alt+a标记开始,而后光标移至要复制或剪切的末尾,alt+6复制,ctrl+k剪贴,若要取消,再按一次ctrl+6
搜索:Ctrl+W

翻页:

Ctrl+Y到上一页

Ctrl+V到下一页

保存:Ctrl+O 

退出:Ctrl+X

23.gedit
功能:文本编辑器

了解便可,文本编辑仍是vim强大

快捷键:  

CTRL-Z:撤销  

CTRL-C:复制  

CTRL-V:粘贴  

CTRL-T:缩进  

CTRL-Q:退出  

CTRL-S:保存  

CTRL—R:替换  

CTRL+Tab 切换  

CTRL+W 关闭选项卡


24.seq

功能:打印数字序列
语法:seq [OPTION]... LAST
    seq [OPTION]... FIRST LAST
    seq [OPTION]... FIRST INCREMENT LAST
经常使用选项:
-f  使用printf样式格式
-s  指定分隔符,默认换行符\n
-w  等宽,用0填充
示例:

数字序列:
方法1:
[root@localhost ~]# seq 10
1
2
3
4
5
6
7
8
9
10
方法2:
for循环
#!/bin/bash
for i in `seq 1 10`;
do
echo $i;
done
或者用
for i in $(seq 1 10)
方法3:
经过指定步长,所谓步长就是一步之长
[root@localhost ~]# seq 1 5 20  #这里的步长就是5
1
6
11
16
方法4:
[root@localhost ~]# seq 20 30  #指定范围
20
21
22
23
24
25
26
27
28
29
30
-f选项:
#seq -f"%3g" 9 11
9
10
11
% 后面指定数字的位数 默认是"%g",
"%3g"那么数字位数不足部分是空格
#sed -f"%03g" 9 11  这样的话数字位数不足部分是0
% 前面制定字符串
seq -f "str%03g" 9 11
str009
str010
str011
-w选项:
不能和-f一块儿使用
[root@localhost ~]# seq -w -f"str%03g" 9 11
seq: format string may not be specified when printing equal width strings
Try `seq --help' for more information.
[root@localhost ~]# seq -w 1 20 #数字前面带0
01
...............
20
-s选项:
[root@localhost ~]# seq -s" " -f"str%03g" 9 11  #空格分隔
str009 str010 str011
[root@localhost ~]# seq -s"+" 1 10  #+号分隔
1+2+3+4+5+6+7+8+9+10
[root@localhost ~]# seq -s"\t" 1 10  #有正则的这样写虽然打印了,可是违背了题意
1\t2\t3\t4\t5\t6\t7\t8\t9\t10
[root@localhost ~]# seq -s "$(echo -e "\t")" 1 10  #这样写才会达到效果。制表符为分隔打印
1      2      3      4      5      6      7      8      9      10
[root@localhost ~]# seq -s "$(echo -e "\n")" 1 10
12345678910
[root@localhost ~]# seq -s "$(echo -e "\n\n")" 1 10  #两个效果同样
12345678910
12345678910
批量建立文件:
方法1:
[root@localhost ~]# awk 'BEGIN { while (num < 10 ) printf "dir%03d\n", ++num ; exit}' | xargs mkdir  #使用awk来获取文本
[root@localhost ~]# ls
anaconda-ks.cfg  dir003  dir006  dir009      install.log.syslog
dir001          dir004  dir007  dir010      system.sh
dir002          dir005  dir008  install.log
方法2:
[root@localhost ~]# mkdir $(seq -f 'dir%03g' 1 10)
方法3:
for i in `seq -f '%02g' 1 20`
do
if ! wget -P $HOME/tmp -c [img]http://www.xxxsite.com/photo/$i.jpg[/img] ; then
wget -P $HOME/tmp -c $_
fi
done


方法4:
[victor@localhost ~]$ seq -s '
>
> ' 1 5
1

2

3

4

5
\t 便得改變IFS, 如用\t\t
OIFS=$IFS
IFS="\t\t"
seq -s `echo -e $IFS` 1 5
IFS=$OIFS


25.shuf

功能:生成随机序列

经常使用选项:

-i  输出数字范围

-o  结果写入文件

示例:

输出范围随机数:
[root@localhost ~]# seq 10

1
2
3
4
5
6
7
8
9
10
[root@localhost ~]# seq 10|shuf
3
7
4
6
8
9
5
10
2
[root@localhost ~]# shuf -i 1-10
10
5
2
7
9
8
4
1
6
3