linux之字符处理

linux之字符处理

管道

linux中存在着管道,它是一个固定大小的缓冲区,缓冲区大小通常为1页,也就是4K字节。

管道是一个非常频繁的通信机制,利用符号“|”来连接进程,管道表现为输入输出重定向的一种方法,可以把一个命令的输出内容当作下一个命令的输入内容。

例如:我们将ls-l/etc/init.d的命令输出当作more命令的输入

swz@swz-debian:~$ ls -l /etc/init.d | more
总用量 180
-rwxr-xr-x 1 root root 2269 11月 22  2018 acpid
-rwxr-xr-x 1 root root 5336 3月  30  2019 alsa-utils
-rwxr-xr-x 1 root root 2948 12月  6  2021 bluetooth
-rwxr-xr-x 1 root root 3059 10月 11  2019 cron
-rwxr-xr-x 1 root root  937 8月  26  2019 cryptdisks
-rwxr-xr-x 1 root root  896 8月  26  2019 cryptdisks-early
-rwxr-xr-x 1 root root 2804 4月   4  2021 cups
-rwxr-xr-x 1 root root 2813 7月   6  2020 dbus
-rwxr-xr-x 1 root root 1399 9月  22  2019 gdomap
-rwxr-xr-x 1 root root 3809 1月  10  2019 hwclock.sh
-rwxr-xr-x 1 root root 3566 11月  2  2021 ipsec
-rwxr-xr-x 1 root root 2824 11月 25  2021 ipwatchd
-rwxr-xr-x 1 root root 2044 2月  10  2019 kmod
-rwxr-xr-x 1 root root 2168 8月   3  2019 laptop-mode
-rwxr-xr-x 1 root root 2610 12月  6  2021 lightdm
-rwxr-xr-x 1 root root  695 6月  21  2019 lvm2
-rwxr-xr-x 1 root root  586 6月  21  2019 lvm2-lvmpolld
-rwxr-xr-x 1 root root 2653 1月  16  2019 mdadm
-rwxr-xr-x 1 root root 1249 1月  16  2019 mdadm-waitidle
-rwxr-xr-x 1 root root 4445 8月  25  2018 networking
-rwxr-xr-x 1 root root 1942 11月 16  2021 network-manager
-rwxr-xr-x 1 root root 1938 10月 20  2021 nmbd
-rwxr-xr-x 1 root root 1846 10月  9  2019 open-vm-tools
-rwxr-xr-x 1 root root 9138 9月   1  2020 openvpn
-rwxr-xr-x 1 root root 1366 5月  28  2020 plymouth
-rwxr-xr-x 1 root root  752 5月  28  2020 plymouth-log
-rwxr-xr-x 1 root root  612 2月  21  2020 pppd-dns
-rwxr-xr-x 1 root root  924 5月  31  2018 procps
lrwxrwxrwx 1 root root   12 2月  15  2019 rc -> /lib/init/rc
lrwxrwxrwx 1 root root   13 2月  15  2019 rcS -> /lib/init/rcS
-rw-r--r-- 1 root root 2427 10月 26  2018 README
-rwxr-xr-x 1 root root 4417 3月  15  2019 rsync
-rwxr-xr-x 1 root root 2864 1月  20  2021 rsyslog
-rwxr-xr-x 1 root root 2263 10月 20  2021 samba-ad-dc
-rwxr-xr-x 1 root root 3088 12月 21  2020 smartmontools
-rwxr-xr-x 1 root root 2065 10月 20  2021 smbd
-rwxr-xr-x 1 root root 3939 6月  22  2021 ssh
-rwxr-xr-x 1 root root 1030 3月  31  2021 sudo
-rwxr-xr-x 1 root root 6872 8月   9  2021 udev
-rwxr-xr-x 1 root root 2757 11月 23  2016 x11-common
--More--

使用grep搜索文本

grep是linux下的基于行的文本搜索工具,使用其工具时,匹配到相关信息时便会打印在其所在的行。

常用参数:-i 不区分大写(ignore);-c 统计包括的匹配的行数;-n 输出行号 ;-v 反向匹配

swz@swz-debian:~/swz$ cat swz.txt 
swz
hello,what's is your name?
ok,i'm swz.
my name is swz,i come from china.
how old are you?
my age is 24.
do you want to know more thing?
no,let's come here.
swz@swz-debian:~/swz$ grep "name" swz.txt 
hello,what's is your name?
my name is swz,i come from china.

可以用管道进行上述命令的改写,把cat显示的内容输出到grep上。

swz@swz-debian:~/swz$ cat swz.txt | grep -v "name"
swz
ok,i'm swz.
how old are you?
my age is 24.
do you want to know more thing?
no,let's come here.

使用sort排序

对无序的数据进行排序需要用到sort命令来进行排序。

常用参数如下:-n 采取数字排序;-t 指定分隔符;-k 指定第几列;-r 反向排序。

swz@swz-debian:~/swz$ cat sort.txt 
c:11
b:5
a:6
d:8
f:-9
e:7
swz@swz-debian:~/swz$ cat sort.txt | sort -n
a:6
b:5
c:11
d:8
e:7
f:-9

使用uniq删除重复内容

当文件中有多行完全相同的内容,我们可以使用unip命令来删除文件重复的内容,也可以统计出文章相同的行的次数。

swz@swz-debian:~/swz$ cat uniq.txt 
swz
hello,what's is your name?
ok,i'm swz.
my name is swz,i come from china.
how old are you?
my age is 24.
do you want to know more thing?
no,let's come here.
hello,what's is your name?
ok,i'm swz.
ok,i'm swz.
swz@swz-debian:~/swz$ cat uniq.txt |sort | uniq
do you want to know more thing?
hello,what's is your name?
how old are you?
my age is 24.
my name is swz,i come from china.
no,let's come here.
ok,i'm swz.
swz

使用cut截取文本

cut截取文本,处理的是一行文本,可以选取出用户所需要的部分。

使用tr做文本转换

tr命令比较简单,主要用于文本转换或者删除。

tr "原先字符" "转换的字符" 转换字符 tr -d "字符" 删除字符

swz@swz-debian:~/swz$ cat 123.txt 
swz
hello,what's is your name?
ok,i'm swz.
my name is swz,i come from china.
how old are you?
my age is 24.
do you want to know more thing?
no,let's come here.
swz@swz-debian:~/swz$ cat 123.txt | tr "swz" "shuwenzhao"
shu
hello,hhat's is your name?
ok,i'm shu.
my name is shu,i come from china.
hoh old are you?
my age is 24.
do you hant to knoh more thing?
no,let's come here.
swz@swz-debian:~/swz$ cat 123.txt | tr -d "24"
swz
hello,what's is your name?
ok,i'm swz.
my name is swz,i come from china.
how old are you?
my age is .
do you want to know more thing?
no,let's come here.

使用paste做文本合并

可以paste命令将两个文件按照对应的的格式进行合并。利用-d 字符指定分分割的字符

swz@swz-debian:~/swz$ paste -d : a.txt b.txt 
你好 :hello
中国人:chinese people
共军:i'm a chinese
我是中国共产党:i can speak chinese
我的家乡是湖北的:my hometown is hubei province

本文由 mdnice 多平台发布

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值