文本处理sed&软件管理

一、文本处理之sed命令

sed是流编辑器,最常用来进行文本替换。sed可以使用另一个字符串来替换匹配模式,模式可以是字符串或正则表达式:使用格式如下:
$ sed ‘s/pattern/replace_string/’ file
也可以从stdin中输入
$ cat file | sed ‘s/pattern/replace_string/’

-i 参数会使得sed用修改后的数据替换原始文件 。为避免修改有误,可以使用以下形式的sed:
sed -i.bak ‘s/abc/def/’ file

1.1 利用sed 取出ifconfig命令中本机的IPv4地址

root@ubuntu1804:~# ifconfig ens33 | grep netmask
        inet 192.168.1.17  netmask 255.255.255.0  broadcast 192.168.1.255
root@ubuntu1804:~# ifconfig ens33 | sed -nr '2s/(^.*inet )(.*)(  netmask.*)/\2/p'
192.168.1.17
root@ubuntu1804:~#  
root@ubuntu1804:~# ifconfig ens33 | sed -n '2s/^.*inet //;s/  netmask.*//p'
192.168.1.17
root@ubuntu1804:~#

1.2 删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首的#和空白字符

[root@centos8 ~]# sed  's/# //' /etc/fstab

#
/etc/fstab
Created by anaconda on Wed Dec 15 09:59:00 2021
#
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.
#
After editing this file, run 'systemctl daemon-reload' to update systemd
units generated from this file.
#
UUID=e0c4a242-a522-427a-b03f-210c8edac6b6 /                       xfs     defaults        0 0
UUID=92e28589-fcd8-4b9e-9dd3-025ade9ab870 /boot                   ext4    defaults        1 2
UUID=dee90cb2-9e9a-49f7-92c0-a0646ff9bb77 /data                   xfs     defaults        0 0
UUID=d5a288f3-e351-4be1-a5d4-c2756133024e none                    swap    defaults        0 0

1.3 处理/etc/fstab路径,使用sed命令取出其目录名和基名

[root@centos8 ~]# echo "/etc/fstab" |sed -nr 's#(^/.*/)([^/]+)/?#\1#p'
/etc/
[root@centos8 ~]# echo "/etc/fstab" |sed -nr 's#(^/.*/)([^/]+)/?#\2#p'
fstab
[root@centos8 ~]# 

二、软件管理

现在的Linux发行版都有软件包管理器来进行软件安装、管理和删除相应的软件。软件包通常会依赖其他的包,为了前者能够正常运行,被依赖的包必须提前安装在系统中,而软件包管理器工具将会检测这些依赖关系,并在安装需要的包之前先安装好的有额外的软件包。利用包管理器可以方便快捷地实现程序包的安装、卸载、查询、升级和校验等管理操作

2.1 dpkg

package manager for Debian,类似于rpm, dpkg是基于Debian的系统的包管理器。可以安装,删除和构建软件包,但无法自动下载和安装软件包或其依赖项。dpkg 常见用法:

  1. 安装包:dpkg -i package.deb
  2. 删除包(包括配置文件):dpkg -P package
  3. 列出当前已安装的包,类似rpm -qa : dpkg -l

2.2 使用apt管理软件包

  1. 如果想快速显示某个特定包的详细信息,可以直接使用apt show命令。
root@ubuntu1804:~# apt show screen
Package: screen
Version: 4.6.2-1ubuntu1.1
Priority: optional
Section: misc
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Axel Beckert <abe@debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 1,010 kB
Depends: libc6 (>= 2.15), libpam0g (>= 0.99.7.1), libtinfo5 (>= 6), libutempter0 (>= 1.1.5)
Suggests: byobu | screenie | iselect (>= 1.4.0-1), ncurses-term
Homepage: https://savannah.gnu.org/projects/screen
Task: cloud-image, server
Supported: 5y
Download-Size: 577 kB
APT-Sources: http://hk.archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages
Description: terminal multiplexer with VT100/ANSI terminal emulation
 GNU Screen is a terminal multiplexer that runs several separate "screens" on
 a single physical character-based terminal. Each virtual terminal emulates a
 DEC VT100 plus several ANSI X3.64 and ISO 2022 functions. Screen sessions
 can be detached and resumed later on a different terminal.
 .
 Screen also supports a whole slew of other features, including configurable
 input and output translation, serial port support, configurable logging,
 and multi-user support.

N: There are 2 additional records. Please use the '-a' switch to see them.
root@ubuntu1804:~# 
  1. 使用apt可以轻松安装软件,而不用考虑包依赖关系。
root@ubuntu1804:~# apt install tree
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  tree
0 upgraded, 1 newly installed, 0 to remove and 165 not upgraded.
Need to get 40.7 kB of archives.
After this operation, 105 kB of additional disk space will be used.
Get:1 http://hk.archive.ubuntu.com/ubuntu bionic/universe amd64 tree amd64 1.7.0-5 [40.7 kB]
Fetched 40.7 kB in 6s (6,678 B/s)                                                                                                                                      
Selecting previously unselected package tree.
(Reading database ... 67125 files and directories currently installed.)
Preparing to unpack .../tree_1.7.0-5_amd64.deb ...
Unpacking tree (1.7.0-5) ...
Setting up tree (1.7.0-5) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
root@ubuntu1804:~#  
  1. 使用apt卸载软件。
root@ubuntu1804:~# apt remove tree
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  tree
0 upgraded, 0 newly installed, 1 to remove and 165 not upgraded.
After this operation, 105 kB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 67132 files and directories currently installed.)
Removing tree (1.7.0-5) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...

2.3 基于red-hat的系统

和基于Debian的发行版类似,基于red-hat的系统也有几种不同的工作。常见的有以下:

  1. yum:在red-hat、Fedora和CentOS中使用
  2. zypper:在openSUSE中使用
2.3.1 使用yum
  1. 使用yum找出某个特定软件包的详细信息。
[root@centos8 ~]# yum list installed tree
Installed Packages
tree.x86_64                        1.7.0-15.el8                        @anaconda
[root@centos8 ~]# 
  1. 找出系统上的某个特定文件属于哪个软件包
root@centos8 ~]# yum list installed tree
Installed Packages
tree.x86_64                        1.7.0-15.el8                        @anaconda
[root@centos8 ~]#  yum provides tree
Last metadata expiration check: 0:10:27 ago on Wed 19 Jan 2022 11:33:28 AM CST.
tree-1.7.0-15.el8.x86_64 : File system tree viewer
Repo        : @System
Matched from:
Provide    : tree = 1.7.0-15.el8

tree-1.7.0-15.el8.x86_64 : File system tree viewer
Repo        : baseos
Matched from:
Provide    : tree = 1.7.0-15.el8

[root@centos8 ~]# 
  1. 用yum安装软件
[root@centos8 ~]# yum install xterm
Last metadata expiration check: 0:11:14 ago on Wed 19 Jan 2022 11:33:28 AM CST.
Dependencies resolved.
====================================<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值