zoukankan      html  css  js  c++  java
  • 字符截取:cut,格式化输出:printf,字符截取:awk,文件或命令输出编辑:sed

    cut 选项 文件名

    -f 列号  提取第几列

    -d 分隔符  指定分隔符把行分成多列

    不能以空格为分隔符。

    [root@localhost ~]# cat testfile
    no.     name    sex     score
    1       zhangsan        m       88
    2       lisi    f       89
    3       wangwu  m       87
    [root@localhost ~]# cut -f 2 testfile  注释:默认分隔符是制表符	
    name
    zhangsan
    lisi
    wangwu
    [root@localhost ~]# cut -f 2,4 testfile
    name    score
    zhangsan        88
    lisi    89
    wangwu  87
    [root@localhost ~]# cat testfile
    no.     na:me   sex     sco:re
    1       zhang:san       m       8:8
    2       li:si   f       8:9
    3       wang:wu m       8:7
    [root@localhost ~]# cut -f 1,3 -d : testfile
    no.     na:re
    1       zhang:8
    2       li:9
    3       wang:7
    root@localhost ~]# cut -f 1 -d : /etc/passwd|grep xiong
    xiongjiawei
    [root@localhost ~]# grep xiong /etc/passwd|cut -d : -f 1
    xiongjiawei
    [root@localhost ~]# grep xiong /etc/passwd|cut -d ":" -f 1
    xiongjiawei
    [root@localhost ~]# grep xiong /etc/passwd|cut -f 1 -d :
    xiongjiawei
    [root@localhost ~]# grep xiong /etc/passwd|cut -f 1 -d ":" 
    xiongjiawei 

    printf '格式' 输出内容

    %ns  输出n个字符串

    %ni  输出n个数字

    %n.mf  输出共n位数字,m位小数,如%5.2f表示共3位整数,2位小数

    a  警告声

      Backspace键

    f  清屏

      换行,常用

      Enter,常用

      Tab,常用

    v  垂直Tab

    awk支持printf和print(linux默认无此命令),print输出会自动加换行,printf是标准格式输出命令,不会自动加换行,如果要换行需要手动加。

    [root@localhost ~]# printf %s a b cd ef g 1 2
    abcdefg12[root@localhost ~]# printf %s %s a b cd ef g 1 2  
    %sabcdefg12[root@localhost ~]# printf '%s %s' a b cd ef g 1 2
    a bcd efg 12 [root@localhost ~]# printf '%s
    %s' a b cd ef g 1 2 
    a
    bcd
    efg
    12
    [root@localhost ~]# printf '%s %s
    ' a b cd ef g 1 2  
    a b
    cd ef
    g 1
    2 
    [root@localhost ~]# printf %s $(cat testfile)
    no.na:mesexsco:re1zhang:sanm8:82li:sif8:93wang:wum8:7[root@localhost ~]# 
    [root@localhost ~]# cat testfile
    no.     na:me   sex     sco:re
    1       zhang:san       m       8:8
    2       li:si   f       8:9
    3       wang:wu m       8:7

    awk '条件1{动作1} 条件2{运行2}...' 文件名

    默认以空格为分隔符,执行命令时首先读取文件一行。

    [root@localhost ~]# cat testfile
    no.     name    sex     score   age     comment
    1       zhansan m       88      18      student
    2       li:si   f       89      20      member
    3       wangwu  m       87      22      teacher
    [root@localhost ~]# awk {printf $2 $4} testfile
    awk: cmd. line:1: {printf
    awk: cmd. line:1:        ^ unexpected newline or end of string  注释:报错的原因是awk命令后的条件动作未加单引号
    [root@localhost ~]# awk '{printf $2 $4}' testfile
    namescorezhansan88li:si89wangwu87[root@localhost ~]# awk '{printf $2	$4
    }' testfile  
    awk: cmd. line:1: {printf $2	$4
    }
    awk: cmd. line:1:           ^ backslash not last character on line
    awk: cmd. line:1: {printf $2	$4
    }
    awk: cmd. line:1:           ^ syntax error  注释:报错是因为制表符	未加双引号
    [root@localhost ~]# awk '{printf $2 "	" $4 
    }' testfile
    awk: cmd. line:1: {printf $2 "	" $4 
    }
    awk: cmd. line:1:                    ^ backslash not last character on line
    awk: cmd. line:1: {printf $2 "	" $4 
    }
    awk: cmd. line:1:                    ^ syntax error  注释:报错是因为换行符
    未加双引号
    [root@localhost ~]# awk '{printf $2 "	" $4 "
    "}' testfile
    name    score
    zhansan 88
    li:si   89
    wangwu  87
    [root@localhost ~]# awk '{printf $2"	"$4"
    "}' testfile   
    name    score
    zhansan 88
    li:si   89
    wangwu  87
    [root@localhost ~]# df -h|awk '{printf $1}'
    文件系统/dev/sda5devtmpfstmpfstmpfstmpfs/dev/sda2/dev/sdb5/dev/sdb1/dev/sda1tmpfs[root@localhost ~]# df -h|awk '{printf $1"
    "}'
    文件系统
    /dev/sda5
    devtmpfs
    tmpfs
    tmpfs
    tmpfs
    /dev/sda2
    /dev/sdb5
    /dev/sdb1
    /dev/sda1
    tmpfs
    [root@localhost ~]# df -h|awk '{print $1}'     
    文件系统
    /dev/sda5
    devtmpfs
    tmpfs
    tmpfs
    tmpfs
    /dev/sda2
    /dev/sdb5
    /dev/sdb1
    /dev/sda1
    tmpfs
    [root@localhost ~]# df
    文件系统          1K-块    已用     可用 已用% 挂载点
    /dev/sda5      16558080 1443112 15114968    9% /
    devtmpfs         490168       0   490168    0% /dev
    tmpfs            499968       0   499968    0% /dev/shm
    tmpfs            499968    6872   493096    2% /run
    tmpfs            499968       0   499968    0% /sys/fs/cgroup
    /dev/sda2       2086912   33160  2053752    2% /home
    /dev/sdb5       1998672    6144  1871288    1% /disk5
    /dev/sdb1        999320    2564   927944    1% /disk1
    /dev/sda1        201380  116576    84804   58% /boot
    tmpfs             99996       0    99996    0% /run/user/0
    [root@localhost ~]# df -h|grep sda5
    /dev/sda5        16G  1.4G   15G    9% /
    [root@localhost ~]# df -h|grep sda5|awk '{print $5}'
    9%
    [root@localhost ~]# df -h|grep sda5|awk '{print $5}'|cut -d % -f1  注释:cut命令的-f选项后的列序号和f之间可以无空格
    9
    [root@localhost ~]# df -h|grep sda5|awk '{print $5}'|cut -d % -f 1
    9
    [root@localhost ~]# df -h|grep sda5|awk 'BEGIN{print "This is a title:"}{print $5}'|cut -d % -f 1              
    This is a title:
    9
    [root@localhost ~]# df -h|grep sda5|awk '{print "This is a title:"}{print $5}'|cut -d % -f1      
    This is a title:
    9
    [root@localhost ~]# awk '{print $1}' /etc/passwd  注释:awk的默认分隔符是空格
    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/sbin/nologin
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    adm:x:3:4:adm:/var/adm:/sbin/nologin
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    operator:x:11:0:operator:/root:/sbin/nologin
    games:x:12:100:games:/usr/games:/sbin/nologin
    ftp:x:14:50:FTP
    nobody:x:99:99:Nobody:/:/sbin/nologin
    systemd-bus-proxy:x:999:997:systemd
    systemd-network:x:192:192:systemd
    dbus:x:81:81:System
    polkitd:x:998:996:User
    libstoragemgmt:x:997:995:daemon
    abrt:x:173:173::/etc/abrt:/sbin/nologin
    rpc:x:32:32:Rpcbind
    tss:x:59:59:Account
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    sshd:x:74:74:Privilege-separated
    ntp:x:38:38::/etc/ntp:/sbin/nologin
    chrony:x:996:994::/var/lib/chrony:/sbin/nologin
    tcpdump:x:72:72::/:/sbin/nologin
    xiongjiawei:x:1000:1000:xiongjiawei:/home/xiongjiawei:/bin/bash
    qiaofeng:x:1001:1001::/home/qiaofeng:/bin/bash
    yangguo:x:1002:1002::/home/yangguo:/bin/bash
    st:x:1003:1004::/home/st:/bin/bash
    user1:x:1004:1005::/home/user1:/bin/bash
    [root@localhost ~]# awk '{FS=":"}{print $1}' /etc/passwd  
    root:x:0:0:root:/root:/bin/bash  注释:设置分隔符为冒号:后第一行并未处理,因为awk命令的执行是首先读取第一行
    bin
    daemon
    adm
    lp
    sync
    shutdown
    halt
    mail
    operator
    games
    ftp
    nobody
    systemd-bus-proxy
    systemd-network
    dbus
    polkitd
    libstoragemgmt
    abrt
    rpc
    tss
    postfix
    sshd
    ntp
    chrony
    tcpdump
    xiongjiawei
    qiaofeng
    yangguo
    st
    user1
    [root@localhost ~]# awk 'BEGIN{FS=":"}{print $1}' /etc/passwd   注释:加BEGIN后就不会首先读取第一行,而是首先设置分隔符
    root
    bin
    daemon
    adm
    lp
    sync
    shutdown
    halt
    mail
    operator
    games
    ftp
    nobody
    systemd-bus-proxy
    systemd-network
    dbus
    polkitd
    libstoragemgmt
    abrt
    rpc
    tss
    postfix
    sshd
    ntp
    chrony
    tcpdump
    xiongjiawei
    qiaofeng
    yangguo
    st
    user1
    [root@localhost ~]# awk 'END{print "The end!"}BEGIN{FS=":"}{print $1}' /etc/passwd        注释:有BEGIN就有END,在命令动作中的顺序无所谓              
    root
    bin
    daemon
    adm
    lp
    sync
    shutdown
    halt
    mail
    operator
    games
    ftp
    nobody
    systemd-bus-proxy
    systemd-network
    dbus
    polkitd
    libstoragemgmt
    abrt
    rpc
    tss
    postfix
    sshd
    ntp
    chrony
    tcpdump
    xiongjiawei
    qiaofeng
    yangguo
    st
    user1
    The end!
    [root@localhost ~]# cat testfile
    no.     name    sex     score   age     comment
    1       zhansan m       88      18      student
    2       li:si   f       89      20      member
    3       wangwu  m       87      22      teacher
    [root@localhost ~]# awk '$4>88{print $2}' testfile  注释:根据条件执行动作
    name
    li:si

    sed

     sed [选项] '[动作]' 文件名

    选项:

    -n 只输出sed处理过的行

    -i 修改原文件

    -e 执行多条sed动作

    动作:

    p 打印,例2p打印第2行,2,3p打印第2、3行

    d 删除,例‘3,4d',不输出第3、4行

    a 追加,例'2a Welcome to China',在第2行后换行追加Welcom to China

    i 插入,例'2i Welcom to China',在第2行前插入Welcom to China,即在第1行后换行插入Welcom to China

    c 行替换

    s 字符串替换

    [root@localhost ~]# cat testfile
    no.     name    sex     score   age     comment
    1       zhansan m       88      18      student
    2       li:si   f       89      20      member
    3       wangwu  m       87      22      teacher
    [root@localhost ~]# sed '2p' testfile  注释:不加选项-n时即输出原文件又输出经过处理的结果
    no.     name    sex     score   age     comment
    1       zhansan m       88      18      student
    1       zhansan m       88      18      student
    2       li:si   f       89      20      member
    3       wangwu  m       87      22      teacher
    [root@localhost ~]# sed -n '2p' testfile
    1       zhansan m       88      18      student
    [root@localhost ~]# sed -n '2,3p' testfile
    1       zhansan m       88      18      student
    2       li:si   f       89      20      member
    [root@localhost ~]# df -h|sed -n '2p'
    /dev/sda5        16G  1.4G   15G    9% /
    [root@localhost ~]# sed '3,4d' testfile
    no.     name    sex     score   age     comment
    1       zhansan m       88      18      student
    [root@localhost ~]# df -h|sed '2a This is added words'
    文件系统        容量  已用  可用 已用% 挂载点
    /dev/sda5        16G  1.4G   15G    9% /
    This is added words
    devtmpfs        479M     0  479M    0% /dev
    tmpfs           489M     0  489M    0% /dev/shm
    tmpfs           489M  6.8M  482M    2% /run
    tmpfs           489M     0  489M    0% /sys/fs/cgroup
    /dev/sda2       2.0G   33M  2.0G    2% /home
    /dev/sdb5       2.0G  6.0M  1.8G    1% /disk5
    /dev/sdb1       976M  2.6M  907M    1% /disk1
    /dev/sda1       197M  114M   83M   58% /boot
    tmpfs            98M     0   98M    0% /run/user/0
    [root@localhost ~]# df -h|sed '2i This is added words' 
    文件系统        容量  已用  可用 已用% 挂载点
    This is added words
    /dev/sda5        16G  1.4G   15G    9% /
    devtmpfs        479M     0  479M    0% /dev
    tmpfs           489M     0  489M    0% /dev/shm
    tmpfs           489M  6.8M  482M    2% /run
    tmpfs           489M     0  489M    0% /sys/fs/cgroup
    /dev/sda2       2.0G   33M  2.0G    2% /home
    /dev/sdb5       2.0G  6.0M  1.8G    1% /disk5
    /dev/sdb1       976M  2.6M  907M    1% /disk1
    /dev/sda1       197M  114M   83M   58% /boot
    tmpfs            98M     0   98M    0% /run/user/0
    [root@localhost ~]# df -h|sed '2i This is added words'
    文件系统        容量  已用  可用 已用% 挂载点
    This is dded words
    /dev/sda5        16G  1.4G   15G    9% /
    devtmpfs        479M     0  479M    0% /dev
    tmpfs           489M     0  489M    0% /dev/shm
    tmpfs           489M  6.8M  482M    2% /run
    tmpfs           489M     0  489M    0% /sys/fs/cgroup
    /dev/sda2       2.0G   33M  2.0G    2% /home
    /dev/sdb5       2.0G  6.0M  1.8G    1% /disk5
    /dev/sdb1       976M  2.6M  907M    1% /disk1
    /dev/sda1       197M  114M   83M   58% /boot
    tmpfs            98M     0   98M    0% /run/user/0
    [root@localhost ~]# df -h|sed '2i This is 
    > added words' 
    文件系统        容量  已用  可用 已用% 挂载点
    This is 
    added words
    /dev/sda5        16G  1.4G   15G    9% /
    devtmpfs        479M     0  479M    0% /dev
    tmpfs           489M     0  489M    0% /dev/shm
    tmpfs           489M  6.8M  482M    2% /run
    tmpfs           489M     0  489M    0% /sys/fs/cgroup
    /dev/sda2       2.0G   33M  2.0G    2% /home
    /dev/sdb5       2.0G  6.0M  1.8G    1% /disk5
    /dev/sdb1       976M  2.6M  907M    1% /disk1
    /dev/sda1       197M  114M   83M   58% /boot
    tmpfs            98M     0   98M    0% /run/user/0
    [root@localhost ~]# sed '2c deleted...' testfile
    no.     name    sex     score   age     comment
    deleted...
    2       li:si   f       89      20      member
    3       wangwu  m       87      22      teacher
    [root@localhost ~]# sed '2c deleted...
    > new lines!!!' testfile
    no.     name    sex     score   age     comment
    deleted...
    new lines!!!
    2       li:si   f       89      20      member
    3       wangwu  m       87      22      teacher
    [root@localhost ~]# cat testfile
    no.     name    sex     score   age     comment
    1       zhansan m       88      18      student
    2       li:si   f       89      20      member
    3       wangwu  m       87      22      teacher
    
    [root@localhost ~]# sed '3s/li:si/lisi/g' testfile   注释:2表示行序号,不加数字表示替换整个文档
    no.     name    sex     score   age     comment
    1       zhansan m       88      18      student
    2       lisi    f       89      20      member
    3       wangwu  m       87      22      teacher
    [root@localhost ~]# cat testfile
    no.     name    sex     score   age     comment
    1       zhansan m       88      18      student
    2       li:si   f       89      20      member
    3       wangwu  m       87      22      teacher
    [root@localhost ~]# sed -i '3s/li:si/lisi/g' testfile  注释:-i选项表示修改原文件,此选项有一定风险性,慎用,如果需要修改文件内容建议使用vim
    [root@localhost ~]# cat testfile                     
    no.     name    sex     score   age     comment
    1       zhansan m       88      18      student
    2       lisi    f       89      20      member
    3       wangwu  m       87      22      teacher
    [root@localhost ~]# sed -e '2s/zhansan/sanzhan/g;3s/li/Li/g' testfile
    no.     name    sex     score   age     comment
    1       sanzhan m       88      18      student
    2       Lisi    f       89      20      member
    3       wangwu  m       87      22      teacher
  • 相关阅读:
    mac 10.15.7 修改PATH
    oc 属性类型一般用法
    ubuntu解压zip文件名乱码
    telnet 退出
    docker 根据容器创建镜像
    mac android adb device 没有显示设备
    Yii2 查看所有的别名 alias
    Yii2 App Advanced 添加 .gitignore
    ubuntu 18.04 搜狗突然就提示乱码
    An error occured while deploying the file. This probably means that the app contains ARM native code and your Genymotion device cannot run ARM instructions. You should either build your native code to
  • 原文地址:https://www.cnblogs.com/xiongjiawei/p/7353639.html
Copyright © 2011-2022 走看看