Change numbers in flat file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change numbers in flat file
# 1  
Old 11-28-2013
Change numbers in flat file

Hi, I have some datatexts with values and I want to replace only the values of the first file with the result of array loaded in one variable.
Also I want the operation difference with the original value replaced and only replace the new value not the character format of the file.
Some Idea?
Thanks in advance.

I have the file1.text
Code:
Data 1
Value One 24.24426
Value Two 45.24535
Other values in the list 85.24535

Data 2
Value One 44.24225
Value Two 25.24535
Other values of new 55.13546

Code:
Variable=54.24425 35.24535  25.53231 24.24225 35.24535 28.350132

And I want to generate the back template file1.text with the new data and display the data of the variable and the difference of the operation with the original value.
Code:
New File generated
Data 1
Value One 54.24425 +29.99999 
Value Two  35.24535  -10.00000
Other values in the list  25.53231  -59.71304

Data 2
Value One 24.25225 -20.00000
Value Two 35.24535 +10.00000
Other values of new  28.350132  -26.785328


Last edited by faka; 11-28-2013 at 02:40 PM..
# 2  
Old 11-28-2013
save the following as manip.pl and call as perl manip.pl file2.txt file1.txt (sorry dealt with files in wrong order Smilie )

Code:
#!/usr/bin/perl
use strict;
use warnings;

open (my $var, '<', $ARGV[0]);
open (my $data, '<',$ARGV[1]);

my$var_string=readline($var);
my @vars=($var_string=~/(\d\S+)/g);
my $index=0;
while(<$data>){
    if ($_=~/(\d+\.\d+)/){
        my $replacement="$vars[$index] ";
        my $replacement_abs=abs($vars[$index] - $1);
        $replacement .= ($vars[$index]>=$1)?"+$replacement_abs":"-$replacement_abs";
        $_=~s/(\d+\.\d+)/$replacement/;
        $index++
    }
    print
}

# 3  
Old 11-28-2013
Try :

Code:
awk -v Variable="54.24425 35.24535 24.24225 35.24535" \
    '
    BEGIN{
            split(Variable,A)
         }
 /^Value/{
            ++i
            $3 = A[i] FS A[i] -$3
         }1
    '  file

# 4  
Old 11-28-2013
Note that the output you said you want on lines starting with "Value One" does not match the sample data you provided.

Akshay's proposal comes close to what I think you want, but doesn't print the results with leading + signs and five decimal places.

Try:
Code:
awk -v Variable='54.24425 35.24535 24.24225 35.24535' '
BEGIN { split(Variable, vlist) }
$1 == "Value" {
        $4 = sprintf("%+.5f", vlist[++v] - $3)
        $3 = vlist[v]
}
1' file1.text > file2.txt

which puts the following into file2.txt:
Code:
Data 1
Value One 54.24425 +29.99999
Value Two 35.24535 -10.00000

Data 2
Value One 24.24225 -20.00000
Value Two 35.24535 +10.00000

I believe the values shown in red are correct, but do not match the output you said you wanted.

If you want to run this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
# 5  
Old 11-28-2013
Quote:
Originally Posted by Don Cragun
Note that the output you said you want on lines starting with "Value One" does not match the sample data you provided.

Akshay's proposal comes close to what I think you want, but doesn't print the results with leading + signs and five decimal places.

Try:
Code:
awk -v Variable='54.24425 35.24535 24.24225 35.24535' '
BEGIN { split(Variable, vlist) }
$1 == "Value" {
        $4 = sprintf("%+.5f", vlist[++v] - $3)
        $3 = vlist[v]
}
1' file1.text > file2.txt

which puts the following into file2.txt:
Code:
Data 1
Value One 54.24425 +29.99999
Value Two 35.24535 -10.00000

Data 2
Value One 24.24225 -20.00000
Value Two 35.24535 +10.00000

I believe the values shown in red are correct, but do not match the output you said you wanted.

If you want to run this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
Don thanks for your reply, Data 1 and Data 2 are texts stamped in file1.txt but file2.txt does`nt exist, I want to generate it by matching first value with the first of the variable make the operation with the original value and get the result (first value of the variable) on the same row and at the right of the old value the result of the operation, with a + or - symbol at left.
# 6  
Old 11-28-2013
@ Don Thanks you, I was in hurry forgot to usesprintf and I too noticed expected is 30.0001 and actual result is 29.99999

@faka please post your requirement clearly
# 7  
Old 11-28-2013
Quote:
Originally Posted by faka
Don thanks for your reply, Data 1 and Data 2 are texts stamped in file1.txt but file2.txt does`nt exist, I want to generate it by matching first value with the first of the variable make the operation with the original value and get the result (first value of the variable) on the same row and at the right of the old value the result of the operation, with a + or - symbol at left.
I know file2.txt doesn't exist; the script I gave you creates it.

