Watnog
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:
File2:
The result would need to become:
BHP-027#ZHRBI_PC_TRIG_EMPL _SELF_FILE ,SAP,HR
BHP-027#ZHRBI_PC_TRIG_HDCT N_TRN_FILE ,SAP,HR
BHP-027#ZHRBI_PC_TRIG_PACT N_TRN_FILE ,SAP,HR
EBZAPPP01#APACHESTART,SUN, ECOM
EBZAPPP01#DMGRSTART,SUN,EC OM
EBZAPPP01#DMGRSTOP,SUN,ECO M
EBZAPPP01#EWTUDMUP,SUN,ECO M
EBZAPPP01#EWTUFTUP,SUN,ECO M
UTLAPPP03#SYSUDN00,SUN,UTL AP
UTLAPPP03#SYSUUP00,SUN,UTL AP
UTLAPPP04#AHPUSTART,SUN,UT LAP
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.
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
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
The result would need to become:
BHP-027#ZHRBI_PC_TRIG_EMPL
BHP-027#ZHRBI_PC_TRIG_HDCT
BHP-027#ZHRBI_PC_TRIG_PACT
EBZAPPP01#APACHESTART,SUN,
EBZAPPP01#DMGRSTART,SUN,EC
EBZAPPP01#DMGRSTOP,SUN,ECO
EBZAPPP01#EWTUDMUP,SUN,ECO
EBZAPPP01#EWTUFTUP,SUN,ECO
UTLAPPP03#SYSUDN00,SUN,UTL
UTLAPPP03#SYSUUP00,SUN,UTL
UTLAPPP04#AHPUSTART,SUN,UT
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.
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
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
ASKER
This is the output:
mfile3 remains empty, the sed line seems to fail ...
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_HDCT N_TRN_FILE ,,
BHP-027#ZHRBI_PC_TRIG_PACT N_TRN_FILE ,,
EBZAPPP01#APACHESTART,,
EBZAPPP01#DMGRSTART,,
EBZAPPP01#DMGRSTOP,,
EBZAPPP01#EWTUDMUP,,
EBZAPPP01#EWTUFTUP,,
UTLAPPP03#SYSUDN00,,
UTLAPPP03#SYSUUP00,,
UTLAPPP04#AHPUSTART,,
BHP-027#ZHRBI_PC_TRIG_EMPL
BHP-027#ZHRBI_PC_TRIG_HDCT
BHP-027#ZHRBI_PC_TRIG_PACT
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.
ASKER
I have experience with HPUX (and ksh) only... It sure is something to take in consideration...
Sorry for the hassle.
Sorry for the hassle.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
This does a great job!
Many thanks.
Many thanks.
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