Day 5 文本处理 Text processing

Day 5 文本处理 Text processing

一 文本处理三剑客命令初探 text processing three swordsman

Grep 过滤

globally search for a regular expression and print matching lines

Options 选项

  • -n: Precede each matching line with a line number.显示行号

  • -c: Output count of matching lines only.只显示行的数量

  • -i: Ignore uppercase vs. lowercase.不区分大小写

  • -v: Invert match.反向匹配

  • -w:Select only those lines containing matches that form whole words.精确匹配单词

  • -r:For each directory operand, read and process all files in that directory, recursively.递归

  • -l:Suppress normal output; instead print the name of each input file from which output would normally have been printed. 打印文件名,而不是文件内容

  • -A num Print num lines of trailing context after matching lines.打印符合条件行及后几行

  • -B num Print num lines of leading context before matching lines.打印符合条件行及前几行

  • -C num Print num lines of leading and trailing output context.打印符合条件行及前后几行

 

Regular expression 正则

  • ^Anchor the beginning of the line 定位开头是。。。的

  • $Anchor line end 定位结尾是。。。的

  • *The preceding character can appear any number of times 前一个字符出现任意次数

  • ?The preceding character can appear 0 times or 1 time 前一个字符出现0次或1次

  • +The preceding character appears at least once 前一个字符至少出现1次

  • .Matches any single character 匹配任意单个字符

  • []Match any single character in the specified range 匹配中括号中的任意一个字符

Grep exercises

正则表达式及字符处理
目标文件/etc/passwd,使用grep命令或egrep
1.显示出所有含有root的行:
grep "root" /etc/passwd
2.输出任何包含bash的所有行,还要输出紧接着这行的上下各两行的内容:
grep -C 1 "bash" /etc/passwd
3. 显示出有多少行含有nologin。
grep -c "nologin" /etc/passwd
4.显示出那些行含有root,并将行号一块输出。
grep -n "root" /etc/passwd
6.新建用户
  abominable
  abominate
  anomie
  atomize
  编写正则表达式,将他们匹配出来
  egrep 'a.omi(nabl|nat|z|)e' /etc/passwd
7.建四个用户
  Alex213sb
  Wpq2222b
  yH438PIG
  egon666
  过滤出用户名组成是字母+数字+字母的行
  [root@MiWiFi-R3-srv ~]# egrep '^[a-Z]+[0-9]+[a-Z]+' /etc/passwd
8.显示出/etc目录下所有包含root的文件名
   grep -l 'root' /etc
9. 过滤掉/etc/ssh/sshd_config内所有注释和所有空行
grep -v '^#' /etc/ssh/sshd_config |grep -v '^ *$'

 

SED

stream editor

sed [options] {sed-commands} {input-file}

 

sed options 选项

-n Suppress automatic printing of pattern space. 取消默认输出

-i Edit files in place

sed command 命令

d Delete the pattern space; immediately start next cycle. p Print out the pattern space (to the standard output). This command is usually only used in conjunction with the -n command-line option.

a Append

i Insert

c Chang

s Substitute

SED exercises

Sed作业:以/etc/passwd文件为模板

1,删除文件每行的第一个字符。
sed -r 's/(.)(.*)/2/' /etc/passwd

2,删除文件每行的第二个字符。
sed -r 's/(.)(.)(.*)/13/' /etc/passwd

3,删除文件每行的最后一个字符。
sed -r 's/(.*)(.)/1/' /etc/passwd

4,删除文件每行的倒数第二个字符。
sed -r 's/(.*)(.)(.)/13/' /etc/passwd

5,删除文件每行的第二个单词。
sed -r 's/(^[a-Z]*)([^a-z]*)([a-Z]*)([^a-z]*)/124/' /etc/passwd

6,删除文件每行的倒数第二个单词。
sed -r 's/([^a-Z]+)([a-Z]+)([^a-Z]+)([a-Z]+)$/134/' /etc/passwd

7,删除文件每行的最后一个单词。
sed -r 's/([a-Z])([^a-Z]+)([a-Z]+)$/12/' /etc/passwd

