Linux Learning Notes (f) - regular expressions and file format handling

Regular expressions and file format handling

The basis of regular expression characters aggregate (characters)

sed tool

sed 本身也是一个管线命令,可以分析 standard input 的啦! 而且 sed 还可以将数据进行取代、删除、新增、撷取特定行等等的功能呢。
复制代码

Example 1: The contents of / etc / passwd lists and print line numbers, meanwhile, please delete the line 2 to 5!

[dmtsai@study ~]$ nl /etc/passwd | sed '2,5d'
1 root:x:0:0:root:/root:/bin/bash
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
复制代码

Extended regular expressions

File format and related processing

Format Printing: printf

范例一:将刚刚上头数据的文件 (printf.txt) 内容仅列出姓名与成绩:(用 [tab] 分隔)
[dmtsai@study ~]$ printf '%s\t %s\t %s\t %s\t %s\t \n' $(cat printf.txt)
Name Chinese English Math Average
DmTsai 80 60 92 77.33
VBird 75 55 80 70.00
Ken 60 90 70 73.33
复制代码

awk: easy to use data processing tools

awk is a great data processing tools! Sed often compared to a process acting on the entire row, awk is a line which tend divided into several "field" to handle. Therefore, awk quite suitable for handling small data deal with it! awk normal operation mode is as follows:

[dmtsai@study ~]$ awk '条件类型1{动作1} 条件类型2{动作2} ...' filename
复制代码

If I want to remove the IP account and lander, and between the account and the IP to [tab] apart, it will become like this:

[dmtsai@study ~]$ last -n 5 | awk '{print $1 "\t" $3}'
dmtsai 192.168.1.100
dmtsai 192.168.1.100
dmtsai 192.168.1.100
dmtsai 192.168.1.100
dmtsai Fri
复制代码

File Compare Tool

diff

范例一:比对 passwd.old 与 passwd.new 的差异:
[dmtsai@study testpw]$ diff passwd.old passwd.new
4d3 <==左边第四行被删除 (d) 掉了,基准是右边的第三行
< adm:x:3:4:adm:/var/adm:/sbin/nologin <==这边列出左边(<)文件被删除的那一行内容
6c5 <==左边文件的第六行被取代 (c) 成右边文件的第五行
sync:x:5:0:sync:/sbin:/bin/sync <==左边()文件第六行内容

> no six line <==右边(>)文件第五行内容
复制代码

Very smart now! We just put the diff to deal with than finished!

This patch patch with the diff command, but there is a close relationship ah! We mentioned earlier, diff can be used to tell the difference between the two versions, for example, it is between two different versions of the file just passwd.old and passwd.new we have created. So, if you want to "upgrade" mean? When it is "to upgrade the old file into a new file" should be how to do it? In fact, it is not difficult! It is the "first to compare differences in the old version, and will make the difference file becomes patch files, old files and then update the patch file" button.

Guess you like

Origin blog.csdn.net/weixin_33895604/article/details/91394409