Shell script to compare and generate a new file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to compare and generate a new file
# 1  
Old 04-19-2013
Shell script to compare and generate a new file

Requirement is
I have two files their format is
Code:
File1 - input_file
-----
tmp_value|3|number||
tmp_value1|3|alpha||
tmp_value2|6|alpha|AA AA|
tmp_value3|15|number|000000005|
tmp_value4|15|number|000000000000000|
tmp_value5|11|alpha|bbbbbbbbbbb|
tmp_value6|11|alpha|bb bb|
input_file  definition
-------------------
column1 = field_name
column2= field_length
column3= datatype
column4=value for the field
File2 - description_file
-----
tmp_value2|1|1|6|alpha|
tmp_value4|2|7|15|number|
tmp_value1|3|22|3|alpha|
tmp_value3|15|25|15|number|
reserved1|5|40|10|alpha|
filler1|6|50|6|number|
tmp_value6|7|56|11|alpha|
description_file definition
----------------------
column1 = field_name
column2= s1.no
column3= offset
column4=field_length
column5=datatype
fillers can be = fillers1 ...filler<n>
reserved can be = reserved1... reserved<n>

Total length of string can be 6+15+3+15+10+6+11 =66 
The resulted inputstring needed as per description_file is
|AA AA 000000000000000   000000000000005          000000bb bb      |
Description of above result
tmp_value2|1|1|6|alpha| ------------> value is |AA AA | (spaces for remaining 1 character)
tmp_value4|2|7|15|number| ----------> value is  |000000000000000|  samelength so subsitituted as same
tmp_value1|3|22|3|alpha|  ----------> value is  |   | - it is empty in input_file so subsituted spaces
tmp_value3|15|25|15|number| --------> value is |000000005| - filled remaining zero's leading
reserved1|5|40|10|alpha| ------------> value is |          | - it is reserved with alpha as datatype so subsituted spaces
filler1|6|50|6|number| --------------> value is |000000| - it is filler with number as datatype so subsituted zero's
tmp_value6|7|56|11|alpha|-----------> value is |bb bb      | - (spaces for remaining 6 character)

Code:
Conditions are:
1)I need to loop thru description file each line with each line in input_file.
2)if column1 in both file r equals then i need to take the value of column4 in input_file and have it in a string.
3)For the above there r certain conditions
 if column1=column1 
  then take column4 of input_filevalue 
  if column4 of input_file value length equals length =column4(field_length) of description_file
   result=result+column4(field_value)
  if column4(field_value) length is not equal to field_length
   if column5(datatype) = number
    fill leading zero's to the value to make to that field_length
    then
     result=result+value
   if column5(datatype) = alpha
    fill trailing spaces to value  to make to that field_length
    then
     result=result+value
 if column1(description_file) not present in input_file
  if column5(datatype) = number
  then
  fill zero's to the value to make to that field_length
  result=result+value
  if column5(datatype) = alpha
  then
  fill spaces to value  to make to that field_length
  result=result+value
 if column1(description_file) has string reserved or filler
  if column5(datatype) = number
  then
  fill zero's to the value to make to that field_length
  result=result+value
  if column5(datatype) = alpha
  then
  fill spaces to value  to make to that field_length
  result=result+value

i wrote a shell script for by refering unix.com but still i am not getting proper
result and trailing spaces and forming of string is not correct

---------- Post updated at 01:05 AM ---------- Previous update was at 12:44 AM ----------

i am not much familler in shell script .. so need help on this
# 2  
Old 04-19-2013
Not clear. Do you want to convert the input file using rules/conditions from the description file, or vice versa? The "reserved" string doesn't show up in the input file, so why should it in the output? That expected line would be the output file structure, wouldn't it? And line length CAN be or HAS TO be 66?

Why don't you post your script attempts, an input file, and the output connected to it?

P.S.: While other frequently have to be reminded to use code tags, you are very generous with them. Unfortunately, that doesn't improve readability either...
# 3  
Old 04-19-2013
i want to subsitute values(field_value) from input_file accoring to the description_file .. description file some has some more fields which may not in the input_file.
# 4  
Old 04-19-2013
use awk scripting language for this .. It will be easier do this kind of tasks
# 5  
Old 04-19-2013
I am not much familliar with awk
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script newbie- how to generate service log from shell script

Hi, I am totally a newbie to any programming languages and I just started an entry level job in an IT company. One of my recent tasks is to create a script that is able to show the log file of linux service (i.e. ntpd service) lets say, if I run my script ./test.sh, the output should be... (3 Replies)
Discussion started by: xiaogeji
3 Replies

2. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

3. Shell Programming and Scripting

Parameterizing to dynamically generate the extract file from Oracle table using Shell Script

I have below 2 requirements for parameterize the generate the extract file from Oracle table using Shell Script. Could you please help me by modifying the script and show me how to execute it. First Requirement: I have a requirement where I need to parameterize to generate one... (0 Replies)
Discussion started by: hareshvikram
0 Replies

4. Shell Programming and Scripting

Generate 10000 unique audio file of 2MB each using shell script.

Hi, I want 10000+ unique Audio file of approx 2MB each. How can i generate numerous audio files using shell script. Any tool, command or suggestions are welcome. If i give one audio seed file then can we create numerous unique files with same seed file? Any help is highly appreciable.... (11 Replies)
Discussion started by: sushil.kumar
11 Replies

5. Shell Programming and Scripting

How to compare two lines in a csv file using shell script?

I have a file lets say input.csv having two columns like- Name,Mobile No A,111 B,222 A,333 A,123 B,213 I would like to find result in a new file lets say output.csv as- Name,Mobile No A,111 B,222 means short the file on the basis of first column and first value corresponding to the... (5 Replies)
Discussion started by: Ashish Singhal
5 Replies

6. 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

7. Shell Programming and Scripting

a shell script to generate an excel sheet from a text file..

hi, i have a text file that looks like this! i want to generate an excel sheet out of it, removing all the junk data except the addresses that look like . Arrow Electrical Services Rotating Machinery, Electrical Contracting & Mining Specialists Onsite maintenance, breakdown... (8 Replies)
Discussion started by: vemkiran
8 Replies

8. UNIX for Advanced & Expert Users

Shell Script to compare xml files and print output to a file

All, PLease can you help me with a shell script which can compare two xml files and print the difference to a output file. I have attached one such file for you reference. <Group> <Member ID=":Year_Quad:41501" childCount="4" fullPath="PEPSICO Year-Quad-Wk : FOLDER.52 Weeks Ending Dec... (2 Replies)
Discussion started by: kanthrajgowda
2 Replies

9. Shell Programming and Scripting

help to compare data from file using shell script to c++ program

hello ....i am new in shell script..I have one shell script which read inputs from file...and second is c++ pgm..i call my script from c++ programm..to compare file values to c++ pgm value.. our shell script is shubhi.sh #! /bin/sh while read line do echo -e"$line" done<file1 this script... (1 Reply)
Discussion started by: shubhig15
1 Replies

10. Shell Programming and Scripting

shell script to compare file contents

Hello Has anyone got an example shell script that I can use to compare the contents of two files. The files should contain the same contents, eg. file1.txt apple pear grape file2.txt apple pear grape (2 Replies)
Discussion started by: deedaz
2 Replies
Login or Register to Ask a Question