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
500 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now