Link to home
Start Free TrialLog in
Avatar of Watnog
WatnogFlag for Belgium

asked on

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.
Avatar of point_pleasant
point_pleasant
Flag of United States of America image

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

Avatar of Watnog

ASKER

Thanks, but grep -A is not supported on hpux with ksh...
:-}
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
Avatar of Watnog

ASKER

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 ...
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.
Avatar of Watnog

ASKER

I have experience with HPUX (and ksh) only... It sure is something to take in consideration...
Sorry for the hassle.
ASKER CERTIFIED SOLUTION
Avatar of point_pleasant
point_pleasant
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
tried the above with ksh on RHEL 5.4 and works hopefully it will run HPUX
Avatar of Watnog

ASKER

This does a great job!
Many thanks.