N25-第六周作业

第六周

请详细总结vim编辑器的使用并完成以下练习题

文本编辑器:

vim:
    模式化的编辑器

        基本模式:
            编辑模式,命令模式,
            输入模式
            末行模式:
                内置的命令行借口;

打开文件:
    编辑模式:默认模式
        编辑模式-->输入模式:
            i:insert,在光标所在处输入;
            a:append,在光标所在处后方输入;
            o:在光标所在处的下方打开一个新行;
            I:子啊光标所在行的行首输入;
            A:在光标所在行的行尾输入;
            O:在光标所在处的上方打开一个新行;

        输入模式-->编辑模式
            ESC

        编辑模式-->末行模式
            :

        末行模式-->编辑模式
            ESC                                 

关闭文件:
    ZZ:保存并提出;

    :q 退出
    :q! 强制退出,不保存此前的编辑操作
    :wq 保存并退出;
        :w,:q
    :x 保存并退出;   
    :w /PATH/TO/SOMEFILE 另存为

光标跳转:

    字符间跳转
        h:左
        j:下
        k:上
        l:右

        #COMMAND:跳转由#指定的个数的字符;

    单词间跳转
        w:下一个单词的词首;
        e:当前或后一个单词的词尾;
        b:当前或前一个单词的词首;

        #COMMAND:跳转由#指定的个数的单词;

    行首行尾跳转
        ^:跳转至行首的第一个非空白字符;
        0:跳转至行首;
        $:跳转至行尾;

    行间跳转
        #G:跳转至#指定的行;
        1G,gg:第一行;
        G:最后一行;

    句间跳转
        ) 下一句
        ( 上一句

        #):跳转后#句
        #(:跳转前#句

    段间跳转
        } 上一段
        { 下一段

        #}:跳转后#段            
        #{:跳转前#段

翻屏:
    Ctrl+f:向文件尾部翻一屏
    Ctrl+b:向文件首部翻一屏
    Ctrl+d:向文件尾部翻半屏
    Ctrl+u:向文件首部翻半屏
    Enter:按行向后翻

vim的编辑命令:

字符编辑
    x:删除光标所在处的字符;
    #x:删除光标所在处起始的#个字符;

    xp:交换光标所在处的字符与其后面的字符的位置;

替换命令(replace):
    r:替换光标所在处的字符;
        rCHAR

删除命令:
    d:删除命令,可结合光标跳转字符,实现范围删除;
        d$:
        d^:

        dw:
        de:
        db:
            #COMMAND:

        dd:删除光标所在处的行;
            #dd:删除光标所处的行起始的共#行;

粘贴命令(p,put。paste):
    p:缓冲区的内容如果为整行,则粘贴在当前光标所在行的下方;否则粘贴至当前光标所在处的后方;
    P:缓冲区的内容如果为整行,则粘贴在当前光标所在行的上方;否则粘贴至当前光标所在处的前方;

复制命令(yank,y):
    y:复制,工作行为相似于d命令
        y$
        y^
        y0

        ye
        yw
        yb
            #COMMAND

        yy:复制一整行
            #yy:复制#行
改变命令(change,c):修改
    编辑模式-->输入模式 ,实现删除操作;
        c$
        c^
        c0

        cb
        ce
        cw
            #COMMAND

        cc:删除光标所在的行,并转换为输入模式;
            #cc:

其他的编辑操作:

可视化模式:选定(向鼠标一样选定)
v:按字符选定;
V:按行选定;

结合编辑命令:d,c,y                        

撤销(undo)操作:
    u:撤销此前的操作;
        #u:撤销此前的#个操作;

撤销此前的撤销:
    Ctrl+r  

重复执行前一个编辑操作;
    .   

vim自带的练习教程:vimtutor

vim末行模式: 内建命令行接口

1.地址界定
    :start_pos[,end_pos]
        #:特定的第#行,例如5即di5行;
        .:当前行;
        $:最后一行;
        #,#:指定行范围,左侧为起始行,右侧为结束行;
        #,+#:指定范围,左侧为起始绝对编号,右侧为相对左侧行号的偏移量;例如:3,+7
            .,$-1
            1,$
        %:全文
        /pattern/:从光标所在处起始向文件尾部第一次被模式所匹配到的行;
            /first/,$
        /pat1/,/pat2/:从光标所在处起始,第一次由pat1匹配到的行开始,至第一次由pat2匹配到的行结束之间的所有行;

    可同编辑命令一同使用,实现编辑操作:
        d
        y
        c

        w /PATH/TO/SOMEFILE:将范围内的文本保存至指定的文件中;
        r /PATH/FROM/SOMEFILE:将指定的文件中的文本读取并插入至指定位置;

2.查找
    /PATTERN:从当前光标所在处向文件尾部查找能够被当前模式匹配到的所有字符串;
    ?/PATTERN:从当前光标所在处向文件首部查找能够被当前模式匹配到的所有字符串;          
        n:下一个,与命令方向相同;
        N:上一个,与命令方向相反;

3.查找并替换
    s:末行模式的命令;使用格式:
        s/要查找的内容/替换为的内容/修饰符
            要查找的内容:可使用正则表达式;
            替换为的内容:不能使用正则表达式,但可以引用;
                如果“要查找的内容”部分在模式中使用分组符号:在“替换为的内容”中使用后向引用;
                直接引用查找模式匹配到的全部文本;
            修饰符:
                i:忽略大小写;
                g:全局替换,意味着一行中如果匹配到多次,则均替换;

        可把分隔符替换为其他非常用字符:
            s@@@
            s###

1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#;

[root@zf ~]# cp /etc/rc.d/rc.sysinit /tmp 

[root@zf ~]# vi /tmp/rc.sysinit 
    :%s@^\([[:space:]]\{1,\}.*\)@#\1@       
    381 次替换,共 381 行

2、复制/boot/grub/grub.conf至/tmp目录中,删除/tmp/grub.conf文件中的行首的空白字符;

[root@zf ~]# cp /boot/grub/grub.conf /tmp
[root@zf ~]# vi /tmp/grub.conf

    :%s@^[[:space:]]\+@@        
    3 次替换,共 3 行

3、删除/tmp/rc.sysinit文件中的以#开头,且后面跟了至少一个空白字符的行行的#和空白字符

[root@zf ~]# vi /tmp/rc.sysinit 
:%s@^#[[:space:]]\+@@
38 次替换,共 38 行

4、为/tmp/grub.conf文件中前三行的行首加#号;

[root@zf ~]# vi /tmp/grub.conf
:1,3s@^\(.*\)@#\1@

或
:1,3s@^@#@

3 次替换,共 3 行

5、将/etc/yum.repos.d/CentOS-Media.repo文件中所有的enabled=0或gpgcheck=0的最后的0修改为1;

[root@zf ~]# /etc/yum.repos.d/CentOS-Media.repo
:%s@\(enabled\|gpgcheck\)=0@\1=1@

6、每4小时执行一次对/etc目录的备份,备份至/backup目录中,保存的目录名为形如etc-201504020202

[root@zf ~]# mkdir /backup
[root@zf ~]# crontab -e

0 */4 * * * /bin/cp -a /backup/etc-$(date "+%Y%m%d%H%M") > /dev/null

7、每周2,4,6备份/var/log/messages文件至/backup/messages_logs/目录中,保存的文件名形如messages-20150402

 [root@zf ~]# mkdir /backup/messages_logs
 [root@zf ~]# crontab -e

 0 0 * * 2,4,6 /bin/cp /var/log/messages /backup/messages_logs/messages-$(date "+%Y%m%d" > /dev/null

8、每天每两小时取当前系统/proc/meminfo文件中的所有以S开头的信息至/stats/memory.txt文件中

[root@zf ~]# mkdir /stats
[root@zf ~]# crontab -e

0 */2 * * * /bin/grep -i "^s" /proc/meminfo >> /stats/memory.txt

9、工作日的工作时间内,每两小时执行一次echo "howdy"

[root@zf ~]# crontab -e

0 */2 * * 1-5 /bin/echo "howdy"

脚本编程练习

10、创建目录/tmp/testdir-当前日期时间;

[root@zf ~]# vim date.sh 


   #!/bin/bash

   date1=$(date "+%Y%m%d%H%M%S")

   mkdir /tmp/testdir-$date1


[root@zf ~]# chmod +x date.sh 
[root@zf ~]# ./date.sh 

[root@zf ~]# ls /tmp/ | grep testdir
    testdir-20161224140750
    testdir-20161224140843

11、在此目录创建100个空文件:file1-file100

[root@zf test]# vim file.sh 
    #!/bin/bash

        for number in {1..100}
    do
        touch file$number
    done

[root@zf test]# bash file.sh 
[root@zf test]# ls
    file1    file19  file29  file39  file49  file59  file69  file79  file89  file99
    file10   file2   file3   file4   file5   file6   file7   file8   file9   file.sh
    file100  file20  file30  file40  file50  file60  file70  file80  file90
    file11   file21  file31  file41  file51  file61  file71  file81  file91
    file12   file22  file32  file42  file52  file62  file72  file82  file92
    file13   file23  file33  file43  file53  file63  file73  file83  file93
    file14   file24  file34  file44  file54  file64  file74  file84  file94
    file15   file25  file35  file45  file55  file65  file75  file85  file95
    file16   file26  file36  file46  file56  file66  file76  file86  file96
    file17   file27  file37  file47  file57  file67  file77  file87  file97
    file18   file28  file38  file48  file58  file68  file78  file88  file98

12、显示/etc/passwd文件中位于第偶数行的用户的用户名;

[root@zf ~]# vim user2.sh          
    #!/bin/bash

    num=$(wc -l /etc/passwd | cut -d' ' -f1)

    for n in $(seq 2 2 $num)
    do
        grep -n '^' /etc/passwd | grep "^$n:"| cut -d: -f2 
    done


[root@zf test]# bash  user2.sh 
    bin
    adm
    sync
    halt
    uucp
    games
    ftp
    dbus
    vcsa
    rtkit
    abrt
    nfsnobody
    gdm
    apache
    postfix
    sshd
    nginx
    zabbix
    mageia
    openstack
    bash
    basher
    fedora
    user11
    user13
    user15
    user17
    user19

13、创建10用户user10-user19;密码同用户名;

[root@zf test]# vim user.sh 

    #!/bin/bash

    for n in {10..19}
    do  
        useradd user$n
        echo "user$n" | passwd "user$n" --stdin
    done


    [root@zf test]# bash -x user.sh  
        + for n in '{10..19}'
        + useradd user10
        + echo user10
        + passwd user10 --stdin
        更改用户 user10 的密码 。
        passwd: 所有的身份验证令牌已经成功更新。
        + for n in '{10..19}'
        + useradd user11
        + echo user11
        + passwd user11 --stdin
        更改用户 user11 的密码 。
        ...

        [root@zf test]# tail /etc/passwd
            user10:x:3009:3009::/home/user10:/bin/bash
            user11:x:3010:3010::/home/user11:/bin/bash
            user12:x:3011:3011::/home/user12:/bin/bash
            user13:x:3012:3012::/home/user13:/bin/bash
            user14:x:3013:3013::/home/user14:/bin/bash
            user15:x:3014:3014::/home/user15:/bin/bash
            user16:x:3015:3015::/home/user16:/bin/bash
            user17:x:3016:3016::/home/user17:/bin/bash
            user18:x:3017:3017::/home/user18:/bin/bash
            user19:x:3018:3018::/home/user19:/bin/bash

14、在/tmp/创建10个空文件file10-file19;

[root@zf tmp]# vi file.sh

    #!/bin/bash

        for number in {10..19}
    do
        touch /tmp/file$number
    done


[root@zf tmp]# ls /tmp | grep  "file[0-9][0-9]"
    file10
    file11
    file12
    file13
    file14
    file15
    file16
    file17
    file18
    file19

15、把file10的属主和属组改为user10,依次类推。

[root@zf tmp]# vi user_file.sh

    #!/bin/bash

    for n in {10..19}
    do
       chown user$n:user$n /tmp/file$n
    done


[root@zf tmp]# ls -l /tmp | grep  "file[0-9][0-9]"
    -rw-r--r--. 1 root   root       0 12月 25 11:20 file10
    -rw-r--r--. 1 root   root       0 12月 25 11:20 file11
    -rw-r--r--. 1 root   root       0 12月 25 11:20 file12
    -rw-r--r--. 1 root   root       0 12月 25 11:20 file13
    -rw-r--r--. 1 root   root       0 12月 25 11:20 file14
    -rw-r--r--. 1 root   root       0 12月 25 11:20 file15
    -rw-r--r--. 1 root   root       0 12月 25 11:20 file16
    -rw-r--r--. 1 root   root       0 12月 25 11:20 file17
    -rw-r--r--. 1 root   root       0 12月 25 11:20 file18
    -rw-r--r--. 1 root   root       0 12月 25 11:20 file19

[root@zf tmp]# bash -x user_file.sh  
    + for n in '{10..19}'
    + chown user10:user10 file10
    + for n in '{10..19}'
    + chown user11:user11 file11
    + for n in '{10..19}'
    + chown user12:user12 file12
    + for n in '{10..19}'
    + chown user13:user13 file13
    + for n in '{10..19}'
    + chown user14:user14 file14
    + for n in '{10..19}'
    + chown user15:user15 file15
    + for n in '{10..19}'
    + chown user16:user16 file16
    + for n in '{10..19}'
    + chown user17:user17 file17
    + for n in '{10..19}'
    + chown user18:user18 file18
    + for n in '{10..19}'
    + chown user19:user19 file19    

[root@zf tmp]# ls -l /tmp | grep  "file[0-9][0-9]"
    -rw-r--r--. 1 user10 user10     0 12月 25 11:20 file10
    -rw-r--r--. 1 user11 user11     0 12月 25 11:20 file11
    -rw-r--r--. 1 user12 user12     0 12月 25 11:20 file12
    -rw-r--r--. 1 user13 user13     0 12月 25 11:20 file13
    -rw-r--r--. 1 user14 user14     0 12月 25 11:20 file14
    -rw-r--r--. 1 user15 user15     0 12月 25 11:20 file15
    -rw-r--r--. 1 user16 user16     0 12月 25 11:20 file16
    -rw-r--r--. 1 user17 user17     0 12月 25 11:20 file17
    -rw-r--r--. 1 user18 user18     0 12月 25 11:20 file18
    -rw-r--r--. 1 user19 user19     0 12月 25 11:20 file19

原创文章,作者:N25-深圳-寻觅,如若转载,请注明出处:http://www.178linux.com/65124

(0)
上一篇 2016-12-28 13:51
下一篇 2016-12-28 16:26

相关推荐

  • 初识Linux—-小练习

    1、怎样更改命令提示符的颜色?     步骤一:通过 echo $PS1 命令显示当前提示符格式;     步骤二:通过cat /etc/DIR_COLORS 命令,查询需要颜色的编号;     步骤三:打开.bashrc文件,命令为nano  .bashrc,进入后添加,如图: &nbs…

    2017-02-18
  • Linux 的发展史

    前言 l  免费源码开放 l  安全性高,漏洞修补快 l  多任务、多使用者 l  多平台支持 l  设备要求低,不耗资源 ……      看到这一系列的优点,在IT人的脑海里只会浮出一个名字Linux. 如果还不够直观的话,有这么一个数据, 显示前500系统中的485…

    Linux干货 2016-10-14
  • Linux之旅1

    Linux之旅1  一、描述计算机的组成及其功能 计算机的组成: 1、计算机主要由:CPU(控制器、运算器)、存储器、输入设备、和输出设备(输入设备、和输出设备又称为I/O设备)四部分组成; 计算机个组成部分的功能: CPU:主要有控制器和运算器组成; 控制器:控制器是整个计算机的中枢神经;其功能是对程序规定的控制信息进行解释,根据其要求进行控制,…

    2016-09-12
  • 8.5文本处理工具

    取本机ip地址 利用head命令取行首两行,tail命令取行尾一行, ifconfig |head -2 |tail -1   利用tr -s命令替换空格为“:”,并合并重复的“:”。利用cut -d:-f3命令,保留以“:”为分割符的第三部分   ifconfig |head -2 |tail -1 |tr -s "…

    Linux干货 2016-08-08
  • bash编程初体验(三)

    bash编程初体验之for for while until 概述 本文将介绍以for为代表的循环语句在shell 脚本中的应用,常见的循环语句有for, while,until,作为循环语句,顾名思义,它就是重复地做一件事,直到满足某一条件而退出;另外,还有两个循环控制语句continue与break来配合循环语句,以实现临时中断或跳出循环的功能;以下为fo…

    Linux干货 2016-08-24
  • sed命令用法

    sed命令 [option]  “script”  file sed命令是一种行编辑工具,他是逐行对文件进行编辑。sed命令有一个模式空间,他将一行命令读取进模式空间进行编辑,完成后将本行数据打印再读取下一行数据进行处理。 option: -n:静默模式不显示模式空间内容 -f:从指定文件中读取 -i:修改源文件(-i.###会自动将原文…

    Linux干货 2016-08-12