Merging of all files based on a condition

Hi Friends,

I am new to UNIX. I need to merge all the files(to FINAL.txt) in single directory based one condition. Out of all the files one of file will have specific value like :GF01: at any where in the file.

so the file which is having :GF01: should be appended at the last.
EX:

abx.txt
xyz.txt
pqr.txt
..
123.txt

If Pqr.txt file has the value :GF01: in it; then pqr.txt files data should be merged at the end of FINAL.txt.

appreciate your help:)

Thanks
Arun

Hi ,
Try this.

./test.pl *.txt

Script

#!/usr/bin/perl

$numArgv = $#ARGV + 1;
foreach $argnum ( 1 ... $numArgv) {
        $filename=shift;
        open (FH,$filename);
        @array=<FH>;
        @found = grep (/:GF01:/,@array);
        $arraysize=@found;
        if ($arraysize == 0 ) {
        `cat $filename >> final.txt`;
        }
        else
        {
        $filepattnotfound=$filename;
        }
        close(FH);
}
`cat $filepattnotfound >> final.txt`

Try this:

ls *.txt |
{ while read file
  do
    if grep :GF01: "$file" > /dev/null 2>&1; then
       gffile="$file"
    else
       cat "$file"
    fi
  done
  cat "$gffile"
} > FINAL.txt

Hi Thanks for the help.But Small problem here. Merging of the second file is not appending from new line.
EX: content of one file :A1.txt

{1:F01DELPUS33AXXX0297067264}{2:O9402307100209SOGEFRPPFXXX26050884481002091708N}{3:{108:EEX}}{4:{CHK:489B}}

Content of second file: A2.txt

{1:F01DELPUS33AXXX0297067258}
:62F:C100209EUR2003573,27
:64:C100209EUR2003573,27
-}{5:{CHK:AA67595BDF43}}

Final.txt output is coming like

{1:F01DELPUS33AXXX0297067264}{2:O9402307100209SOGEFRPPFXXX26050884481002091708N}{3:{108:EEX}}{4:{CHK:489B}}{1:F01DELPUS33AXXX0297067258}
:62F:C100209EUR2003573,27
:64:C100209EUR2003573,27
-}{5:{CHK:AA67595BDF43}}

A2.txt got merged from end of A1.txt(Not started from new line)

But i need the Output like:

{1:F01DELPUS33AXXX0297067264}{2:O9402307100209SOGEFRPPFXXX26050884481002091708N}{3:{108:EEX}}{4:{CHK:489B}}
{1:F01DELPUS33AXXX0297067258}
:62F:C100209EUR2003573,27
:64:C100209EUR2003573,27
-}{5:{CHK:AA67595BDF43}}

Merging of A2.txt should start from new line.

Appreciate your help.
Thanks
Arun

---------- Post updated at 10:36 AM ---------- Previous update was at 10:23 AM ----------

I have received the files in email and i have FTP'd them to UNIX. Does this has any difference.

i have created the same file through VI editor and try to merge the files and out put is working fine.

But when i transfer the same files from windows and try to merge is not working fine.

Appreciate your input.:confused:

Thanks
Arun

You need to select ascii mode (not binary mode) for the ftp transfer otherwise your file does not get converted to a Unix file.

Thanks a lot. It worked fine.