Results 1 to 9 of 9
hi, i want to edit my input file which looks like this
1-0.0020 3-0.98768 4-2.98765
i am trying to multiply those float numbers by 1000 and i want my output ...
- 10-05-2009 #1Just Joined!
- Join Date
- Dec 2008
- Posts
- 19
editing a file within
hi, i want to edit my input file which looks like this
1-0.0020 3-0.98768 4-2.98765
i am trying to multiply those float numbers by 1000 and i want my output to look like this
1-002.0 3-987.68 4-2987.65
so i would like to do this without changing the format (i dont want to spend more time as i believe there is an easy way )
so far i have tried to make a code and i get this output on my outputfile.
0.0020
0.98768
2.98765
is there a way of just navigating within the input file and multiply each decimal number dynamically without sending the output to the outstream?(I mean like replacing the multiplied number to the old number)
i have tried using replacing but ending with loooooong big error message.
here is wat i have;
int main(int argc, char** argv)
{
int count = 0;
char c;
ifstream infile;
ofstream outfile;
double value;
double value2;
infile.open("mpctest.txt");
outfile.open("output.txt");
while(!infile.eof())
{
infile >> c;
if(c=='-'){
infile>>setw(sizeof(double))>>value;
value2=value*1000;
outfile<<value2<<endl;
}
if(c == ' ') // loop till space is found
{ break;}
}
infile.close();
outfile.close();
return 0; // standard C/C++ exit code of success
}
any help will bring back my smile for the day
- 10-06-2009 #2Linux User
- Join Date
- Jan 2007
- Location
- cleveland
- Posts
- 452
this comes pretty close
sed 's/ /\n/g' <filename | \
awk ' BEGIN { FS = "-" }
{print $1"-"1000*$2}' | \
tr "\n" " " | sed 's/$/\n/'
where <filename> contains your data.
Or you could use "printf" like this
{printf "%d", $1; printf "%s","-"; \
printf "%07.2f\n",1000*$2}Last edited by tpl; 10-06-2009 at 03:35 AM.
the sun is new every day (heraclitus)
- 10-06-2009 #3Just Joined!
- Join Date
- Dec 2008
- Posts
- 19
thank you very much tpl. That works wonders and suprisingly simple....thank you a lot.
umm...i have one more question bout this.
the data file has a label on top like this
2345
1-0.0020 3-0.98768 4-2.98765 ...(very long)
3423
1-0.502083-0.99798 4-2.98769 ...(very long)
.
.
.(long)
i tried the script you gave and it prints
2345-0 -0 1-2.0 2-987.68 .....
how to skip executing it on the label as i want to leave them as they are...
thank you
- 10-06-2009 #4Linux User
- Join Date
- Jan 2007
- Location
- cleveland
- Posts
- 452
> labels
well, OK: see if this handles it--
sed 's/ /\n/g' <filename |\
awk ' BEGIN { FS="-" } \
/^[0-9]+$/ { printf "\n%s\n", $0 }; \
/-/{printf "%d",$1; printf "%s","-"; printf\
"%07.2f",1000*$2; printf "%s"," " }\
END {print ""}' |\
sed '1d' # removes initial newline
the regular expression "/^[0-9]+$/" matches
any all-digit line such as the labelsthe sun is new every day (heraclitus)
- 10-08-2009 #5Just Joined!
- Join Date
- Dec 2008
- Posts
- 19
thanks again
lastly i would like to use many labels separated by space like
321 890 458
and i want this line to print out as it is
how to match line with numbers separated by space?
help plzzzz
thank uuuu again
- 10-08-2009 #6Linux User
- Join Date
- Jan 2007
- Location
- cleveland
- Posts
- 452
add a space to the character class: /^[" "0-9]+$/
the sun is new every day (heraclitus)
- 10-08-2009 #7Just Joined!
- Join Date
- Dec 2008
- Posts
- 19
hi tpl, is there another way of doing this coz ^[" "0-9]+$/ didnt bring any change.
thanks
- 10-08-2009 #8Linux User
- Join Date
- Jan 2007
- Location
- cleveland
- Posts
- 452
OK, we need to leave the blanks alone in all-digit
lines: change in line 1
sed '/-/s/ /\n/g' <filename |\
awk ' BEGIN { FS="-" } \
/^[" "0-9]+$/ { printf "\n%s\n", $0 }; \
/-/{printf "%d",$1; printf "%s","-"; printf\
"%07.2f",1000*$2; printf "%s"," " }\
END {print ""}' |\
sed '1d' # removes initial newline
may I recommend an excellent book?
"the UNIX programming environment"
by Kernighan and Pike
Kernighan is the 'k' in awk, also in "k&r" cthe sun is new every day (heraclitus)
- 10-09-2009 #9Just Joined!
- Join Date
- Dec 2008
- Posts
- 19
thank u for your kindness tpl. i will definitely check the book in store.stay blessed


Reply With Quote