12.23文本处理--sed和awk

一、sed

sed:stream editor(流编辑器) :一次处理一行内容,处理时,把当前的行存储在临时缓冲区,处理完后,输送到屏幕

sed [参数] '命令' file
       p    ##显示
       d    ##删除
       a    ##添加
       c    ##替换
       i    ##插入

1)p:显示

sed -n '/\:/p' /etc/fstab        ##显示带:的行
sed -n '/^#/p' /etc/fstab       ##显示以#开头的行
sed -n '/^#/!p' /etc/fstab      ##显示不以#开头的行
sed -n '2,6p' /etc/fstab       ##显示2-6行
sed -n '2,6!p' /etc/fstab      ##不显示2-6行

实验

[root@localhost mnt]# sed -n '/\:/p' /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014


[root@localhost mnt]# sed -n '/^#/p' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#


[root@localhost mnt]# sed -n '/^#/!p' /etc/fstab

UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1


[root@localhost mnt]# sed -n '2,6p' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'


[root@localhost mnt]# sed -n '2,6!p' /etc/fstab

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
 

 

2)d:删除

sed '/^UUID/d' /etc/fstab      ##删除以UUID开头的行
sed '/^#/d' /etc/fstab           ##删除以#开头的行
sed '/^$/d' /etc/fstab            ##^$开头即是结尾,删除空行
sed '1,4d' /etc/fstab          ##删除1-4行

实验:

[root@localhost mnt]# sed '/^UUID/d' /etc/fstab

#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#


[root@localhost mnt]# sed '/^#/d' /etc/fstab

UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1


[root@localhost mnt]# sed '/^$/d' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1


[root@localhost mnt]# sed '1,4d' /etc/fstab
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1

3)a:添加

sed '/hello/aworld' westos                  ##在hello下一行添加world  
sed 's/hello/hello world/g' westos       ##在hello替换为hello world
sed 's/hello/hello\nworld/g' westos     ##在hello替换为hello 换行 world

实验

[root@localhost mnt]# cat westos
hello world

hello

[root@localhost mnt]# sed '/hello/aworld' westos
hello world
world

hello
world

[root@localhost mnt]# sed 's/hello/hello world/g' westos
hello world world

hello world

[root@localhost mnt]# sed 's/hello/hello\nworld/g' westos
hello
world world

hello
world

 

4)c:替换

sed '/hello/chello world' westos    ##将hello所在的行换为helloworld

实验:

[root@localhost mnt]# sed '/hello/chello world' westos
hello world

hello world

 

5)i:插入

sed '/hello/iworld\nwestos' westos    ##在hello上面插入 world 换行 westos

[root@localhost mnt]# sed '/hello/iworld\nwestos' westos
world
westos
hello world

world
westos
hello

 

sed 's/\//#/g' /etc/fstab

6)-i:改变原文件内容,执行完成后原来文件也会被修改

sed -i 's/westos/redhat/' passwd

sed -i 's/westos/redhat/g' passwd    ##全局替换

2、awk报告生成器

awk处理机制:根据模式一次从文件中抽取一行文本,对这行文本进行切片(默认使用空白字符作为分隔符)

1)awk的基本用法

一、基本输出

[root@localhost mnt]# cat test.sh
this |   is |  a |   pig

 $1     $2  $3    $4

awk '{print $0}' test    ##$0表示输出一整行

awk '{print $1}' test

awk '{print $4}' test

awk '{print $1,$2}' test    ##显示两个字段

实验:

