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
504 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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 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…

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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now