首页 > 系统服务 > 详细

leetcode 的shell部分4道题整理

时间:2015-08-16 15:09:48      阅读:287      评论:0      收藏:0      [点我收藏+]

对shell的某些细节还不是十分熟悉,借鉴了好多别人的东西

1. Word Frequency
 
    此题很简单,只要能排序就可以
    
    cat words.txt |tr -s " " "\n" sort | unique -c | sort -r | awk ‘{print $2" "$1}‘
 
2. 

Valid Phone Numbers

 
cat file.txt | awk ‘/^\d{3}-\d{3}-\d{4}$/| /^\([0-9]{3}\) [0-9]{3}-[0-9]{4}$/‘
 
3.

Transpose File

 cat file.txt| awk ‘{
    max_nf = NF
    max_nr = NR
    for(x=1;x<=max_nf;x++){
        vector[x,NR]=$x;
    }
}
 
 
END{
    for(x = 1;x <= max_nf;x++){
        for( y = 1;y <= max_nr ; y++){
            printf("%s",vector[x,y])
            if (y < max_nr)  
                printf(" ")
        }
    if (x < max_nf)  
        printf("\n")
}
}
 
4.

Tenth Line

 cat file.txt|awk ‘NR==10‘

leetcode 的shell部分4道题整理

原文:http://www.cnblogs.com/chengxuyuanxiaowang/p/4734280.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!