[博主公告]

“linux大棚”是一个以Linux技术专题为主的博客。
本博客为了保证读者的浏览体验,决定不刊登任何广告信息。

专题

文章发布时间日历

September 2010
M T W T F S S
« Aug    
 12345
6789101112
13141516171819
20212223242526
27282930  
    <<返回主页

  • 25Oct

    最近碰到两道脚本题:

    1、写脚本实现,可以用shell、perl等。在目录/tmp下找到100个以abc开头的文件,然后把这些文件的第一行保存到文件new中。
    2、写脚本实现,可以用shell、perl等。把文件b中有的,但是文件a中没有的所有行,保存为文件c,并统计c的行数。
    解答思路:

    1:觉得用shell更好一些,代码如下

    #!/bin/sh
    for filename in `find /tmp -type f -name "abc*"|head -n 100`
    do
    sed -n '1p' $filename>>new
    done

    注释:第一,用到了find命令,其中-type f表示选取普通文件,-name用于设定文件名;第二,head -n 100命令用于取出前100项。第三,sed -n ‘1p’用于取出文件的第一行内容。第四,>>new表示追加到文件new中。
    2:第二个题目还是用perl更方便一些,代码如下:
    [rocrocket@wupengchong shellstudy]$ cat roc.pl

    #!/usr/bin/perl -w
    open B,"<b" or die "b error.($!)";
    open C,">c" or die "c error.($!)";
    my $find=0;
    my ($myb,$mya);
    while(<B>){
    	$find=0;
    	chomp($myb=$_);
    	print "B ",$myb,"\n";
    	open A,"<a" or die "a error.($!)";
    	while(<A>){
    		chomp($mya=$_);
    		print "A ",$mya,"\n";
    		if($mya=~/$myb/){
    			$find=1;
    			print "FIND!\n";
    		}
    	}
    	if($find==0){
    		print C $myb,"\n";
    	}
    }
    close A;
    close B;
    close C;
    $linenum=`cat c|wc -l`;
    print "c line count:",$linenum;

    第二题一直没有很好的思路,就只好用笨方法实现了。
    看看哪位能有更好的方法和思路?希望能留言交流:D
    当然shell、perl、python都欢迎!
    over~

    我猜您对这些文章感兴趣:

    Posted by rocrocket @ 7:52 pm

    Tags: , , , , ,

    3,022人阅读过了这篇文章。

    如果您还满意我的文章,请您订阅我的博客。点击“我要订阅”即可。谢谢:)

  • <<返回主页

16 Responses

WP_Cloudy
  • Shell.E.Xu Says:

    with open(“b”,”r”) as f:
    set1 = set(f.readlines ());
    with open(“a”,”r”) as f:
    set2 = set(f.readlines ());
    count = 0;
    with open(“c”, w) as f:
    for line in list(set1 – set2):
    f.write (line);
    count += 1;
    print count;

    回复

  • Shell.E.Xu Says:

    TMD又没有格式,凑合看看吧。
    用了2.6的with语法,简洁多了。2.5以下需要启用或者干脆自己close。
    其实核心就一句set1 – set2

    回复

  • dongbin Says:

    find /tmp -type f -name “abc*”|head -n 100|xargs head -q -n 1

    回复

  • dongbin Says:

    第二题 Ruby实现:

    #!/usr/bin/ruby

    File.open(ARGV[2]) do |f|
    f.write((File.readlines(ARGV[0]) – File.readlines(ARGV[1])).join(“\n”))
    end

    回复

  • dongbin Says:

    第二题Ruby实现

    File.open(ARGV[2]) do |f|
    f.write((File.readlines(ARGV[0]) – File.readlines(ARGV[1])).join(“\n”))
    end

    回复

  • dongbin Says:

    第二题Ruby实现:

    File.open(ARGV[2]) do |f|
    f.write((File.readlines(ARGV[0]) – File.readlines(ARGV[1])).join(“\n”))
    end

    回复

  • sanding Says:

    不懂perl。。。
    awk ‘NR==FNR{a[$0]=1}NR!=FNR&&!a[$0]{print > “c”}’ a b

    回复

    qsxing Reply:

    @sanding, 能解释一些是什么意思吗?

    回复

  • kylexlau Says:

    第二题解法:

    1 sort a b | uniq -u | tee c | wc -l
    2 grep -vFf a b | tee c | wc -l

    第一题这个比较短:
    find /tmp -type f -name “abc*” | head -n 100 | xargs head -q -n 1 >> new

    回复

  • Lee.MaRS Says:

    [引用开始]
    第二题解法:

    1 sort a b | uniq -u | tee c | wc -l
    2 grep -vFf a b | tee c | wc -l
    [引用结束]

    方法1不正确。原题中”文件b中有的,但是文件a中没有的所有行”,而不是”文件b中有的但是文件a中没有的,或者,文件a中有的但是文件b中没有的”。

    方法2还差一步,grep时要指定-x参数,表示是整行匹配。

    回复

  • Noel Says:

    第二个可以用comm

    sort a > a2
    sort b > b2
    comm -23 a2 b2 | wc -l

    回复

  • laurel Says:

    1:

    dir ./test* | xargs tail -qn1 >t
    or:
    for i in `dir ./test*`; do tail -n1 $i > tt; done

    2:

    diff t1 t2 | grep “<" | sed 's/^ tt ; cat tt | wc -l

    回复

  • laurel Says:

    alert(“test”);

    回复

  • zllzh Says:

    第二题
    办法以比较笨
    diff b a | sed ‘/^>/d’| sed ’s/c

    回复

  • zllzh Says:

    突然想到是否可能用awk???

    回复

    rocrocket Reply:

    完全可以:)
    有兴趣可以编写一个,分享上来

    回复

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.