sed & awk

看到过的最好的一个讲解sed & awk的PPT

http://www.cs.nyu.edu/~mohri/unix08/lect5.pdf

 

现在制作一个自己的版本的ls

sed_ls_fmt

s/^d/dir  /1
s/^-/file /1
s/\([rwxs-]\{3\}\)\([rwxs-]\{3\}\)\([rwxs-]\{3\}\)/\1 \2 \3/
 

 

awk_ls_fmt

NR != 1{
    size = 0
    unit = "B"
 
    if ($8 < 1024)
        size = $8
    else if($8 < 1024 * 1024)
    {
        size = $8/1024 
        unit = "KB"
    }
    else if($8 < 1024 * 1024 * 1024)
    {
        size = $8/1024/1024 
        unit = "MB"
    }
    else
    {
        size = $8/1024/1024/1024 
        unit = "GB"
    }
 
    printf("%6s\tlink:%s\towner:%6s[%s]\tgroup:%6s[%s]\tothers:[%s]\t%6.2f %s\tmtime:[%2s %2s %5s] %s\n", 
        $1, 
        $5, 
        $6, 
        $2, 
        $7, 
        $3, 
        $4, 
        size, 
        unit,
        $9,
        $10,
        $11,
        $12);
}
 

 

ls.sh

ls -l $1 | sed -f sed_ls_fmt | awk -f awk_ls_fmt

 

效果如下:

bash ls.sh ~
file    link:1    owner:daniel[rw-]    group:daniel[r--]    others:[r--]     32.62 MB    mtime:[3月 28 17:33] pin-2.13-62732-gcc.4.4.7-linux.tar.gz
dir    link:2    owner:daniel[rwx]    group:daniel[r-x]    others:[r-x]      4.00 KB    mtime:[2月 12 20:01] 公共的
dir    link:2    owner:daniel[rwx]    group:daniel[r-x]    others:[r-x]      4.00 KB    mtime:[2月 12 20:01] 模板
dir    link:2    owner:daniel[rwx]    group:daniel[r-x]    others:[r-x]      4.00 KB    mtime:[2月 12 20:01] 视频
dir    link:2    owner:daniel[rwx]    group:daniel[r-x]    others:[r-x]      4.00 KB    mtime:[3月  7 15:00] 图片
dir    link:2    owner:daniel[rwx]    group:daniel[r-x]    others:[r-x]      4.00 KB    mtime:[2月 12 20:01] 文档
dir    link:2    owner:daniel[rwx]    group:daniel[r-x]    others:[r-x]      4.00 KB    mtime:[5月 22 17:50] 下载
dir    link:2    owner:daniel[rwx]    group:daniel[r-x]    others:[r-x]      4.00 KB    mtime:[2月 12 20:01] 音乐
dir    link:2    owner:daniel[rwx]    group:daniel[r-x]    others:[r-x]      4.00 KB    mtime:[3月  8 16:26] 桌面

add color theme to output:
NR != 1{
    size = 0
    unit = "B"

    if ($8 < 1024)
        size = $8
    else if($8 < 1024 * 1024)
    {
        size = $8/1024 
        unit = "KB"
    }
    else if($8 < 1024 * 1024 * 1024)
    {
        size = $8/1024/1024 
        unit = "\033[;31mMB\033[0m"
    }
    else
    {
        size = $8/1024/1024/1024 
        unit = "\033[;34mGB\033[0m"
    }

    if ($1 == "file")
    {
        $1 = "\033[;34mfile\033[0m"
    }
    else
    {
        $1 = "folder"
    }

    printf("%6s\tlink:%s\towner:%6s[%s]\tgroup:%6s[%s]\tothers:[%s]\t%6.2f %s\tmtime:[%2s %2s %5s] %s\n", 
        $1, 
        $5, 
        $6, 
        $2, 
        $7, 
        $3, 
        $4, 
        size, 
        unit,
        $9,
        $10,
        $11,
        $12);
}

 



转载于:https://www.cnblogs.com/long123king/p/3747251.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值