LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-05-2006, 04:42 PM   #1
Flobsi
LQ Newbie
 
Registered: Oct 2006
Posts: 2

Rep: Reputation: 0
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.

Last edited by Flobsi; 10-05-2006 at 06:55 PM.
 
Old 10-06-2006, 12:43 AM   #2
ahedler
Member
 
Registered: Oct 2005
Location: A safe distance from Detroit
Distribution: SuSE 10.0, Knoppix
Posts: 99

Rep: Reputation: 17
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.
 
Old 10-06-2006, 02:08 AM   #3
Emmanuel_uk
Senior Member
 
Registered: Nov 2004
Distribution: Mandriva mostly, vector 5.1, tried many.Suse gone from HD because bad Novell/Zinblows agreement
Posts: 1,606

Rep: Reputation: 53
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
 
Old 10-06-2006, 03:10 AM   #4
olaola
Member
 
Registered: Aug 2006
Location: Italy
Distribution: Fedora
Posts: 41

Rep: Reputation: 15
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..
 
  


Reply

Tags
shell, shell script



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
Does anyone know of a bash script can search & replace txt in a file. jimwelc Linux - Newbie 6 09-15-2008 12:13 AM
modify content in a file using shell script fjkum Linux - Newbie 2 08-03-2006 04:46 AM
Shell script: substitute a file's content according to a map? Chowroc Programming 11 11-15-2005 04:08 AM
use file content as tcl script parameters powah Linux - General 3 07-16-2005 11:10 AM
Perl Script Reading a txt file and generate html to be published! kofi Linux - Software 1 09-22-2003 05:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:41 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
Open Source Consulting | Domain Registration