linux文本处理脚本题,Linux文本处理练习题(每日一练)

1、显示/var目录下所有以l开头,以一个小写字母结尾,且中间出现至少一位数字的文件或目录[root@localhost ~]# ls -d /var/l*[[:digit:]]*[[:lower:]]

/var/ladw23fsret

/var/ljjie3424sjfAAa:

2、显示/etc目录下以任意一位数字开头,且以非数字结尾的文件或目录[root@localhost ~]# ls -d /etc/[[:digit:]]*[^[:digit:]]

/etc/334adawdad

/etc/12adniwda:

3、显示/etc/目录下以非字母开头,后面跟了一个字母及其它任意长度任意字符的文件或目录[root@localhost ~]# ls -d /etc/[^[:alpha:]]*[[:alpha:]]?*

/etc/334adawdad  /etc/334adawdad123

/etc/12adniwda:

4、显示/etc目录下所有以m开头以非数字结尾的文件或目录[root@localhost ~]# ls /etc/m*[^[:digit:]] -d

/etc/machine-id                /etc/man_db.conf  /etc/modules-load.d  /etc/my.cnf

/etc/magic                     /etc/maven        /etc/motd            /etc/my.cnf.d

/etc/mailcap                   /etc/mime.types   /etc/mtab

/etc/mail.rc                   /etc/mke2fs.conf  /etc/mtools.conf

/etc/makedumpfile.conf.sample  /etc/modprobe.d   /etc/multipath

5、显示/etc目录下,所有以.d结尾的文件或目录[root@localhost ~]# ls -d /etc/*.d

/etc/bash_completion.d  /etc/ipsec.d         /etc/profile.d      /etc/rwtab.d

/etc/binfmt.d           /etc/ld.so.conf.d    /etc/rc0.d          /etc/sane.d

/etc/chkconfig.d        /etc/libibverbs.d    /etc/rc1.d          /etc/setuptool.d

/etc/cron.d             /etc/logrotate.d     /etc/rc2.d          /etc/statetab.d

/etc/depmod.d           /etc/modprobe.d      /etc/rc3.d          /etc/sudoers.d

/etc/dnsmasq.d          /etc/modules-load.d  /etc/rc4.d          /etc/sysctl.d

/etc/dracut.conf.d      /etc/my.cnf.d        /etc/rc5.d          /etc/tmpfiles.d

/etc/exports.d          /etc/oddjobd.conf.d  /etc/rc6.d          /etc/usb_modeswitch.d

/etc/gdbinit.d          /etc/pam.d           /etc/rc.d           /etc/xinetd.d

/etc/grub.d             /etc/popt.d          /etc/request-key.d  /etc/yum.repos.d

/etc/init.d             /etc/prelink.conf.d  /etc/rsyslog.d

6、显示/etc目录下,所有.conf结尾,且以m,n,r,p开头的文件或目录[root@localhost ~]# ls -d /etc/[mnrp]*.conf

/etc/man_db.conf  /etc/nfsmount.conf  /etc/pbm2ppa.conf  /etc/request-key.conf  /etc/rsyslog.conf

/etc/mke2fs.conf  /etc/nsswitch.conf  /etc/pnm2ppa.conf  /etc/resolv.conf

/etc/mtools.conf  /etc/numad.conf     /etc/radvd.conf    /etc/rsyncd.conf

7、显示当前系统上root、fedora、或user1用户的默认shell[root@localhost ~]# grep -e "^root\b" -e "^fedora\b" -e "^user1\b" /etc/passwd

root:x:0:0:root:/root:/bin/bash

[root@localhost ~]# grep -e "^root\b" -e "^fedora\b" -e "^user1\b" /etc/passwd|cut -d: -f7

/bin/bash[root@localhost ~]# egrep "^(root|fedora|user1)\b" /etc/passwd

root:x:0:0:root:/root:/bin/bash

[root@localhost ~]# egrep "^(root|fedora|user1)\b" /etc/passwd|cut -d: -f7

/bin/bash

8、找出/etc/rc.d/init.d/functions文件中某单词后跟一组小括号的行[root@localhost ~]# grep "\b[[:alpha:]]\+\b()" /etc/rc.d/init.d/functions

