[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Unix ksh:  merge files with selected content

Posted on 2011-05-03
10
Medium Priority
?
855 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:Watnog
  • 6
  • 4
10 Comments
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35512853
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
 

Author Comment

by:Watnog
ID: 35512956
Thanks, but grep -A is not supported on hpux with ksh...
:-}
0
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35513676
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Watnog
ID: 35513770
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
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35513911
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
 

Author Comment

by:Watnog
ID: 35514023
I have experience with HPUX (and ksh) only... It sure is something to take in consideration...
Sorry for the hassle.
0
 
LVL 8

Accepted Solution

by:
point_pleasant earned 2000 total points
ID: 35514246
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
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35515096
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
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35515156
tried the above with ksh on RHEL 5.4 and works hopefully it will run HPUX
0
 

Author Closing Comment

by:Watnog
ID: 35688293
This does a great job!
Many thanks.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses
Course of the Month20 days, 14 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question