8.2Shell编程-字符截取命令

一、 cut字段提取命令

[rootlocalhost ~]# cut [选项] 文件名
选项: -f 列号 提取第几列
-d 分隔符 按照指定分隔符分割列

范例:
[rootlocalhost ~]# vi student.txt
ID Name gender Mark
1 lingmi M 68 注意:不能使用空格,要使用Tab键
2 sc M 90
3 gao M 83
[rootlocalhost ~]# cut -f 2,3 student.txt
Name gender
lingmi M
sc M
gao M

[rootlocalhost ~]# vi student.txt
ID Name % gender Mark
1 lingmi % M 68 注意:不能使用空格,要使用Tab键
2 sc % M 90
3 gao % M 83

[rootlocalhost ~]# cut -d “%” -f 2,3 student.txt
Name
lingmi
sc
gao

cut命令的局限:不能用于以空格分列的列表

二、 printf命令

[rootlocalhost ~]# printf ‘输出类型输出格式’ 输出内容
输出类型: %ns 输出字符串。N是数字指代输出集合字符
%ni 输出整数。N 是数字指代输出几个数字
%m. nf s输出浮点小数。如%8.2f代表共输出8位数,其中2位小数,6位整数
输出格式:
\a 输出警告声音
\b 输出退格键(Backspace)
\f 清除屏幕
\n 换行
\r 回车
\t 水平输出退格键(Tab)
\v 垂直输出退格键(Tab)

[rootlocalhost ~]# printf %s 1 2 3 4 5 6
123456[rootlocalhost ~]#

[rootlocalhost ~]# printf %s %s %s 1 2 3 4 5 6
%s%s123456[rootlocalhost ~]#

[rootlocalhost ~]# printf ‘%s %s %s’ 1 2 3 4 5 6
1 2 34 5 6[rootlocalhost ~]#

[rootlocalhost ~]# printf ‘%s %s %s\n’ 1 2 3 4 5 6 以三个为一组
1 2 3
4 5 6
[rootlocalhost ~]#

[rootlocalhost ~]# printf ‘%s\t %s\t %s\n’ $(cat 文件名)
以三个字符串为一行,每列以Tab隔开输出内容

awk命令的输出中支持print和printf命令
print:输出一行后会自动换行,只能在awk里面用
printf:输出一行后不会自定换行,需要手工加入换行符“\n”

三、 awk命令

[rootlocalhost ~]# awk ‘条件1 {动作1} 条件2 {动作2}……‘ 文件名
条件(pattern):
一般用关系表达式作为条件x>10      判断变量x是否大于10x<10      判断变量x是否小于10x>=10     判断变量x是否大于等于10

动作(action)
格式化输出
流程控制语句

范例:
[rootlocalhost ~]# vi student.txt
在这里插入图片描述

[rootlocalhost ~]# awk ‘{printf $1 “\t” $2”\n”}’ student.txt 没条件,只要是student.txt的数据就符合
或[rootlocalhost ~]# awk ‘{print $1 “t” $2}’ student.txt
在这里插入图片描述

[rootlocalhost ~]# vi student.txt | grep lingmi | awk ’{print $4}’
68%
[rootlocalhost ~]# vi student.txt | grep lingmi | awk ‘{print $4}’ | cut -d “%” -f 1
68

BEGIN
[rootlocalhost ~]# awk ‘BEGIN{printf”This is a test\n”} {printf $2 “\t” $6 “\n”}’ student.txt
在所有的数据读取之前执行”This is a test、n“这条命令,然后再处理后面的数据

END
[rootlocalhost ~]# awk ‘END{printf”The Rnd \n”}’
在命令的最后所有的数据都执行完之后,再执行END的动作“The End \n”

FS内置变量 (作用:指定分隔符)
[rootlocalhost ~]# cat /etc/passwd | grep “/bin/bash” | awk ‘BEGIN {FS=”:”} {printf $1 “\t” $3 “\n”}’
/etc/psaawd的分隔符是“:“,因为awk默认识别的分隔符是”Tab“和”空格“符,所以”:“需要指定
如果不加BEGIN,则第一行分隔符永远是默认的,从第二行才是以指定得分隔符为分隔符。因为awk是先读入第一行数据,然后在执行其他的。

关系运算符
[rootlocalhost ~]# cat student.txt | grep -v Name | awk ’$6>=87 {printf $2 “\n”}’
读取student.txt内容,选择除包含Name所在行的所有行,判断每一行第6列是否>=87,是就输出所在行的第2列

四、 sed命令

sed命令:是一种几乎包括在所有UNIX平台(包括Linux)的轻量级编译器。Sed主要是用来将数据进行选取、替换、删除、新增的命令

格式: [rootlocalhost ~]# sed [选项] ‘[动作]’ 文件名
选项:

在这里插入图片描述

动作:
在这里插入图片描述

范例:
[rootlocalhost ~]# vi student.txt

[rootlocalhost ~]# sed -n ‘2p’ student.txt 打印第二行,“-n”表示只输出指定的行

[rootlocalhost ~]# sed ‘2,3d’ student.txt 删除2到3行,但是文件本身内容不变
在这里插入图片描述

[rootlocalhost ~]# sed ‘2a nihao’ student.txt 但是文件本身内容不变

nihao

[rootlocalhost ~]# sed ‘2i hello hudjid sjdadk sjadiak jasodkao jxoas asjoas joas sako >
Dhsmsk’ students.txt “>”代表没有输完

hello hudjid sjdadk sjadiak jasodkao jxoas asjoas joas sako
Dhsmsk

[rootlocalhost ~]# sed ‘ 4c no such person’ students.txt 替换第四行内容

no such person
[rootlocalhost ~]# sed ‘4s/3/6g’ students.txt 将第4行的“3”替换成“6“

[rootlocalhost ~]# sed -i ‘4s/3/6g’ students.txt 将原文件的第4行的“3”替换成“6“
[rootlocalhost ~]# cat student.txt

[rootlocalhost ~]# sed -e ‘s/Name//g ; s/gao//g’ students.txt 允许多个动作执行,用“;” 或 “回车符” 分隔开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值