Solved

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

Posted on 2004-08-30
7
506 Views
Last Modified: 2013-12-27
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.
0
Comment
Question by:cdeelena
  • 4
  • 3
7 Comments
 
LVL 48

Accepted Solution

by:
Tintin earned 250 total points
ID: 11936514
You can get very close by doing

paste -d, file1 file2

However, paste only allows you to specify a single character as the delimiter, so if you want additional commas, you'd have to do something like:

paste -d: file1 file2 | sed "s/:/,,,,/"

You could also use awk or Perl to do what you need.
0
 

Author Comment

by:cdeelena
ID: 11936744
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
0
 
LVL 48

Expert Comment

by:Tintin
ID: 11936820
paste -d: file1 file2 | sed "s/:/,,,=vlookup(A:A,G:H,1,false),,/"
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:cdeelena
ID: 11936898
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?
0
 

Author Comment

by:cdeelena
ID: 11936946
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
0
 
LVL 48

Expert Comment

by:Tintin
ID: 11937008
You shouldn't have needed to escape anything.  I ran the above command verbatim and there are no errors.
0
 

Author Comment

by:cdeelena
ID: 11937540
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.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

772 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