红联Linux门户
Linux帮助

请问:如何用SH脚本实现从一个文件中取出指定的行?

发布时间:2008-08-30 13:16:03来源:红联作者:zhuzhuluoluo
文件1:
239526 121.229.53.235 2008-08-22 11:19:54
303226 122.159.17.142 2008-08-22 16:58:45
......
文件2:
116.21.145.143 239526,http://xxxx.com/cilpage/042.htm,20080822
221.210.29.137 1615383,http://xxxx.com/time/sg2007/news/client_news01.htm,20080822
....

要从文件2中取出满足条件:文件2中的第二个字段在文件1中第一个字段中存在的行,如239526
最后形成文件3:
116.21.145.143 239526,http://xxxx.com/cilpage/042.htm,20080822
......
文章评论

共有 3 条评论

  1. yehhl 于 2009-05-11 08:37:43发表:

    我来学习了~这里真是个不错的地方

  2. haibian 于 2009-05-10 21:51:59发表:

    尚未用到

  3. taomic 于 2008-09-01 21:51:20发表:

    #!/bin/sh
    cat /dev/null>c.txt #清空c.txt
    lines=`wc -l a.txt|awk '{print $1}'` #统计a.txt的行数
    pLine=1
    until [ $pLine -gt $lines ]
    do
    rows=`sed -n "$pLine p" a.txt|wc -w` #统计该行的列数
    pRow=1
    until [ $pRow -gt $rows ]
    do
    case "$pRow" in
    1)a=`sed -n "$pLine p" a.txt|awk '{print $1}'`;;
    2)a=`sed -n "$pLine p" a.txt|awk '{print $2}'`;;
    3)a=`sed -n "$pLine p" a.txt|awk '{print $3}'`;;
    4)a=`sed -n "$pLine p" a.txt|awk '{print $4}'`;;
    5)a=`sed -n "$pLine p" a.txt|awk '{print $5}'`;;
    6)a=`sed -n "$pLine p" a.txt|awk '{print $6}'`;;
    esac
    echo $a
    grep -s -h "$a" b.txt >> c.txt #存入临时文件
    let pRow+=1
    done
    let pLine+=1
    done
    exit 0
    (提醒: ` 与单引号 ' 一定要区分好,还要注意句首空白处一定要用Tab来填)
    如有问题,欢迎指正

    [ 本帖最后由 taomic 于 2008-9-2 08:57 编辑 ]