views:

896

answers:

4

In shell script, how do I iterate through each line in an ASCII file and perform an operation on its value

This is the example for an ASCII file which I have

23      3.4e-09     55.90       5.7e-07
24        12.5      79.90       7.9e-09
25        67.9      78.9       3.4e-09
26        98.8      89.67       9.7e-09

how cum it will take first line and the iterate...

WAT DO U MEAN BY LINE

A: 

Using bash on Linux, you can use read. It may depend on the shell and version you want to use.

The "<" is the redirect operation and it tells the shell to redirect stdin from the file test.txt

See the read command under SHELL BUILTIN COMMANDS in the bash manpage for more info.

#!/bin/sh

while read line
do
  echo "[" $line "]"
done < test.txt
Phil
+1  A: 

One way (in bash and ksh, at least, may also work in plain sh) is by using something like:

while read line
do
  ...
done

However, this will modify whitespace on each line. If you have a "sufficiently fixed" format on the lines (say each line is ValA ValB ValC), you can modify read line to read vala valb valc

Vatine
+1  A: 

In Windows,

Parse a text string: A string of text will be treated just like a single line of input from a file, the string must be enclosed in double quotes (or single quotes with usebackq).

Echo just the date from the following string

FOR /F "tokens=4 delims=," %%G IN ("deposit,$45.50,23.7,12-AUG-07") DO @echo Date paid %%G

From here: for /f loop through text

Alex. S.
A: 

I suspect even if you get the looping correctly setup and the spacing is always the same you will have trouble with doing maths on those values as there are some scientific numbers in there like 3.4e-09. I doubt scripting is setup to turn that into a real value and it would have to be something like a double not an integer.

Don't know what you programming skills are like but I personally would do it in a program.

PeteT