Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
529 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 1000 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
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: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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
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…
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 Month5 days, 22 hours left to enroll

773 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