Adding new fields to an existing layout


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding new fields to an existing layout
# 1  
MySQL Adding new fields to an existing layout

Hi Everybody,
I have an layout file like below

Code:
f1 1 char 10,
f2 11 char 2,
f3 13 char 1,
lineend 14 char 1

Their I need to add a new field which would be like
Code:
f5 char 3,
f6 char 2

The o/p should be

Code:
f1 1 char 10,
f2 11 char 2,
f3 13 char 1,
f5 14 char 3,
f6 17 char 2
lineend 14 char 1 /* Note this should be the last field. */


Now my requirement is , I need to automate this process.
i.e the new field addtion f5 and f6 should be automated and the field name data type and the length will be given in a text file. We need to read that file and modify the layout. Can anyone help me how to approach this?

Note : For the new fields to be added, the spaces in between fields should be similar to the old fields.

Last edited by Franklin52; 10-14-2009 at 08:23 AM.. Reason: Please use code tags!
# 2  
Hi Manii,

a.txt is your existing file...

b.txt is file whith new data

Code:
cnt=`wc -l a.txt | awk '{print $1}'`
num=`expr $cnt - 1 | bc`
head -$num a.txt >> new.txt
cat b.txt >> new.txt
tail -1 a.txt >> new.txt

# 3  
Hi Amit,
Thanks for the reply

From your script:

file b.txt will only contain the fild name data type and the length

For ex:f6 char 2
f7 char 3

If the file a.txt has 5 fields, like
 
f1 1 char 10,
f2 11 char 2,
f3 13 char 1,
f5 14 char 3,
I need the output like
 
f1 1 char 10,
f2 11 char 2,
f3 13 char 1,
f5 14 char 3,
f6 17 char 2,
f7 19 char 3,
lineend 22 char 1
Note the f6 2nd column starting is 17 which is the sum of col2 + col4 of the pervious record.
Also I need the spaces/tab as equavalent to the previous record to make the look and feed better for which I dont get an idea how to do?
# 4  
Here is code :
Code:
#! /bin/ksh

aa=" "
cnt=`wc -l a.txt | awk '{print $1}'`
num=`expr $cnt - 1 | bc`
head -$num a.txt >> new.txt
let i=1

let sec=`tail -1 new.txt | awk '{print $2}'`
las=`tail -1 new.txt | awk '{print $4}'`
let llas=`echo $las | awk -F, '{print $1}'`


bcnt=`wc -l b.txt | awk '{print $1}'`

while [ $i -le $bcnt ]
do
	col1=`head -$i b.txt| tail -1 | awk '{print $1}'`
	col2=`expr $sec + $llas | bc`
	col3=`head -$i b.txt | tail -1 | awk '{print $2}'`
	col4=`head -$i b.txt | tail -1 | awk '{print $3}'`
	echo "${col1}${aa}${col2}${aa}${col3}${aa}${col4}" >> new.txt
	let i=`expr $i + 1 | bc`
	let sec=$col2
	let llas=`echo $col4 | awk -F, '{print $1}'`
	
done
tail -1 a.txt >> new.txt

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #813
Difficulty: Medium
The CSS RGBA alpha parameter defines the opacity as a number between 1.0 (fully transparent) and 0.0 (fully opaque).
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding a new column to an existing one

I have a data file that looks like this: 0.01 1 3822 4.97379915032e-14 4.96982253992e-09 0 0.01 3822 1 4.97379915032e-14 4.96982253992e-09 0 0.01 2 502 0.00993165137406 993.165137406 0 0.01 502 2 0.00993165137406 993.165137406 0 0.01 4 33 0.00189645523539 189.645523539 0 0.01 33 4... (3 Replies)
Discussion started by: kayak
3 Replies

2. AIX

Adding existing VG to powerHA Resource group

Hello. I am Running AIX 6.1 and PowerHA 6.1 I have an active/active cluster (Prod/Dev) cluster. Each side will failover to the other. I have on my prod side an active volume group with a file system. The VG is imported on both nodes and active (varried on, file system mounted) on the prod... (3 Replies)
Discussion started by: mhenryj
3 Replies

3. Shell Programming and Scripting

Adding a new column in a file with other existing columns

Hi All , Kindly help me with this soln awk '{printf "%s %7s \n", $1,$c}' infile where value of variable c I am externally giving input But executing the above command shows all the columns of infile where as I want only 1st column of infile and 2nd column should print value c (8 Replies)
Discussion started by: Pratik4891
8 Replies

4. Shell Programming and Scripting

Adding New Selections to the existing Script

Hi.... i have two scripts called: "type" & "selecttype". In "type" i have only the name of the products ex.: product1 & product2. Now, i have to ADD product3 which includes sub categories: productA, productB, productC, productD, productE, productF. so my New type script (menu) looks as... (1 Reply)
Discussion started by: Netrock
1 Replies

5. Web Development

Adding a Web Calendar to an existing Groupware

Hi All, I have an existing groupware consisting of postfix,dovecot,openldap and webmail(RoundCube). But It lacks a shared calendar. Can someone advise how can I implement a calendar in my groupware ? Thanks. (1 Reply)
Discussion started by: coolatt
1 Replies

6. Shell Programming and Scripting

Adding file to an existing tar

Hi Friends, I want to know the command to add a new file in a existing tar file. For Ex: I have a tar file file1.tar with the contents one.txt two.txt three.txt Now I need to add file four.txt to this existing tar file, how can I do it? Thanks in advance (4 Replies)
Discussion started by: mr_manii
4 Replies

7. Linux

Adding space to existing mount

I am on Red Hat Linux 4.7. I am getting this error message: Preparing... ########################################### You have insufficient diskspace in the destination directory (/usr/lib) to install xxxxxxxxxxxxx. The installation requires at least 1.5 GB free on this... (2 Replies)
Discussion started by: jxh461
2 Replies

8. UNIX for Dummies Questions & Answers

Adding header to an existing file

Dear All, I need to add a header of one line to an already existing file. I know that it can be achieved by the following: echo "Header" > newfile cat file1 >> newfile But my problem is that file is huge and there is no space for creating a new file every time. Is there a way that I can... (5 Replies)
Discussion started by: shash
5 Replies

9. Solaris

SMF - Adding instances to existing manifest

I have a default, sun supplied, apache2 install on solaris10. It's running fine, everything's great. I'm looking to add another instance, fairly happy with how to do that apart from one thing - the SMF manifest. Can i just edit the XML file sun supplied for the default apache2 and add in details... (2 Replies)
Discussion started by: craigp84
2 Replies

10. UNIX for Dummies Questions & Answers

Create a calculated field from existing fields

I need to add an average field to a file. I can create it in an on-screen report and it does what I need using the awk command but I can't get it to create a new file with the additional field. Here's what I'm working with: file layout: id:lastname:firstname:grade1:grade2:grade3:grade4 I... (1 Reply)
Discussion started by: atchleykl
1 Replies

Featured Tech Videos