File creation , csv


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File creation , csv
# 1  
Old 12-22-2015
IBM File creation , csv

Dear All,

Below is my requirement.

Scenario:
There are four file input.txt, object.txt, output.txt, another_object.txt
Now the number of records in all these file may be different.

Sample Content of Files:
Object.txt -- > abcd.txt (eof)
Input.txt --> abc.txt \n xyz.txt(eof)
Output.txt --> output_file.txt(eof)
another_object.txt --> another_object_file_1.txt (eof) [ sometimes there may not be any such file]

Required output:

abcd.txt,abc.txt,output_file.txt,another_object_file_1.txt
abcd.txt,xyz.txt,NA,NA

i.e in order object.txt, input.txt, output.txt, another_object.txt (if there is no record or file present for particular... then NA should appear)

Expecting a constructive discussion / suggestion.

Thanks.
# 2  
Old 12-22-2015
1. Please also let us know what you've attempted.
2. Let us know what OS/shell/tools you are using/want to use.
# 3  
Old 12-22-2015
Please reformat your post using Code Tags when you post any code or data samples so we can easily read and understand your example. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)



Note that by definition a text file on a UNIX or Linux systems consists of zero or more lines of text each of which is terminated by a <newline> character. If the (eof) in you samples is meant to indicate that your files do not include the line-terminating <newline> character on the last input line, your input files will need to be turned into legitimate text files before they can reliably be processed by UNIX and Linux text processing utilities.

With your given sample input, why is the desired output:
Code:
abcd.txt,abc.txt,output_file.txt,another_object_file_1.txt
abcd.txt,xyz.txt,NA,NA

instead of:
Code:
abcd.txt,abc.txt,output_file.txt,another_object_file_1.txt
NA,xyz.txt,NA,NA

or, maybe:
Code:
abcd.txt,abc.txt,output_file.txt,another_object_file_1.txt
abcd.txt,xyz.txt,output_file.txt,another_object_file_1.txt

???
# 4  
Old 12-22-2015
Quote:
Originally Posted by Don Cragun
Please reformat your post using Code Tags when you post any code or data samples so we can easily read and understand your example. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)



Note that by definition a text file on a UNIX or Linux systems consists of zero or more lines of text each of which is terminated by a <newline> character. If the (eof) in you samples is meant to indicate that your files do not include the line-terminating <newline> character on the last input line, your input files will need to be turned into legitimate text files before they can reliably be processed by UNIX and Linux text processing utilities.

With your given sample input, why is the desired output:
Code:
abcd.txt,abc.txt,output_file.txt,another_object_file_1.txt
abcd.txt,xyz.txt,NA,NA

instead of:
Code:
abcd.txt,abc.txt,output_file.txt,another_object_file_1.txt
NA,xyz.txt,NA,NA

or, maybe:
Code:
abcd.txt,abc.txt,output_file.txt,another_object_file_1.txt
abcd.txt,xyz.txt,output_file.txt,another_object_file_1.txt

???
Its <newline> and not <EOF>.

I want the output like below
Code:
abcd.txt,abc.txt,output_file.txt,another_object_file_1.txt
abcd.txt,xyz.txt,NA,NA

Where abcd.txt will get repeated. ( input, output, and another_object are files that are used inside the file abcd.txt) there by I want the output to be in the above format

This is one part of the coding that I am doing. I am going to use this as a function to parse different files(like abcd.txt) and create csv files.

Hope this information would be helpful for you.

Using - AIX

---------- Post updated at 02:57 PM ---------- Previous update was at 02:52 PM ----------

I don't have any idea as such on how to proceed.
But felt I can use a for loop and print as desired into an output file.
# 5  
Old 12-22-2015
We are still waiting to see what you have tried to solved this problem (as requested by balajesuri). And, no, the output doesn't make sense to me.

The 2nd input file has two lines. The 1st input line in that file goes into the 1st output file line and the 2nd input line in that file goes into the 2nd output file line.

The other three input files have one line. Why does that input line get duplicated in the output file for one input file and become NA in the output for the other two input files???

And, what should be in the output file if another_object.txt doesn't exist?
# 6  
Old 12-22-2015
I don't know how I should go ahead with this., and yes I dint give a try to this module.

Code:
 
 Column 1 -- abcd.txt gets repeated untill the maximum number of records in any of the other 3 files.
 Column 2, 3 and 4 will have to be "BLANK" or "NA" if it doesn't contain record.