[root@localhost mnt]# awk '{print $0}' test
awk: fatal: cannot open file `test' for reading (No such file or directory)
[root@localhost mnt]# awk '{print $0}' test.sh
this is a pig
[root@localhost mnt]# awk '{print $1}' test.sh
this
[root@localhost mnt]# awk '{print $2}' test.sh
is
[root@localhost mnt]# awk '{print $3}' test.sh
a
[root@localhost mnt]# awk '{print $4}' test.sh
pig
[root@localhost mnt]# awk '{print $1,$2}' test.sh
this is

二、指定分隔符

awk -F ":" '{print $1,$3}' /etc/passwd    ##指定分隔符

实验

[root@localhost mnt]# cat test.sh
this is: a: pig
[root@localhost mnt]# awk -F ":" '{print $1,$3}' test.sh
this is  pig

 

2).awk常用变量

FILENAME    ##文件名

NR               ##行号

NF               ##列号


awk '{print FILENAME,NR}' /etc/passwd    ##输出文件名,和当前操作的行号

awk -F: '{print NR,NF}' /etc/passwd    ##输出每次处理的行号,以及当前以":"为分隔符的字段个数

总结:awk '{print "第NR行","有NF列"}' /etc/passwd

实验:

[root@localhost mnt]# awk '{print FILENAME,NR}' /etc/passwd
/etc/passwd 1
/etc/passwd 2
/etc/passwd 3
/etc/passwd 4
/etc/passwd 5
/etc/passwd 6
/etc/passwd 7
/etc/passwd 8
/etc/passwd 9

[root@localhost mnt]# awk -F: '{print NR,NF}' /etc/passwd
1 7
2 7
3 7
4 7
5 7
6 7
7 7
8 7
9 7

3)wak的一些简单命令

一、BEGIN 和 END

BEGIN{}            ##读入第一行文本之前执行的语句,一般用来初始化操作
{}:逐行处理
END{}               ##处理完最后以行文本后执行,一般用来处理输出结果

 

awk -F: 'BEGIN{print "REDHAT"} {print NR;print } END {print "WESTOS"}' file        ##文件开头加REDHAT,末尾加WESTOS,打印行号和内容

实验

[root@localhost mnt]# cat file
this is a file
[root@localhost mnt]# awk -F: 'BEGIN{print "REDHAT"} {print NR;print } END {print "WESTOS"}' file
REDHAT
1
this is a file
WESTOS

 

awk -F: '/bash$/{print}' /etc/passwd    ##输出以bash结尾的

实验

[root@localhost mnt]# awk -F: '/bash$/{print}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
student:x:1000:1000:Student User:/home/student:/bin/bash
brian:x:1001:1002::/home/brian:/bin/bash
rob:x:1002:1003::/home/rob:/bin/bash

 

awk -F: 'NR==3 {print}' /etc/passwd      ##输出第三行

实验

[root@localhost mnt]# awk -F: 'NR==3 {print}' /etc/passwd
daemon:x:2:2:daemon:/sbin:/sbin/nologin

 

awk -F: 'NR % 2 == 0 {print}' /etc/passwd    ##偶数行

实验

awk -F: 'NR >=3 && NR <=5 {print }' /etc/passwd  ##输出3-5行

实验

[root@localhost mnt]# awk -F: 'NR >=3 && NR <=5 {print }' /etc/passwd
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

 

awk 'BEGIN{i=0}{i+=NF}END{print i}' /etc/passwd   ##统计文本总字段个数

实验

[root@localhost mnt]# awk 'BEGIN{i=0}{i+=NF}END{print i}' /etc/passwd
77

二、条件语句

#if单分支语句
awk -F: 'BEGIN{i=0}{if($7~/bash$/){i++}}END{print i}' /etc/passwd    ##统计登录shell为bash的用户

[root@localhost mnt]# awk -F: 'BEGIN{i=0}{if($7~/bash$/){i++}}END{print i}' /etc/passwd
4

 

#if双分支
awk -F: 'BEGIN{i=0;j=0}{if($3<=500){i++}else{j++}}END{print i,j}' /etc/passwd    ##统计uid小于等于500和大于500的用户个数

[root@localhost mnt]# awk -F: 'BEGIN{i=0;j=0}{if($3<=500){i++}else{j++}}END{print i,j}' /etc/passwd
33 11

 

三、循环语句

#for循环
awk 'BEGIN{for(i=1;i<=5;i++){print i}}'     ##输出1-5

[root@localhost mnt]# awk 'BEGIN{for(i=1;i<=5;i++){print i}}'
1
2
3
4
5

#while循环

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: icarus-3.3.12.23是一个版本号,通常用于软件开发或系统更新的标识。它表示某个软件、操作系统或系统组件的版本,其中icarus可能是软件或项目的名称,3.3.12.23则代表该软件或项目的具体版本号。 版本号常用于区分不同版本的软件,以便用户能够追踪和了解软件的开发历程和功能更新。在软件开发过程中,每个版本通常会修复一些已知的问题、提供一些新功能或改进现有功能。因此,通过版本号,用户可以知道当前软件所具备的功能和性能,并据此决定是否进行升级或更新。 icarus-3.3.12.23的版本号中的每个数字可能代表特定含义。例如,3可能表示第三个主要版本,3.3可能表示该版本有三个次要更新,12表示该版本的第12个修复,23可能表示该版本的第23个小改进。 总之,icarus-3.3.12.23是一个版本号,用于表示某个软件或项目的具体版本,可以帮助用户识别软件的发展历程和了解软件的功能和性能。 ### 回答2: icarus-3.3.12.23是一个版本号。版本号通常用来标识软件、应用程序或系统的特定版本。在这个例子中,icarus-3.3.12.23可能是一个软件或应用程序的版本号。 版本号通常由一串数字和点号组成,每个点号之间的数字代表不同的层级。以icarus-3.3.12.23为例,最左边的数字3可能代表主要版本号,表示该软件或应用程序经历了重大的改进或功能增加。接下来的3.12可能代表次要版本号,表示该版本引入了一些较小的更改或修复。最后的23可能代表修订版本号,表示对该软件或应用程序进行了一些小的修改或更新。 版本号的目的是为了帮助用户和开发人员追踪和识别软件的不同版本。通过版本号,用户可以了解到一个软件或应用程序的重要特点、功能和修复的问题。对于开发人员来说,版本号对于记录和追踪代码更改以及软件版本的发布也非常重要。 总之,icarus-3.3.12.23是一个标识某个软件或应用程序特定版本的版本号,它帮助用户和开发人员追踪软件版本、功能和修复的重要工具。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值