How to insert/expand data/rows in a file, with rules??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to insert/expand data/rows in a file, with rules??
# 1  
Old 12-21-2010
Bug How to insert/expand data/rows in a file, with rules??

Hi, I am rather new to Unix/Linus. I have this problem that I would like to solve using unix.

Here is what I have

start stop expression
1 5 15
2 6 10

I want a output like this

position expression
1 15
2 25
3 25
4 25
5 25
6 10

Can anyone help me withe this??

Thanks a lot

Hsiao.Smilie
# 2  
Old 12-21-2010
does this help...?
Code:
awk 'NR==1||/[0-9]+/{print $1,$3}' inputfile > outfile

# 3  
Old 12-21-2010
Code:
awk '$2>m{m=$2}{A[$3]=$1;B[$3]=$2}END{for(i=1;i<=m;i++){t=0;for(j in A)if(i>=A[j]&&i<=B[j])t+=j;print i,t}}' infile

While reading the files, you need to store the minimum values and maximum values for which the amount applies.. I used arrays A and B for this. Also we need to determine the max value for which any rule applies. I used m for this. Then after the reading is over we have collected all the files and we can enumerate the values between 1 and max value and see what values should be added.

Last edited by Scrutinizer; 12-21-2010 at 02:30 AM..
# 4  
Old 12-21-2010
bash way:
bash code:
  1. #!/bin/bash
  2. while read A B C
  3. do
  4.    for ((i=A; i<=B; i++)); do ((N&#91;$i]+=C)); done
  5. done <file
  6. for i in ${!N[@]}; do echo "$i ${N[$i]}"; done
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to insert data in a file at last?

Hi Am Using Unix ksh... I have file name called FILE1 Have a content in a FILE as 11/01/2012 12/07/2012 -- -- I have used one variable DATE=12/11/2012 I wants to insert DATE variable value at last line in a file I need Output as cat FILE1 11/01/2012 12/07/2012 -- (2 Replies)
Discussion started by: Venkatesh1
2 Replies

2. UNIX for Dummies Questions & Answers

Merge rows in bid data file

Dear all, Please help me ,,,, if I have input file like this A_AA960715 leucine-rich repeat-containing protein GO:0006952 defense response P A_AA960715 leucine-rich repeat-containing protein GO:0008152 metabolic process P A_AA960715 leucine-rich... (5 Replies)
Discussion started by: AAWT
5 Replies

3. Shell Programming and Scripting

Insert rows of text into a file after pattern

SHELL=bash OS=rhel I have a file1 that contains text in sentences like so: file1 this is a sentence this is a sentence this is a sentence I also have a file2 like so: file2 command=" here is some text here is more text again we have some text " I wish to echo the text from... (4 Replies)
Discussion started by: jaysunn
4 Replies

4. UNIX for Dummies Questions & Answers

Insert rows with some rules??

Hi, I am rather new to Unix/Linus. I have this problem that I would like to solve using unix. Here is what I have start stop expression 1 5 15 2 6 10 I want a output like this position expression 1 15 2 25 3 ... (1 Reply)
Discussion started by: wanghlv
1 Replies

5. Emergency UNIX and Linux Support

Insert data into sql queries from a file

Hello friends, I need to insert data from a file to another. I need this to form an sql query file which will consist of 50.000 INSERT INTO sentences. my sql query file will consist of 50.000 times the below line consecutively: insert into subscriber... (6 Replies)
Discussion started by: EAGL€
6 Replies

6. Shell Programming and Scripting

insert a header in a huge data file without using an intermediate file

I have a file with data extracted, and need to insert a header with a constant string, say: H|PayerDataExtract if i use sed, i have to redirect the output to a seperate file like sed ' sed commands' ExtractDataFile.dat > ExtractDataFileWithHeader.dat the same is true for awk and... (10 Replies)
Discussion started by: deepaktanna
10 Replies

7. Shell Programming and Scripting

chop a data file into rows

A very naive question... I have a file which has many rows and many columns and I would like to chop off the rows and create a new file per row named after the first column of every row + 1. The data files look like: # Donades de la trajectoria de la particula 60001 # 1:T 2:Massa 3:Rx 4:Ry 5:Rz... (7 Replies)
Discussion started by: pau
7 Replies

8. UNIX for Dummies Questions & Answers

How to insert new line in the data file using the script

Hi all, I have a test.dat file.In that file i have many lines of data. IN between some lines i want to insert a new line while running the test.ksh. Say for ex: In the dat file i have data like N001 100.00 N001 200.00 N001 300.00 N001 400.00 <== After this line i want to... (2 Replies)
Discussion started by: Sona
2 Replies

9. HP-UX

find the position in a file and insert the data there

Hi, I have a EDI data file ARROWTEST of size 18246 characters. And I want to insert some data after 4200 position in the file. How I can find the position 4200 in that file....Please advise. Regards, (5 Replies)
Discussion started by: isingh786
5 Replies

10. Shell Programming and Scripting

sed, insert data from a file to another?

Hello, I have 2 files. File1 has data I wrote, and File2 is a file created by an application. I would like to insert the data from File1 into File2, but it has to be inserted at a certain location on File2. I know I need to search for "</jsp-param> </jsp-descriptor>" But I don't know... (4 Replies)
Discussion started by: ctcuser
4 Replies
Login or Register to Ask a Question