Dynamic file generation using shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Dynamic file generation using shell
# 1  
Old 09-18-2014
Dynamic file generation using shell

I have to generate the file dynamically from the source file based on the below control file.

Code:
control_file.txt 
1,3,5,-1,8,-1,4

The control file contain the position of column which i required from the source file, Example 1 column ,3 column ,5 column ,blank column(-1 indicates blank column) and so on..

I have written a shell to read one by one position from control file to generate multiple files and finally used paste command to generate new files incase of -1 values i created touch files. Based on sequence of file order its's pasted by ls-v option.

So my existing shell looks like below,
Code:
if [ position != -1 ]
then
cut -d, -f$position > file_$var.csv 
else
touch file_$var.csv
fi
paste -d, $(ls -v file_*.csv) > newe_file.csv

I hope there is a way to minimize the I/O of files, I am looking for something below,

Code:
cut -d, -f1,3,5 > file1.csv
touch file2.csv
cut -d, -f8 > file3.csv
touch file4.csv
cut -d, -f4 > file5.csv

or better solution will be great.

The number of column in source files will be in hundred's

Expected results:

input-file is sample.csv

Code:
col1,col2,col3,col4,col5,col6,col7,col8
1,2,3,4,5,6,7,8
9,10,11,12,13,14,15,16

output.csv

Code:
col1,col3,col5,-1,col8,-1,col4
1,3,5,,8,,4
9,11,13,,16,12

output.csv is based on controlfile.txt

Please suggest me to get some idea , I hope sed can help me to achieve !!

Last edited by Scrutinizer; 09-18-2014 at 04:40 AM.. Reason: alligned; (mod) change bold tags to code tags
# 2  
Old 09-18-2014
That will be difficult like that because cut will not keep the order of columns. Also you need to somehow put the the columns in file 2 into shell variables first. That wil become a bit cumbersome with a lot of external programs..

An alternative would be to use awk, that for example:
Code:
awk 'NR==FNR{split($0,P); next}{split($0,F); $0=x; for(i in P) $i=F[P[i]]}1' FS=, OFS=, controlfile.txt sample.csv

# 3  
Old 09-18-2014
Note, however, that if you using a UNIX System (rather than a Linux System),awk probably won't work if any lines in your input file are longer than 2048 bytes (actually whatever number is returned by getcont LINE_MAX). With hundreds of input fields, this may be a problem depending on what OS you're using.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sequence generation in shell

Hi, I am using the nested for loops to generate the sequence , taking start and end sequence number input from test.txt (sample content mentioned below). Also , can I print the rest of columns as well with sequence number into the same file. for i in `cat test.txt|cut -d"," -f7` do ... (8 Replies)
Discussion started by: tushar.modgil
8 Replies

2. Shell Programming and Scripting

Shell Printf command , a little more dynamic..

A big hello to everyone tagged to this site of knowledge . This is the first post of mine and I am looking forward to an enjoyable stint in this forum where I get to know a lot of new ideas and share whatever knowledge (its not much though :) ) I have acquired throughout my career so far with... (4 Replies)
Discussion started by: kumarjt
4 Replies

3. Linux

Shell Script to generate Dynamic Param file Using SQL Plus Quey

Hi All, Can anyone give me Shell script sample script to generate Param file by Reading Values from SQL Plus query and it should assign those values to variables like.. $$SChema_Name='ORCL' Thanks in Advance... Srav... (4 Replies)
Discussion started by: Sravana Kumar
4 Replies

4. Shell Programming and Scripting

Passing dynamic value to shell script

Hi Team, I'm using HP UX. I want to pass a variable dynamically as input while executing the script and that value need to be replaced with a string in the script. I tired SED, in the command line its working but while I keep the same command in the script its not working. Can someone help me... (4 Replies)
Discussion started by: hazel
4 Replies

5. Shell Programming and Scripting

Monitor file generation for an hour using Korn shell script

Hi, I am new to this unix scripting, I got a requirement like.. Files with *.XML extension will be generating in a /home/sample/ folder for every 15 mins. I need to monitor those files in that particular folder for every hour. If no file has been generated in that particular folder for an... (7 Replies)
Discussion started by: siri_886
7 Replies

6. Shell Programming and Scripting

Dynamic output file generation using a input text file with predefined output format

Hi, I have two files , one file with data file with attributes that need to be sent to another file to generate a predefined format. Example: File.txt AP|{SSHA}VEEg42CNCghUnGhCVg== APVG3|{SSHA}XK|"password" AP3|{SSHA}XK|"This is test" .... etc --------- test.sh has... (1 Reply)
Discussion started by: hudson03051nh
1 Replies

7. Shell Programming and Scripting

Dynamic command line generation with awk

Hi, I'm not an expert in awk but i need a simple script to do this: I'd like to AutoCrop PDF files. I found 2 simple script that combined together could help me to automatize :) The first utiliti is "pdfinfo" that it gives the MediaBox and TrimBox values from the pdf. The pdfinfo output... (8 Replies)
Discussion started by: gbagagli
8 Replies

8. Shell Programming and Scripting

Shell script dynamic command

I need to run a shell script with dynamic command in it like # Begin script... mysql xx "select * from tab" | sed 's/\t/|/g' > GENERATED_20100304.txt the dynamic part is 20100304 which should be today's date, and it needs to run every day and create a new file with... (2 Replies)
Discussion started by: nuthalapati
2 Replies

9. Shell Programming and Scripting

creating dynamic shell script

Hello I am trying to create a dynamic ksh script and I have an issue. I have a script a.ksh and it has got the following lines (for example) #!/bin/ksh # trace mode +x : without trace -x : with trace set +xv echo hi, i am going to create a dynamic script now cat >> dynamic.ks <<EOF... (2 Replies)
Discussion started by: sundarkumars
2 Replies

10. Shell Programming and Scripting

Dynamic variables within shell script

Hi Gurus, I have a requirement of writting the shell script where it should ask me two values FND_TOP=/d02/app/oracle/xxx/fnd/11.5.0 CDCRM_TOP=/d02/app/oracle/xxx/cdcrm/11.5.0 and then keep these values stored as variables for the execution of rest of the script. Because, I have to... (2 Replies)
Discussion started by: isingh786
2 Replies
Login or Register to Ask a Question