Unix ksh: merge files with selected content

Dear Experts,

I have 2 files with information that need to be merged into 1 file.

File1:
 
CPUNAME BHP-027
  DESCRIPTION "SAP - HR  - sapbhp | HR BI ABAP Production - R/3 Agent for SAPBHP"
  OS OTHER
  NODE null TCPADDR 31111
END

CPUNAME EBZAPPP01
  DESCRIPTION "SUN - ECOM -  EBIZ APP01 Prod Sun FTA"
  OS UNIX
  NODE ebzappp01 TCPADDR 31111
END

CPUNAME UTLAPPP04
  DESCRIPTION "SUN - UTLAP -  Sun Zone System"
  OS UNIX
  NODE utlappp04 TCPADDR 31111
END

Open in new window


File2:
 
BHP-027#ZHRBI_PC_TRIG_EMPL_SELF_FILE
BHP-027#ZHRBI_PC_TRIG_HDCTN_TRN_FILE
BHP-027#ZHRBI_PC_TRIG_PACTN_TRN_FILE
EBZAPPP01#APACHESTART
EBZAPPP01#DMGRSTART
EBZAPPP01#DMGRSTOP
EBZAPPP01#EWTUDMUP
EBZAPPP01#EWTUFTUP
UTLAPPP03#SYSUDN00
UTLAPPP03#SYSUUP00
UTLAPPP04#AHPUSTART

Open in new window


The result would need to become:

BHP-027#ZHRBI_PC_TRIG_EMPL_SELF_FILE,SAP,HR
BHP-027#ZHRBI_PC_TRIG_HDCTN_TRN_FILE,SAP,HR
BHP-027#ZHRBI_PC_TRIG_PACTN_TRN_FILE,SAP,HR
EBZAPPP01#APACHESTART,SUN,ECOM
EBZAPPP01#DMGRSTART,SUN,ECOM
EBZAPPP01#DMGRSTOP,SUN,ECOM
EBZAPPP01#EWTUDMUP,SUN,ECOM
EBZAPPP01#EWTUFTUP,SUN,ECOM
UTLAPPP03#SYSUDN00,SUN,UTLAP
UTLAPPP03#SYSUUP00,SUN,UTLAP
UTLAPPP04#AHPUSTART,SUN,UTLAP

That means that from File2 from every line the value before # is taken, that value is searched in File1, from the line below ("DESCRIPTION")  the first 2 values are copied back to File1.
If that would become commas instead of dashes that would be nice, but this can also be done afterwards, so that's not the real issue.

I hope this is possible.
Many thanks.
WatnogAsked:
Who is Participating?
 
point_pleasantCommented:
here is a very simple perl script to execute it run "perl progname.pl"  let me know




#!/usr/local/bin/perl

open (INFILE2, "<./mfile2");
while (<INFILE2>) {
        @mach_name = split/\#/,$_;
        open (INFILE1, "<./mfile1");
        while (<INFILE1>) {
                if (  $_ =~ /^CPUNAME $mach_name[0]/ ) {
                        $nextline = <INFILE1>;
                        $nextline =~ tr/ //s;
                        $nextline =~ tr/"//d;
                        @myline = split / /,$nextline;
                        chomp($mach_name[1]);
                        $mach_name[1] =~ tr/ //d;
                        print "$mach_name[0]#$mach_name[1],$myline[2],$myline[4]\n";
                }
        }
        close (INFILE1);
}
0
 
point_pleasantCommented:
mfiole2 isd you second file mfile1 is first there was no appo3 data in file 1


for i in `cat mfile2`
do
        tag=`echo $i | cut -f1 -d'#'`
        tag2=`grep -A 1 $tag mfile1`
        tag=`echo $tag2 | cut -f4,6 -d' ' | tr -d '"'`
        tag2=`echo $tag | cut -f2 -d' '`
        tag1=`echo $tag | cut -f1 -d' '`
        echo "$i,$tag1,$tag2"
done

0
 
WatnogAuthor Commented:
Thanks, but grep -A is not supported on hpux with ksh...
:-}
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
point_pleasantCommented:
try this


sed '1~6 {N;N;N;N;s/\n/ /g}' mfile1 | tr -s " " > /tmp/mfile3
for i in `cat mfile2`
do
        tag=`echo $i | cut -f1 -d'#'`
        tag1=`grep $tag /tmp/mfile3`
        tag=`echo $tag1 | cut -f4,6 -d' ' | tr -d '"'`
        tag2=`echo $tag | cut -f2 -d' '`
        tag1=`echo $tag | cut -f1 -d' '`
        echo "$i,$tag1,$tag2"

done
0
 
WatnogAuthor Commented:
This is the output:

sed: 1~6 {N;N;N;N;s/\n/ /g} is not a recognized function.
BHP-027#ZHRBI_PC_TRIG_EMPL_SELF_FILE,,
BHP-027#ZHRBI_PC_TRIG_HDCTN_TRN_FILE,,
BHP-027#ZHRBI_PC_TRIG_PACTN_TRN_FILE,,
EBZAPPP01#APACHESTART,,
EBZAPPP01#DMGRSTART,,
EBZAPPP01#DMGRSTOP,,
EBZAPPP01#EWTUDMUP,,
EBZAPPP01#EWTUFTUP,,
UTLAPPP03#SYSUDN00,,
UTLAPPP03#SYSUUP00,,
UTLAPPP04#AHPUSTART,,

mfile3 remains empty, the sed line seems to fail ...
0
 
point_pleasantCommented:
wow both examples work on RHEL 5.4 don't have access to HPUX but will try something else.  The sed command is supposed to be combining all lines up to record seperator END so the tmp file should only have three lines in it.  Seems big differences between RHEL & HPUX.
0
 
WatnogAuthor Commented:
I have experience with HPUX (and ksh) only... It sure is something to take in consideration...
Sorry for the hassle.
0
 
point_pleasantCommented:
one more try with regular shell commands


cat mfile1 | grep "CPUNAME\\|DESCRIPTION" | awk '{printf $0 ";"; getline; print $0}' | tr -d ";" | tr -s " " | tr -d "\"" >/tmp/file1
for i in `cat mfile2`
do
        tag=`echo $i | cut -f1 -d'#'`
        tag1=`grep $tag /tmp/file1`
        tag=`echo $tag1 | cut -f4 -d' '`
        tag2=`echo $tag1 | cut -f6 -d' '`
        echo "$i,$tag,$tag2"
done
0
 
point_pleasantCommented:
tried the above with ksh on RHEL 5.4 and works hopefully it will run HPUX
0
 
WatnogAuthor Commented:
This does a great job!
Many thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.