Chinaunix

标题: 每三个文件合并为一个大文件 [打印本页]

作者: addictlinux    时间: 2010-09-27 20:22
标题: 每三个文件合并为一个大文件
在某个目录下有 file01,file02,file03,...,file99 个文件
   现在想按顺序每三个文件合并一个大文件并重新命名
   如
     file01,file02,file03合并位大文件的名称位 :file01-03
        其它以此类推。
     问问大家有什么好的方法?
     我的有点繁琐,
作者: addictlinux    时间: 2010-09-27 20:39
如有三个文件
  cat file01

  1.    set arrow from 7,1633 to 8,1383
  2.    set label "9575(100916104th)" at 6.5,1683
  3.    plot "diff_029.file" with linespoints
复制代码
cat file02

  1.    set arrow from 7,1633 to 8,1383
  2.    set label "9575(100916104th)" at 6.5,1683
  3.    plot "diff_030.file" with linespoints
复制代码
cat file03

  1.    set arrow from 7,1633 to 8,1383
  2.    set label "9575(100916104th)" at 6.5,1683
  3.    plot "diff_031.file" with linespoints
复制代码
合并新文件名为:file01-03
  cat file01-03

  1.    set arrow from 7,1633 to 8,1383
  2.    set label "9575(100916104th)" at 6.5,1683
  3.     set arrow from 7,1633 to 8,1383
  4.    set label "9575(100916104th)" at 6.5,1683
  5.    set arrow from 7,1633 to 8,1383
  6.    set label "9575(100916104th)" at 6.5,1683
  7.    plot "diff_029.file" with linespoints, "diff_030.file" with linespoints,"diff_031.file" with linespoints
  8.   
复制代码

作者: blackold    时间: 2010-09-27 21:04
你的方法呢?
作者: sunbw001    时间: 2010-09-27 21:06
回复 1# addictlinux


    你的所有这些文件都是这样的吗?都只有这么几行吗?
作者: blackold    时间: 2010-09-27 21:08
灰太郎来了,还有什么搞不定?
作者: blackold    时间: 2010-09-27 21:10
cat file01 file02 file03>file01-03
cat file04 file05 file06>file04-06
cat file07 file08 file09>file07-09
cat file10 file11 file12>file10-12
cat file13 file14 file15>file13-15
cat file16 file17 file18>file16-18
cat file19 file20 file21>file19-21
cat file22 file23 file24>file22-24
cat file25 file26 file27>file25-27
cat file28 file29 file30>file28-30
cat file31 file32 file33>file31-33
cat file34 file35 file36>file34-36
cat file37 file38 file39>file37-39
cat file40 file41 file42>file40-42
cat file43 file44 file45>file43-45
cat file46 file47 file48>file46-48
cat file49 file50 file51>file49-51
cat file52 file53 file54>file52-54
cat file55 file56 file57>file55-57
cat file58 file59 file60>file58-60
cat file61 file62 file63>file61-63
cat file64 file65 file66>file64-66
cat file67 file68 file69>file67-69
cat file70 file71 file72>file70-72
cat file73 file74 file75>file73-75
cat file76 file77 file78>file76-78
cat file79 file80 file81>file79-81
cat file82 file83 file84>file82-84
cat file85 file86 file87>file85-87
cat file88 file89 file90>file88-90
cat file91 file92 file93>file91-93
cat file94 file95 file96>file94-96
cat file97 file98 file99>file97-99

作者: addictlinux    时间: 2010-09-27 21:12
回复 4# sunbw001


    呵呵,想从固定行上动脑筋。
   每个文件行数大小不一。
作者: where27    时间: 2010-09-27 21:12
黑哥太有才了
作者: blackold    时间: 2010-09-27 21:18
回复 8# where27


    前段时间发大水,你是不是也大赚了一把?
作者: blackold    时间: 2010-09-27 21:20
回复 2# addictlinux


    嗯?是这样合并啊,那我的方法不对。
作者: sunbw001    时间: 2010-09-27 21:34
本帖最后由 sunbw001 于 2010-09-27 21:40 编辑

回复 2# addictlinux


    黑哥竟笑话我

试试这个:
  1. ls file* |xargs -n3|while read str1 str2 str3; do awk '/^set/{print}/^plot/{$1=s?"":$1;s=s?s","$0:$0}END{print s}' $str1 $str2 $str3 >../file${str1:4:2}-${str3:4:2}; done
复制代码

作者: blackold    时间: 2010-09-27 21:41
回复 11# sunbw001
不是笑话你啊,
你的命令不错啊。

