LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 10-25-2006, 05:21 AM   #1
assasukasse
Member
 
Registered: Mar 2006
Location: UK
Distribution: Debian, Ubuntu
Posts: 129

Rep: Reputation: 15
Calculation aid script!


Hello
i have a very tedious task to be automated:
i am running a computational analysis, and i need to edit MANY time the coordinates on a file..
the file is a txt, and the coordinate to be edited are those:

lines before (which i don't need to edit)
SDEF pos= x y z blabla blablabla
lines after (which i don't need to edit)

i need to edit them taking from a list of coordinates in another file txt i can provide; and the script should output N new files (where N is the number of coordinates i have to examine) each one with a tag number (example sample1, sample2..)
So in short it should edit a file N times, saving each time it edits with a different name.
please help me i have to edit 3580 times the file, the coordinates needed are just 70 but i've to consider many different cases..
Thanks!!
 
Old 10-25-2006, 05:40 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
so the text file coords is just 70 lines?
and you need 3580 files output.

need more example data and example output
 
Old 10-25-2006, 05:54 AM   #3
assasukasse
Member
 
Registered: Mar 2006
Location: UK
Distribution: Debian, Ubuntu
Posts: 129

Original Poster
Rep: Reputation: 15
yes:
the file with the coordinates is a 70 lines simple TXT file made like that:
1 x y z
2 x y z
3 x y z
where the line number should be used in the output file name.
i need only 70 output files because this is the most tedious work:
i am calculating for energy ranges so what i do is basically this, i choose an energy range and write to the file
then i make 70 files for all the coordinates and launch the analysis
once is done i collect the data in a excel sheet and i pass to the next energy by editing the main file
then again i make 70 files out of the template changing only the coordinates.
and so on..
what i need the script for is avoiding the work of editing 70 times the main file, by getting 70 duplicate files where only the coordinate value has been changed..
i need 3580 analisys in total but this i need to do myself, the important is automating the coordinate change or i will get crazy or very likely make an error..

Last edited by assasukasse; 10-25-2006 at 05:55 AM.
 
Old 10-25-2006, 06:00 AM   #4
assasukasse
Member
 
Registered: Mar 2006
Location: UK
Distribution: Debian, Ubuntu
Posts: 129

Original Poster
Rep: Reputation: 15
i post you an example data:
is part of the file since is pretty long

/cut
13 RCC 0 0 1.634 0 0 9.5 7
14 RCC 6.45 0 10.134 0 0 1 0.55 $cella sorgente
c fine surface cards

c definizione della sorgente
SDEF pos=6.45 0 10.634 axs=0 0 1 par=2 ERG=0.662 CEL=11
RAD=D1 EXT=D2
SI1 0 0.55
SI2 0.5
c fine definizione della sorgente
c mat cards
/cut

what i need to edit is the value pos=x y z

the output file should be a copy of the imput file with the coordinate changes according to the coordinate file that should have this form:
1 x y z
2 x y z
3 x y z
...

for example if i have only 3 coordinate files i should get 4 files:
one is the original, and 3 are the modified coordinate ones

so if original is sample
i will have the following 4 files: sample, sample1 (which has been modified to have pos=x y z as in line 1 of coordinate file), sample2, sample3

Thanks
 
Old 10-25-2006, 06:27 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
So this is the ONLY line to change,

Code:
SDEF pos=6.45 0 10.634 axs=0 0 1 par=2 ERG=0.662 CEL=11
and only pos=n n n

and will it always start with SDEF
 
Old 10-25-2006, 06:56 AM   #6
assasukasse
Member
 
Registered: Mar 2006
Location: UK
Distribution: Debian, Ubuntu
Posts: 129

Original Poster
Rep: Reputation: 15
yes this is the ONLY line to change
will always start with SDEF 5spaces then pos=x y z
actually what is needed to change is the coordinates after pos=
 
Old 10-25-2006, 07:55 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
Try this.

It's probably not totally robust, think of it as a prototype.

It's a quick dirty solution,
but I've added some rudimentary integrity checks
on the data.

will create files named after the template with a suffix

use it like:

do_it.pl template_file coord_file


Code:
#!/usr/bin/perl -w

sub usage {

    die "Usage: $0 template-file coord-file\n";
}
usage() unless (scalar(@ARGV) == 2); # count params



($template, $data_file) = @ARGV; # get filenames


@ARGV = ($data_file);
@coords = <>;  # slurp coordinates
warn "Is '$data_file' a proper coordinate file? It contains non-numerics\n"
     if grep /[a-z]/i, @coords;

@ARGV = ($template);
@data = <>; # slurp template
die "No SDEF found! in $template" unless grep /SDEF/, @data;


foreach (@coords) {

    next unless /./; # jump blank lines

    ($nm, $x, $y, $z) = split; 

    @copy = @data; 

# do the sub for SDEF
    map {s/(SDEF pos=)[\d .]+(.*)/$1$x $y $z $2/} @copy;
    $filename = "$template.$nm";

    open OUT, ">$filename";
    print OUT @copy;
    close OUT;

}
 
Old 10-25-2006, 08:55 AM   #8
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 37
I'd just like to mention that sed or awk might work for you as well.
Although bigearsbilly's perl solution looks good
 
Old 10-25-2006, 08:56 AM   #9
assasukasse
Member
 
Registered: Mar 2006
Location: UK
Distribution: Debian, Ubuntu
Posts: 129

Original Poster
Rep: Reputation: 15
thanks!
i have only one question
the line

map {s/(SDEF pos=)[\d .]+(.*)/$1$x $y $z $2/} @copy;

make me think that the correct sintax for the coordinate file is this:
1x y z

and not this
1 x y z

is that true?

i don't know perl but looks a nice a powerful language!
Thanks
 
Old 10-25-2006, 08:59 AM   #10
assasukasse
Member
 
Registered: Mar 2006
Location: UK
Distribution: Debian, Ubuntu
Posts: 129

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by muha
I'd just like to mention that sed or awk might work for you as well.
Although bigearsbilly's perl solution looks good
thanks muha
do u have a link to a guide how to use sed or awk?
i want to learn how to use them too!
 
Old 10-25-2006, 09:11 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
Quote:
make me think that the correct sintax for the coordinate file is this:
1x y z
no, that's incorrect

the $1$x you refer to
is for the SDEF pos=n part grabbed by the parenthesised
expression (SDEF pos=)
hence there is no space for the output.

in this expression $1 represents "SDEF pos=" and $x = n
so $1$x interpolates to SDEF pos=n

the data is a list of lines,
map applies the substitution expression to each line in turn,
(easy but slightly redundant for most lines )


perl is a very powerful language for text processing.

sed and awk are a subset of perl pretty much
I never bother with sed and awk now.

except for sed one liners.
 
Old 10-29-2006, 09:12 AM   #12
assasukasse
Member
 
Registered: Mar 2006
Location: UK
Distribution: Debian, Ubuntu
Posts: 129

Original Poster
Rep: Reputation: 15
i found a small problem with the script:
if i choose a clearly wrong file as a template for the coordinates
it tells me that maybe i am wrong since there are letters too
but proceed anyway making a bunch of wrong files...
is there any way to stop the generation of files in case a letter is encountered?
thanks
 
Old 01-04-2007, 11:32 AM   #13
assasukasse
Member
 
Registered: Mar 2006
Location: UK
Distribution: Debian, Ubuntu
Posts: 129

Original Poster
Rep: Reputation: 15
hi bigersbilli i incurred in a small problem
i have the need to process files that have tabs instead of spaces between the coordinates.
or even better, to ignore if there is 1-4-5-6 or whatever spaces in between the numbers, or even tabs..
do i need to change anything?
Thanks

Last edited by assasukasse; 01-04-2007 at 04:36 PM.
 
Old 01-05-2007, 07:06 PM   #14
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 37
Quote:
Originally Posted by assasukasse
thanks muha
do u have a link to a guide how to use sed or awk?
i want to learn how to use them too!
I missed that before, here you go:
[sed]
http://sed.sourceforge.net/
http://www.linuxquestions.org/bookmarks/tags/sed
http://wiki.linuxquestions.org/wiki/Sed

[awk]
http://www.vectorsite.net/tsawk_1.html#m1
http://sparky.rice.edu/~hartigan/awk.html
http://www.gnu.org/software/gawk/manual/gawk.html

With sed you could do something like this (if the files are called 1, 2 and 3)
Code:
sed -n '/SDEF/s/\(.*\)axs.*/\1/p' 1 2 3
SDEF pos=6.45 0 10.634
SDEF pos=7.45 1 11.634
SDEF pos=8.45 2 12.634
Or, to get all files in this directory and feed them into sed one at a time:
Code:
for i in $(ls); do echo -n $i; sed -n '/SDEF/s/SDEF\(.*\)axs.*/\1/p' $i; done
1 pos=6.45 0 10.634
2 pos=7.45 1 11.634
3 pos=8.45 2 12.634
Now just grab the standard output and feed that into a textfile, one directory higher
Code:
for i in $(ls); do echo -n $i; sed -n '/SDEF/s/SDEF\(.*\)axs.*/\1/p' $i; done > ../output.txt
If you need more info post back or pm.
/ah, you need to include spaces in the filenames, that can be done also.
Code:
for i in *; do echo -n "$i"; sed -n '/SDEF/s/SDEF\(.*\)axs.*/\1/p' "$i"; done

Last edited by muha; 01-05-2007 at 07:12 PM.
 
Old 01-06-2007, 12:09 PM   #15
assasukasse
Member
 
Registered: Mar 2006
Location: UK
Distribution: Debian, Ubuntu
Posts: 129

Original Poster
Rep: Reputation: 15
Thanks muha
i need more time to study sed, i made some tests but i didn't go much farther..
however, i was thinking i missed something when in the file bigearsbilly gave me:
there is another line that needs to be edited
the line is BEFORE the SDEF line
and starts with 14 RCC then 5 spaces, then the same coordinates of SDEF then other numbers..
now i added the following line to the pl file:
Code:
map {s/(14 RCC     )[\d .]+(.*)/$1$x $y $z $2/} @copy;
however it doesnt work
DOES work if i remove the 5 spaces, but deletes the other numbers that should remain there..
what should i do?
 
  


Reply

Tags
automated, edit, file, script, text


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need aid with static IP Zmyrgel Linux - Networking 9 06-25-2006 07:59 AM
Aid me recover from reboot. Zmyrgel Slackware 1 05-11-2006 04:01 AM
Xandros internet aid Gavin19 Linux - Newbie 0 09-23-2004 04:54 PM
Linux Webserver Aid. HaTHor Linux - Networking 2 10-23-2003 02:56 PM
Looking for software to aid in writing my own o/s ChimpFace9000 Linux - Software 4 09-11-2002 07:16 PM


All times are GMT -5. The time now is 01:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration