sed与grep练习题
第1章 练习题
第1题 取得/etc/hosts 文件的权限
如何取得/etc/hosts 文件的权限对应的数字内容,如-rw-r--r-- 为 644,要求使用命令取得644 这样的数字。
方法一 awk 指定分隔符
[root@node2 ~]# stat /etc/hosts |awk -F "[0/]" 'NR==4{print $2}'
方法二 stat -c%a
[root@node2 ~]# stat -c%a /etc/hosts
第2题 目录的硬链接数
linux 下通过 mkdir 命令创建一个新目录/clsn/ett,它的硬链接数是多少,为什么?
如果在/clsn/ett下面再创建一个目录 test。再问/clsn/ett 的硬链接数是多少?为什么。
新目录的硬链接数为2
[root@node2 ~]# mkdir /clsn/ett -p
[root@node2 ett]# ls -ldi /clsn/ett/
drwxr-xr-x. root root 12月 : /clsn/ett/
目录下的硬链接数为 2+下级目录数
[root@node2 ett]# mkdir dir01/
[root@node2 ett]# ls -ldi /clsn/ /clsn/. /clsn/ett/..
drwxr-xr-x. root root 12月 : /clsn/
drwxr-xr-x. root root 12月 : /clsn/.
drwxr-xr-x. root root 12月 : /clsn/ett/..
第3题 取出ip地址
执行命令取出 linux中 eth0 的 IP地址。
方法一 awk
[root@node2 ett]# ifconfig ens33|awk -F "[ :]+" 'NR==2{print $3}'
192.168.1.223
方法二 grep
[root@node2 ett]# ifconfig ens33|egrep "([0-9]{1,3}\.?){4}" -o |head -|tail -
192.168.1.223
内容在同一行输出
在一个命令上加什么参数可以实现下面命令的内容在同一行输出。
-n 不输出每行行尾的回车
-e 让echo支持 \n \t 转移字符系列
\n 换行
\t tab键
[root@node2 scprits]# echo -n "" ; echo "" [root@node2 scprits]# echo -n ""
[root@node2 scprits]# echo -e "123\n" [root@node2 scprits]# echo -ne "123\n"
date相关 时间同步
请给出如下格式的 date 命令 例:19-12-02。在给出实现按周输出 比如:周六输出为 6,请分别给出命令。
%F 与 %Y-%m-%d 相同
[root@node2 scprits]# date +%F
--
[root@node2 scprits]# date +%Y-%m-%d
--
时间
%H:%M:%S 与 %T相同
[root@node2 scprits]# date +%T
::
[root@node2 scprits]# date +%H:%M:%S
::
时间和周数
%w 表示周
[root@node2 scprits]# date +%F_%w
--02_1
手动设置系统日期
[root@node2 scprits]# date -s "20191202 15:55:00"
2019年 12月 02日 星期一 :: CST
自动同步系统时间
[root@node2 scprits]# ntpdate ntp1.aliyun.com
Dec :: ntpdate[]: step time server 120.25.115.20 offset -112.993154 sec
环境变量问题
当从root用户切到普通用户时,执行ifconfig会提示。
-bash: command not found.
运行目录的过程
先去PATH找
找到 运行命令解释器 shell
找不到 提示 command not found
永久修改 PATH
PATH 内容放到/etc/profile 文件最后
soucre /etc/profile 让它生效
打印三天前的日期格式
-d "-3day" 表示三天前
-d "3day" 表示三天后
[root@node2 scprits]# date +%F
--
[root@node2 scprits]# date -d "-3day" +%F
--
[root@node2 scprits]# date -d "3day" +%F
--
已知test.txt 文件内容为:
[root@node2 scprits]# cat test.txt
clsn xizi xiaochao
请问如何把文件中的空格过滤掉。
方法一 grep
[root@node2 scprits]# grep -v "^$" test.txt
clsn
xizi
xiaochao
-v 排除 ^$ (空行)
方法二 awk
[root@node2 scprits]# awk '!/^$/' test.txt
clsn
xizi
xiaochao
'!/^$/' 排除空行
方法三 sed
[root@node2 scprits]# sed '/^$/d' test.txt
clsn
xizi
xiaochao
'/^$/d' 排除空行
sed与awk
awk '找谁{干啥}'
sed '找谁干啥'
请使用 grep或 egrep 正则匹配的方式过滤文件test.txt出前两行内容
+ 表示前一个字符出现一次或一次以上。
[root@node2 scprits]# cat test.txt
clsn clssssn test
[root@node2 scprits]# egrep "c+l+s+n" test.txt
clsn
clssssn
排除以t开头的
[root@node2 scprits]# grep -iv "^t" test.txt
clsn clssssn
排除以t结尾的
[root@node2 scprits]# grep -v "t$" test.txt
clsn clssssn
请描述下列路径的内容是做什么的?
/var/log/messages 系统普通日志 /var/log/secure 用户登陆日志 /etc/fstab 开机自动挂载 /etc/profile 配置别名 环境变量 /etc/spool/cron/root 定时任务
sed与grep练习题的更多相关文章
- linux下的文本处理命令sed&awk&grep
Sedsed 是个精简的.非交互式的编辑器.他能执行和编辑vi和emacs相同的编辑任务.sed编辑器不提供交互使用方式:只能在命令行输入编辑命令.指定文件名,然后在屏幕上察看输出.sed编辑器没有破 ...
- 1.Sed | Awk | Grep | Find
1.Sed | Awk | Grep | Find 可以参考的文档链接 CentOS7 查看 当前机器 已经启动的端口的Shell命令: netstat -lntup | awk -F' ' {'pr ...
- 【转】如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等
如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等 你是否曾经有过要计算一个非常大的数据(几百GB)的需求?或在里面搜索,或其它操作——一些无法并 ...
- awk、sed、grep更适合的方向
awk.sed.grep更适合的方向: grep 更适合单纯的查找或匹配文本 sed 更适合编辑匹配到的文本 awk 更适合格式化文本,对文本进行较复杂格式处理 关于awk内建变量个人见解,简单易懂 ...
- 【转帖】vim/sed/awk/grep等文件批处理总结
vim/sed/awk/grep等文件批处理总结 https://www.cnblogs.com/cangqiongbingchen/p/9760544.html Vim相关操作 1.基础 * 和 # ...
- sed awk grep三剑客常用
sed的常用用法: awk的常用用法: grep的常用用法: 除了列出符合行之外,并且列出后10行. grep -A 10 Exception kzfinance-front.log 除了列出符合行之 ...
- sed,grep,awk命令常用法
查看当天nginx访问日志中2016:03:25到2016:05点passport.mingxiao.com域名访问量最多的url,可以查看网站是否被刷. 法一: sed -n '/2016:03:2 ...
- <转>如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等
原文链接:http://www.vaikan.com/use-multiple-cpu-cores-with-your-linux-commands/ 你是否曾经有过要计算一个非常大的数据(几百GB) ...
- 转摘--如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等
http://www.vaikan.com/use-multiple-cpu-cores-with-your-linux-commands/ 你是否曾经有过要计算一个非常大的数据(几百GB)的需求?或 ...
随机推荐
- day2_窗口句柄切换
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/7/16 14:21 # @Author : 大坏男孩 # @File : d ...
- [C1W2] Neural Networks and Deep Learning - Basics of Neural Network programming
第二周:神经网络的编程基础(Basics of Neural Network programming) 二分类(Binary Classification) 这周我们将学习神经网络的基础知识,其中需要 ...
- CF-1155 D.Beautiful Array
题目大意:现在有一个数列,还有一个数字x,你可以将这个数列中的一段连续子序列同时乘以这个数字x(当然也可以不乘),然后问你最大子段和是多少 做法:dp,你懂的 #include<iostream ...
- SecureCRT 8.1工具下载和破解附Xshell6
附教程:https://jingyan.baidu.com/article/eae078275917861fec548592.html 这一篇教程实际上已经说得非常明确了,只需要把注册机放在和secu ...
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) E. Rock Is Push dp
E. Rock Is Push You are at the top left cell (1,1) of an n×m labyrinth. Your goal is to get to the b ...
- Paper | No-reference Quality Assessment of Deblocked Images
目录 故事背景 本文方法(DBIQ) 发表在2016年Neurocomputing. 摘要 JPEG is the most commonly used image compression stand ...
- ROS机器人路径规划介绍--全局规划
ROS机器人路径规划算法主要包括2个部分:1)全局路径规划算法:2)局部路径规划算法: 一.全局路径规划 global planner ROS 的navigation官方功能包提供了三种全局路径规划器 ...
- h5py报错:FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
导入h5py的时候,报错: /home/harris/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: ...
- Vue.js 源码分析(一) 代码结构
关于Vue vue是一个兴起的前端js库,是一个精简的MVVM.MVVM模式是由经典的软件架构MVC衍生来的,当View(视图层)变化时,会自动更新到ViewModel(视图模型),反之亦然,View ...
- Kubernetes 中的服务发现与负载均衡
原文:https://www.infoq.cn/article/rEzx9X598W60svbli9aK (本文转载自阿里巴巴云原生微信公众号(ID:Alicloudnative)) 一.需求来源 为 ...