作者: addictlinux    时间: 2010-09-27 23:14
本帖最后由 addictlinux 于 2010-09-27 23:15 编辑
  1. #!/bin/sh
  2.   SAVEIFS=$IFS;
  3.   IFS=$'\n'
  4.   rm -fr *-*.gnuplot;
  5.   
  6.   ls *.gnuplot |grep -v "-" | paste -d " " - - - - > tempfile

  7.   for I in `cat tempfile`
  8.        do
  9.           flag=0;
  10.          #j=1;
  11.           name1=`echo "$I" | cut -d " " -f1`;num1=${name1//[a-z_.]}; #newn1=${name1%.*};
  12.           name2=`echo "$I" | cut -d " " -f2`; #newn2=${name2%.*}
  13.           name3=`echo "$I" | cut -d " " -f3`; #newn3=${name3%.*}
  14.           name4=`echo "$I" | cut -d " " -f4`; #num4=${name4//[a-z_.]};newn4=${name4%.*};

  15.            name=("$name1" "$name2" "$name3" "$name4");

  16.            for n in `seq 1 4`
  17.                do
  18.                    if [ ! -z ${name[n]} ]; then
  19.                       let "++flag";
  20.                    fi
  21.                 done

  22. #        echo $flag;
  23.           namestr=${name[flag]//[a-z_.]};

  24.           for m in $name1 $name2 $name3 $name4
  25.                do
  26.                    cat $m>>diff_$num1-$namestr.gnuplot
  27.                done

  28.                 echo -n "plot" >>diff_$num1-$namestr.gnuplot

  29.                  let "num=$flag-1" ;

  30.                for p in `seq 0  $num`
  31.                    do
  32.                     echo -n " \"${name[p]%.*}.file\" with linespoints," >>diff_$num1-$namestr.gnuplot
  33.                    done

  34.                echo "\"${name[flag]%.*}.file\" with linespoints">>diff_$num1-$namestr.gnuplot;
  35.        done

  36.   IFS=$SAVEIFS;
复制代码
衍生问题:name1="abcd"
                j=1;
                ${name$j} 对么? 如何得到name1的结果。// 上文中绕了一大圈就是为了避免此问题。
作者: sunbw001    时间: 2010-09-27 23:31
本帖最后由 sunbw001 于 2010-09-27 23:39 编辑

回复 13# addictlinux
  1. [root@localhost ~]# name1="abcd"
  2. [root@localhost ~]# j=1
  3. [root@localhost ~]# eval echo \$name$j
  4. abcd
  5. [root@localhost ~]#
  6. [root@localhost ~]# name1="abcd"
  7. [root@localhost ~]# j=1
  8. [root@localhost ~]# eval str=\$name$j
  9. [root@localhost ~]# echo $str
  10. abcd
  11. [root@localhost ~]#
复制代码

作者: iori809    时间: 2010-09-28 08:56

作者: lkk2003rty    时间: 2010-09-28 09:26
回复 15# iori809


    最近怎么不见你了。。。。。
作者: iori809    时间: 2010-09-28 09:33
回复 16# lkk2003rty


    让大家挂念啦~好感动~最近在忙个项目~时间上确实有些紧张。不过CU已经是我生活的一部分了
我会坚持来的~谢谢大家~
作者: magnet2008    时间: 2010-09-28 09:56
for num in $(seq -w 1 99)
        do
                if [ $(echo $num%3|bc) -ne 0 ]; then
                        str=${str}" file"$num;
                        else str=${str}" file"$num;
                        echo $str|perl -ne '{chomp;$str=$_;$first=substr($str,4,2);$end=substr($str,18,2);print "cat $str >file$first-$end\n";}'
                        str=
                        fi
                        done
这个是打印了命令,然后再用shell执行即可。
perl也可以利用system调用命令,直接执行命令
作者: where27    时间: 2010-09-28 10:14
回复 9# blackold


    何止发大水啊,还有什么拆迁自 焚啊,建什么铅厂啊,假酒什么的,害的我都不敢回去了
作者: 网速20M    时间: 2014-10-14 19:29
本帖最后由 网速20M 于 2014-10-14 19:31 编辑

转自   http://bbs.chinaunix.net/thread-1790335-1-1.html   awk 'FNR==1&&ARGIND%3==1{if(ARGIND>1){print s>f;s=""};f=sprintf("file%02d-%02d",ARGIND,ARGIND+2)}/^set/{print $0>f}/^plot/{$1=s?""1;s=s?s","$00}END{print s>f}' file[0-9][0-9]
他妹的那个笑脸为:$




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2