首页 技术 正文
技术 2022年11月14日
0 收藏 730 点赞 3,425 浏览 3492 个字

10.1 cut

cut的工作就是“剪”,具体的说就是在文件中负责剪切数据用的。cut 命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段输出。

1.基本用法

cut [选项参数]  filename

说明:默认分隔符是制表符

2.选项参数说明

表1-55

选项参数

功能

-f

列号,提取第几列

-d

分隔符,按照指定分隔符分割列

3.案例实操

(0)数据准备

[atguigu@hadoop101 datas]$ touch cut.txt
[atguigu@hadoop101 datas]$ vim cut.txt
dong shen
guan zhen
wo wo
lai lai
le le

(1)切割cut.txt第一列

[atguigu@hadoop101 datas]$ cut -d " " -f  cut.txt
dong
guan
wo
lai
le

(2)切割cut.txt第二、三列

[atguigu@hadoop101 datas]$ cut -d " " -f , cut.txt
shen
zhen
wo
lai
le

(3)在cut.txt文件中切割出guan

[atguigu@hadoop101 datas]$ cat cut.txt | grep "guan" | cut -d " " -f
guan

(4)选取系统PATH变量值,第2个“:”开始后的所有路径:

[atguigu@hadoop101 datas]$ echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/atguigu/bin[atguigu@hadoop102 datas]$ echo $PATH | cut -d: -f -
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/atguigu/bin

(5)切割ifconfig 后打印的IP地址

[atguigu@hadoop101 datas]$ ifconfig eth0 | grep "inet addr" | cut -d: -f  | cut -d" " -f1
192.168.1.102

10.2 sed

sed是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”,接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。

  1. 基本用法

sed [选项参数]  ‘command’  filename

  1. 选项参数说明

表1-56

选项参数

功能

-e

直接在指令列模式上进行sed的动作编辑。

  1. 命令功能描述

表1-57

命令

功能描述

a 

新增,a的后面可以接字串,在下一行出现

d

删除

s

查找并替换

  1. 案例实操

(0)数据准备

[atguigu@hadoop102 datas]$ touch sed.txt
[atguigu@hadoop102 datas]$ vim sed.txt
dong shen
guan zhen
wo wo
lai laile le

(1)将“mei nv”这个单词插入到sed.txt第二行下,打印。

[atguigu@hadoop102 datas]$ sed '2a mei nv' sed.txt
dong shen
guan zhen
mei nv
wo wo
lai laile le
[atguigu@hadoop102 datas]$ cat sed.txt
dong shen
guan zhen
wo wo
lai laile le

注意:文件并没有改变

(2)删除sed.txt文件所有包含wo的行

[atguigu@hadoop102 datas]$ sed '/wo/d' sed.txt
dong shen
guan zhen
lai laile le

(3)将sed.txt文件中wo替换为ni

[atguigu@hadoop102 datas]$ sed 's/wo/ni/g' sed.txt
dong shen
guan zhen
ni ni
lai laile le

注意:‘g’表示global,全部替换

(4)将sed.txt文件中的第二行删除并将wo替换为ni

[atguigu@hadoop102 datas]$ sed -e '2d' -e 's/wo/ni/g' sed.txt
dong shen
ni ni
lai laile le

10.3 awk

一个强大的文本分析工具,把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行分析处理。

  1. 基本用法

awk [选项参数] ‘pattern1{action1}  pattern2{action2}…’ filename

pattern:表示AWK在数据中查找的内容,就是匹配模式

action:在找到匹配内容时所执行的一系列命令

  1. 选项参数说明

表1-55

选项参数

功能

-F

指定输入文件折分隔符

-v

赋值一个用户定义变量

  1. 案例实操

(0)数据准备

[atguigu@hadoop102 datas]$ sudo cp /etc/passwd ./

(1)搜索passwd文件以root关键字开头的所有行,并输出该行的第7列。

[atguigu@hadoop102 datas]$ awk -F: '/^root/{print $7}' passwd
/bin/bash

(2)搜索passwd文件以root关键字开头的所有行,并输出该行的第1列和第7列,中间以“,”号分割。

[atguigu@hadoop102 datas]$ awk -F: '/^root/{print $1","$7}' passwd
root,/bin/bash

注意:只有匹配了pattern的行才会执行action

(3)只显示/etc/passwd的第一列和第七列,以逗号分割,且在所有行前面添加列名user,shell在最后一行添加”dahaige,/bin/zuishuai”。

[atguigu@hadoop102 datas]$ awk -F : 'BEGIN{print "user, shell"} {print $1","$7} END{print "dahaige,/bin/zuishuai"}' passwd
user, shell
root,/bin/bash
bin,/sbin/nologin
。。。
atguigu,/bin/bash
dahaige,/bin/zuishuai

注意:BEGIN 在所有数据读取行之前执行;END 在所有数据执行之后执行。

(4)将passwd文件中的用户id增加数值1并输出

[atguigu@hadoop102 datas]$ awk -v i= -F: '{print $3+i}' passwd
  1. awk的内置变量

表1-56

变量

说明

FILENAME

文件名

NR

已读的记录数

NF

浏览记录的域的个数(切割后,列的个数)

  1. 案例实操

(1)统计passwd文件名,每行的行号,每行的列数

[atguigu@hadoop102 datas]$ awk -F: '{print "filename:"  FILENAME ", linenumber:" NR  ",columns:" NF}' passwd
filename:passwd, linenumber:,columns:
filename:passwd, linenumber:,columns:
filename:passwd, linenumber:,columns:

(2)切割IP

[atguigu@hadoop102 datas]$ ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk -F " " '{print $1}'
192.168.1.102

(3)查询sed.txt中空行所在的行号

[atguigu@hadoop102 datas]$ awk '/^$/{print NR}' sed.txt

10.4 sort

sort命令是在Linux里非常有用,它将文件进行排序,并将排序结果标准输出。

  1. 基本语法

sort(选项)(参数)

表1-57

选项

说明

-n

依照数值的大小排序

-r

以相反的顺序来排序

-t

设置排序时所用的分隔字符

-k

指定需要排序的列

参数:指定待排序的文件列表

2. 案例实操

(0)数据准备

[atguigu@hadoop102 datas]$ touch sort.sh
[atguigu@hadoop102 datas]$ vim sort.sh
bb::5.4
bd::4.2
xz::2.3
cls::3.5
ss::1.6

(1)按照“:”分割后的第三列倒序排序。

[atguigu@hadoop102 datas]$ sort -t : -nrk   sort.sh
bb::5.4
bd::4.2
cls::3.5
xz::2.3
ss::1.6
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297