The contents of file2.txt that I showed you is what is produced by the script I gave you.

You said you wanted file2.txt to contain:
Code:
Data 1
Value One 54.24425 +30.00001 
Value Two 35.24535 -10.00000

Data 2
Value One 24.25225 -20.00000
Value Two 35.24535 +10.00000

But, 54.24425 - 24.24426 is +29.99999; not the +30.00001 you said you wanted. And the 3rd value you had in Variable is 24.24225; not the 24.25225 you said you wanted in the output.

Note also that your 1st message in this thread says that your input file is file1.text; not file1.txt. The script I gave you used the filename you specified.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change numbers

Hallo This is the content of the file 3 4 5 6 7 8 9 10 11 12 And I want the following output 1 2 3 4 5 6 7 (4 Replies)
Discussion started by: thailand
4 Replies

2. Shell Programming and Scripting

Change the numbers

Hi friends, i need a command which can be used to change the values in file. I have file which contain some values. Data_Center_costing=XXXX Financial_loss=XXXX operational_cost=XXX I am not aware about the values of XXXX, They may be 4 digit or less/more than that. but i need these... (12 Replies)
Discussion started by: Nakul_sh
12 Replies

3. Shell Programming and Scripting

Adding (as in arithmetic) to numbers in columns in file, and writing new file with new numbers

Hi again. Sorry for all the questions — I've tried to do all this myself but I'm just not good enough yet, and the help I've received so far from bartus11 has been absolutely invaluable. Hopefully this will be the last bit of file manipulation I need to do. I have a file which is formatted as... (4 Replies)
Discussion started by: crunchgargoyle
4 Replies

4. Shell Programming and Scripting

Trying to take file numbers from a file, pass them to sed to change strings in corresponding lines

I have a bunch of file numbers in the file 'test': I'm trying the above command to change all the instances of "H" to "Na+" in the file testsds.pdb at the line numbers indicated in the file 'test'. I've tried the following and various similar alternatives but nothing is working: cat test |... (3 Replies)
Discussion started by: crunchgargoyle
3 Replies

5. Shell Programming and Scripting

How to change the format of the date column in a flat file?

Hi, i have a flat file namely temp.txt with this data below ID|name|contact_date 101|Kay|2013-12-26 102|let|2013-12-26 I need to modify the date data in the flat file into MM/DD/YYYY HH24:MI:SS format let me know the code for this. Thank you! (5 Replies)
Discussion started by: srikanth_sagi
5 Replies

6. Shell Programming and Scripting

Regarding change in column numbers after some commands

Hi All, I was using some commands to: replace a column by a constant string character awk -v a=CA 'NF>1{ $3=a; print; } ' $line>$line"_1" to copy a column and paste it in another place awk '$5=$2" "$5' $line>$line"_2" to delete the extra columns awk '{for(i=1;i<=NF;i++)... (9 Replies)
Discussion started by: CAch
9 Replies

7. Shell Programming and Scripting

Change numbers in a file, incrementing them

Hello, Here's a file of mine: key1:431 key2:159 key3:998 I need to change these keys to something bigger - and I actually need to shift them all by a range of 3. The output would be: key1:434 key2:162 key3:1001 I can't find the propper sed/awk line that would alter all my... (4 Replies)
Discussion started by: fzd
4 Replies

8. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies

9. AIX

how do I change major-minor numbers of disk devices

Good evening ... does anyone of you know how to change major/minor numbers of disk devices ? I had to migrate from raid1 to raid5 and this messed up my ASM cluster - I know which devices should have which IDs to match the content - but I have no idea how to change it. Any help would be... (2 Replies)
Discussion started by: zxmaus
2 Replies

10. Shell Programming and Scripting

script to change filename with numbers

ok, this one is definitely too hard for my shell-script-skills. Hopefully, there is somebody who can help me with this: I have a folder with files in it named 0.ppm 10.ppm 2.ppm ... 5.ppm 50.ppm 55.ppm ... 355.ppm 360.ppm etc. As you will notice, the order in which the files are... (5 Replies)
Discussion started by: silversurfer202
5 Replies
Login or Register to Ask a Question