LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell script for changing a txt file content (http://www.linuxquestions.org/questions/linux-newbie-8/shell-script-for-changing-a-txt-file-content-489784/)

Flobsi 10-05-2006 04:42 PM

shell script for changing a txt file content
 
Hi,

I am new to Linux and try to figure out the best way to change the content of a txt file writing a shell script.
the txt file consists of one row with different numbers,like:

1.057353E-04
2.068785E-01
....

I want each of that numbers multiplied by 2.1455e-07.
So far I tried to pipe the txt-files into gawk with:

ls *.2.txt | gawk ' {print$1*2.1455E-07} ' >v.sh

then added the header in the output v.sh file with:
#!/bin/sh

and run the v.sh , but didnt work.

ahedler 10-06-2006 12:43 AM

Well, first off, 'ls *.2.txt' won't spit out the contents of the file(s), it will only list the file name(s), then pass that to gawk. You need to 'cat' the file to read out the contents. I also can't guarantee that your syntax for gawk will do what you expect it to without trying it. Then on top of that, even if it gave you the output you were expecting, just redirecting it to a file and adding that header won't make it run. You'll probably need to use chmod to add the execute bit before it will do anything, and I suspect it isn't going to do what you want.

You really want the script to read one value at a time from the file, then manipulate it and output the results, either to the screen or to a file.

What you really want to do is find a book or tutorial on shell scripting to learn how to do this, or even better, look into Perl to do this.

Emmanuel_uk 10-06-2006 02:08 AM

I am pretty sure you can do the multpication within awk
http://www.gnulamp.com/awk.html
{ print ($1^2)+1 }
You may have a syntax error, you may need some brackets

NB: Numerical accuracy if you start * small No together with awk,
I have no idea what libraries are behind it

olaola 10-06-2006 03:10 AM

You can use a for cicle:

Code:

for file in *.2.txt
do
gawk ' {print $1*2.1455E-07}' $file
done

If you want you can put this also in a bash script with the #!/bin/bash header, and making it executable..


All times are GMT -5. The time now is 02:52 PM.