if another_object.txt or output.txt or input.txt doesn't have any record (which) or if it doesn't exist, we need that part of the column have either blank or NA
# 7  
Old 12-22-2015
If I understand your requirements correctly (and I'm not sure that I do), you could try something like:
Code:
#!/bin/ksh
f1="Object.txt"
f2="Input.txt"
f3="Output.txt"
[ -f another_object.txt ] && f4="another_object.txt" || f4="/dev/null"
awk '
FNR == 1 {
	fn++
}
{	d[fn, FNR] = $0
	if(FNR > maxline)
		maxline = FNR
}
END {	for(i = 1; i <= maxline; i++)
		print d[1, 1], (2, i) in d ? d[2,i] : "NA",
			(3, i) in d ? d[3, i] : "NA",
			(4, i) in d ? d[4, i] : "NA"
}' OFS=, "$f1" "$f2" "$f3" "$f4"

and Object.txt contains:
Code:
abcd.txt

and Input.txt contains:
Code:
abc.txt
xyz.txt

and Output.txt contains:
Code:
output_file.txt
output_file2.txt
output_file3.txt
output_file4.txt

and another_object.txt contains:
Code:
another_object_file_1.txt

produces the output:
Code:
abcd.txt,abc.txt,output_file.txt,another_object_file_1.txt
abcd.txt,xyz.txt,output_file2.txt,NA
abcd.txt,NA,output_file3.txt,NA
abcd.txt,NA,output_file4.txt,NA

but, if there is no file another_object.txt, it produces the output:
Code:
abcd.txt,abc.txt,output_file.txt,NA
abcd.txt,xyz.txt,output_file2.txt,NA
abcd.txt,NA,output_file3.txt,NA
abcd.txt,NA,output_file4.txt,NA

Note that this code will not work if any input file except the last one is an empty file and will not work if any of the input files other than another_object.txt is missing.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Save output of updated csv file as csv file itself, part 2

Hi, I have another problem. I want to sort another csv file by the first field. result.csv SourceFile,Airspeed,GPSLatitude,GPSLongitude,Temperature,Pressure,Altitude,Roll,Pitch,Yaw /home/intannf/foto5/2015_0313_090651_219.JPG,0.,-7.77223,110.37310,30.75,996.46,148.75,180.94,182.00,63.92 ... (2 Replies)
Discussion started by: refrain
2 Replies

2. Shell Programming and Scripting

Save output of updated csv file as csv file itself

Hi, all I want to sort a csv file based on timestamp from oldest to newest and save the output as csv file itself. Here is an example of my csv file. test.csv SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0739.JPG,2015:02:17 11:32:21 /home/intannf/foto/IMG_0749.JPG,2015:02:17 11:37:28... (10 Replies)
Discussion started by: refrain
10 Replies

3. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

4. Shell Programming and Scripting

Match columns from two csv files and update field in one of the csv file

Hi, I have a file of csv data, which looks like this: file1: 1AA,LGV_PONCEY_LES_ATHEE,1,\N,1,00020460E1,0,\N,\N,\N,\N,2,00.22335321,0.00466628 2BB,LES_POUGES_ASF,\N,200,200,00006298G1,0,\N,\N,\N,\N,1,00.30887539,0.00050312... (10 Replies)
Discussion started by: djoseph
10 Replies

5. Shell Programming and Scripting

Fixed Width file creation from csv

Hello All, I'm able to achieve my goal of creating a fixed width file from a comma delimited but I know I'm not doing it as efficiently as possible. Original File checksab 004429876883,O,342040,981.98,10232014 004429876883,O,322389,2615.00,10232014... (6 Replies)
Discussion started by: aahlrich
6 Replies

6. Shell Programming and Scripting

CSV File Creation Within Shell Script

Hi All, I am trying to create a CSV file within a shell script test.ksh and the code snippet is something like below: #!/usr/bin/ksh # Set required variables. . $HOME/.prof # Output file path Group1=/tmp/G1.csv Group2=/tmp/G2.csv Group3=/tmp/G3.csv $ORACLE_HOME/bin/sqlplus -s... (2 Replies)
Discussion started by: swasid
2 Replies

7. Shell Programming and Scripting

Comparing 2 CSV files and sending the difference to a new csv file

(say) I have 2 csv files - file1.csv & file2.csv as mentioned below: file1.csv ID,version,cost 1000,1,30 2000,2,40 3000,3,50 4000,4,60 file2.csv ID,version,cost 1000,1,30 2000,2,45 3000,4,55 6000,5,70 ... (1 Reply)
Discussion started by: Naresh101
1 Replies

8. Shell Programming and Scripting

2 problems: Mailing CSV file / parsing CSV for display

I have been trying to find a good solution for this seemingly simple task for 2 days, and I'm giving up and posting a thread. I hope someone can help me out! I'm on HPUX, using sqlplus, mailx, awk, have some other tools available, but can't install stuff that isn't already in place (without a... (6 Replies)
Discussion started by: soldstatic
6 Replies

9. UNIX for Dummies Questions & Answers

csv file creation

goodpeople... i have following script which collects info from tab and then generates csv file. all is good except 3 digt and 4 digit enties from tab Issue here is that csv file is not okay with 3 digit and 4 digit entries that reside in tab. script... #!/bin/ksh # # Script to send... (2 Replies)
Discussion started by: Student37
2 Replies
Login or Register to Ask a Question