Link to home
Start Free TrialLog in
Avatar of cdeelena
cdeelena

asked on

KSH scripting and combining two files so that each row of both files is on the same line

Could use some help today.  I am writing a ksh script that will need to cat two files and append them to another file.  The catch is that for each row in both files, I need them appended to the same line in the target file.  The script does some other things as well and those all work.  I just can't seem to find the same line appending part.  I found one document that talks about appending cpu time to a field on the same line, but it is only a 1 line entry.  I need to do this for files that will have over 4 thousand rows.  It would help to have several commas separating the entries as well.  For example:

             File 1                    File 2                 Target File
Row 1    "ABCD","PROD"      "AD","PROD"       "ABCD","PROD",,,,"AD","PROD"
Row 2    "EFGH","PROD"      "GH","PROD"       "EFGH","PROD",,,,"GH","PROD"
etc.
etc.
etc.

File 1 and File 2 are created in the beginning of the script.  File 1 and 2 may not have the same number of rows.  They could be off either way.  File1 may have more or less rows than File 2, but I need to account for both situations.  Any ideas would be greatly appreciated.  Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Tintin
Tintin

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
Avatar of cdeelena
cdeelena

ASKER

Tintin,

Thanks for the response.  It seems to work good.  However, with the extra commas in there, I need to put a value in there so that my output file will look something like so:

Target file
"ABCD","PROD",,,=vlookup(A:A,G:H,1,false),,"AD","PROD"

The text in between the commas is the literal value.  My goal is to combine two text files and import them into Excel.  However, I need the vlookup function in there as well.  My belief is that if I force the vlookup function into the text file, that when I import it into Excel, that it will evaluate out as a function and not a literal.  I haven't tried it yet, just a suspicion.  So how would I get the extra commas and text into my file.  Unfortunately, I know nothing about awk and Perl, and very minimal on sed.

Thanks
paste -d: file1 file2 | sed "s/:/,,,=vlookup(A:A,G:H,1,false),,/"
Tintin,

I tried that, but the commas in my text string come out as delimiters.  So then I tried to enclose the text string in "", but then I get a syntax error at line 7 : `( '  unexpected.  This is the line with the paste command.  How do I resolve?
Tintin,

That's ok.  I figured it out.  I had to escape the parentheses and the quotes in order to get it to work.  Thanks for the help.  It seems to do what I wanted it to.

Thanks
You shouldn't have needed to escape anything.  I ran the above command verbatim and there are no errors.
Tintin,

It does run verbatim with no errors, however when I load it into Excel, Excel expands the text string into several fields because of the commas.  The commas are part of the syntax, so in order to keep them, I have to quote the whole string.  And in order to quote the whole string, I had to escape the quotes and parentheses.