8,交换每行的第一个字符和第二个字符。
sed -r '^s/(.)(.)/21/' /etc/passwd

9,交换每行的第一个字符和第二个单词。
sed -r 's/^(.)([a-Z]+)([^a-Z]+)([a-Z]+)([^a-Z]+)/42315/' /etc/passwd

10,交换每行的第一个单词和最后一个单词。
sed -r 's/^([a-Z]+)([^a-Z]+)(.*)([^a-Z]+)([a-Z]+)$/52341/' /etc/passwd

11,删除一个文件中所有的数字。
sed -r 's/[0-9]+//g' /etc/passwd

12,删除每行所有空格。
sed -r 's/ //g' /etc/passwd

13,用制表符替换文件中出现的所有空格。
sed -r 's/ / /g' /etc/passwd

14,把所有大写字母用括号()括起来。
sed -r 's/[A-Z]+/(&)/g' /etc/passwd

15,打印每行3次。
sed -r 'p;p' /etc/passwd

16,隔行删除。
sed '1~2d' /etc/passwd

17,把文件从第2行到第5行复制到第7行后面。(选做题)

18,把文件从第2行到第5行移动到第7行后面。(选做题)

19,只显示每行的第一个单词。


20,打印每行的第一个单词和第三个单词。

21,将格式为   mm/yy/dd   的日期格式换成   mm;yy;dd
sed 's#../../..#..;..;..#g' /etc/passwd
22, a.txt内容
  ABC
  DEF
  XYZ
  通过SED实现tac命令
  tac a.txt
  XYZ
  DEF
  ABC

 

AWK

 

二 文件查找 Locating file

find [options] [path...] [expression]

按文件名 by name:

[root@localhost ~]# find /etc -name "ifcfg-eth0"
[root@localhost ~]# find /etc -iname "ifcfg-eth0"       # -i忽略大小写
[root@localhost ~]# find /etc -iname "ifcfg-eth*"

按文件大小 by size:

[root@localhost ~]# find /etc -size +3M                 # 大于3M
[root@localhost ~]# find /etc -size 3M
[root@localhost ~]# find /etc -size -3M
[root@localhost ~]# find /etc -size +3M -ls             # -ls找到的处理动作

按时间找 by time(atime,mtime,ctime):

[root@localhost ~]# find /etc -mtime +3                 # 修改时间超过3天
[root@localhost ~]# find /etc -mtime 3                 # 修改时间等于3天
[root@localhost ~]# find /etc -mtime -3                 # 修改时间3天以内

按文件类型 by type:

[root@localhost ~]# find /dev -type f                       # f普通
[root@localhost ~]# find /dev -type d                       # d目录
[root@localhost ~]# find /dev -type l                       # l链接
[root@localhost ~]# find /dev -type b                       # b块设备
[root@localhost ~]# find /dev -type c                       # c字符设备
[root@localhost ~]# find /dev -type s                       # s套接字
[root@localhost ~]# find /dev -type p                       # p管道文件

按权限 by authorization

[root@localhost ~]# find . -perm 644 -ls
[root@localhost ~]# find . -perm -644 -ls
[root@localhost ~]# find . -perm -600 -ls
[root@localhost ~]# find /sbin -perm -4000 -ls      # 包含set uid
[root@localhost ~]# find /sbin -perm -2000 -ls      # 包含set gid
[root@localhost ~]# find /sbin -perm -1000 -ls      # 包含sticky

Exercises

find作业:

1.查找ifconfig命令文件的位置,用不同的方式实现
find / -name "ifconfig"

2.查找/etc/中的所有子目录(仅目录)复制到/tmp下
find /etc -tyde d -exec cp -rvf {} /tmp ;

3.查找/etc目录复制到/var/tmp/, 将/var/tmp/etc中的所有目录设置权限777(仅目录) 将/var/tmp/etc中所有文件权限设置为666

三 上传与下载 Upload and download

 

原文地址:https://www.cnblogs.com/fengshili666/p/14118859.html