checkpid() {

daemon() {

killproc() {

pidfileofproc() {

pidofproc() {

status() {

success() {

failure() {

passed() {

warning() {

action() {

strstr() {

9、使用echo命令输出一个路径,而后使用grep取出其基名[root@localhost ~]# echo "/etc/init.d/functions" |grep -o "[^/]\+\?$"

functions

10、找出ifconfig命令结果中的1-255之间的数[root@localhost ~]# egrep "\b([1-9]|[1-9][0-9]|1[0-9][0-9]|2([0-4][0-9]|5[0-5]))\b" a

123

2

34

255

1

1、显示/etc/rc.d/rc.sysinit中以#开头,且后面跟一个或多个空白字符,而后又跟了任意非空白字符的行[root@localhost ~]# grep "^#[[:space:]]\+.\+" /etc/rc.d/init.d/functions

# -*-Shell-script-*-

# functions    This file contains functions to be used by most or all

#        shell scripts in the /etc/init.d directory.

# Make sure umask is sane

# Set up a default search path.

# Get a sane screen width

.......

2、查找/proc/cpuinfo文件中关键字,并显示前后1行[root@localhost ~]# grep -C1 "model name" /proc/cpuinfo

model        : 94

model name    : Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz

stepping    : 3

--

model        : 94

model name    : Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz

stepping    : 3

--

model        : 94

model name    : Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz

stepping    : 3

--

model        : 94

model name    : Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz

stepping    : 3

3、查找包含至少一个 root关键字 的文件[root@localhost ~]# grep -lr "root" /etc

/etc/X11/xinit/Xclients

/etc/X11/xinit/xinitrc

/etc/X11/xinit/xinitrc.d/zz-liveinst.sh

/etc/pki/tls/certs/make-dummy-cert

/etc/pki/tls/openssl.cnf

/etc/pki/ca-trust/ca-legacy.conf

/etc/pki/ca-trust/extracted/README

/etc/pki/ca-trust/extracted/java/README

......

4、查找文本中的所有QQ邮箱[root@localhost ~]# grep "[1-9][0-9]{4,10}@qq.com$" mail_list.txt

5、查找/etc/fstab中default和dev两个关键字[root@localhost ~]# grep -e "default" -e "dev" /etc/fstab

# Accessible filesystems, by reference, are maintained under '/dev/disk'

UUID=1aa7af5a-15a0-4cf2-a11d-8304d67c7093 /                       xfs     defaults        0 0

UUID=2fb41265-5d13-4ef0-82f6-3147009bd1a5 /boot                   xfs     defaults        0 0

UUID=a5a6b450-ba91-4e5b-a5ad-a17e12a2a994 /testdir                xfs     defaults        0 0

UUID=1d6f94c4-7eb4-430c-b143-b63b498c1c98 swap

6、查找/etc目录下,所有包含root关键字的文件并显示文件路径及文件名[root@localhost ~]# grep -lr "root" /etc/

/etc/X11/xinit/Xclients

/etc/X11/xinit/xinitrc

/etc/X11/xinit/xinitrc.d/zz-liveinst.sh

/etc/pki/tls/certs/make-dummy-cert

/etc/pki/tls/openssl.cnf

/etc/pki/ca-trust/ca-legacy.conf

/etc/pki/ca-trust/extracted/README

/etc/pki/ca-trust/extracted/java/README

/etc/pki/ca-trust/extracted/java/cacerts

/etc/pki/ca-trust/extracted/openssl/README

/etc/pki/ca-trust/extracted/pem/README

/etc/rpm/macros.perl-srpm

........

7、监控日志文档中产生Error的行,并只打印包含Error的行tailf /var/www/logs/error.log  | grep --line-buffered "Error"--line-buffered是不断刷新缓存的选项,当文件较大时可能会导致效率下降

8、过滤functions文件中,以单词或者单词前面跟了一个”_“开头的行,并显示前后2行[root@localhost ~]# egrep -C2 "^[_[:alnum:]]+\(\)" /etc/rc.d/init.d/functions

# Check if any of $pid (could be plural) are running

checkpid() {

local i

--

# local in the caller.

# Returns LSB exit code for the 'status' action.

__pids_var_run() {

local base=${1##*/}

local pid_file=${2:-/var/run/$base.pid}

--

# Output PIDs of matching processes, found using pidof

__pids_pidof() {

pidof -c -m -o $$ -o $PPID -o %PPID -x "$1" || \

pidof -c -m -o $$ -o $PPID -o %PPID -x "${1##*/}"

--

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值