Strings to integers in an arithmetic loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Strings to integers in an arithmetic loop
# 1  
Old 10-21-2010
Strings to integers in an arithmetic loop

Hi all,

Could someone please advise what is the correct syntax for my little script to process a table of values?

The table is as follows:
Code:
0.002432   20.827656
0.006432   23.120364
0.010432   25.914184
0.014432   20.442655
0.018432   20.015243
0.022432   21.579517
0.026432   18.886874
0.030432   18.716673
0.034432   21.537040
0.038432   22.488949
0.042432   22.950917
0.046432   21.831913
0.050432   23.672277
0.054432   22.833953
0.058432   21.612637
0.062432   24.547798
0.066432   18.829130
0.070432   19.830759
0.074432   19.339907
0.078432   18.918358
0.082432   19.867944
0.086432   19.615953
0.090432   15.692428
0.094432   17.568185
0.098432   15.596961
0.102432   16.066848
0.106432   14.792269
0.110432   14.503630
0.114432   12.983533
0.118432   12.873063
0.122432   11.677121
0.126432   12.055901
0.130432   10.980757
0.134432   10.768004
0.138432   10.889363
0.142432   10.769247
0.146432   9.460254
0.150432   10.744807
0.154432   8.657475
0.158432   9.043656
0.162432   9.302273
0.166432   8.663964
0.170432   8.465998
0.174432   8.122258
0.178432   8.019673
0.182432   7.227849
0.186432   7.257996
0.190432   7.329204
0.194432   10.398291
0.198432   6.861956
0.202432   1.074710
0.206432   1.158183

what I want do is to parse this table and print out the value on the left, if the value on the right is less than 2, so desired output for above would be:

Code:
0.202432
0.206432

I've got as far as:

Code:
for i in `cat table.txt | awk '{ printf "%3.3f\n", $2 }'`; do
    test $i -lt 2 && echo $i
done

but this gives me an "integer expected" error...

Any help much appreciated!
# 2  
Old 10-21-2010
Code:
awk '$2<2{print $1}'  file

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 10-21-2010
thank you!



---------- Post updated at 01:10 PM ---------- Previous update was at 12:44 PM ----------




What about if a value on the previous line is wanted in output as well? Thanks in advance

Quote:
Originally Posted by Franklin52
Code:
awk '$2<2{print $1}'  file

# 4  
Old 10-21-2010
What should be the desired output?
# 5  
Old 10-21-2010
0.198432
# 6  
Old 10-21-2010
Something like this?
Code:
awk '$2<2{printf("%s%s\n", $1,s?" "s:"");s="";next}{s=$1}' file

# 7  
Old 10-21-2010
nope, that outputs

Code:
0.202432 0.198432
0.206432

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Concatenate strings in a a for loop

hi guys, I have this question. I am creating an script to that read a text file(.ini) with the list of the patterns to find for example: EPMS_VO EMPS_PARTS Then it check if file have been delivered in a folder and process it with each pattern, but I am having problems concatenting the... (7 Replies)
Discussion started by: Danman
7 Replies

2. UNIX for Dummies Questions & Answers

Strings to integers?

Hi, I'm totally new at this, so help will be appreciated. I have a directory with a bunch of files in it. The files are named xinteger_yinteger_zinteger.vtk (eg, x3_y0_z-1.vtk). I want to read the filenames and then assign the integers to variables that I then can use in expressions. So, for... (6 Replies)
Discussion started by: jhsinger
6 Replies

3. Shell Programming and Scripting

awk -- telling the difference between strings and integers

This should be a really easy question. My input file will have a few fields that are strings in the first line, which I will extract and save as variables. The rest of the fields on every line will be integers and floating point numbers. Can awk tell the difference somehow? That is, is there... (5 Replies)
Discussion started by: Parrakarry
5 Replies

4. Programming

Pointer arithmetic for list of strings

When i want to use a list of strings char *list; i never understand it well. Here is an easy example. It took me long to figure it out, searching the web didn't help much: #include <stdio.h> int main(void) { char *list = {"foo", "bar", "baz"}; char **pl; int... (6 Replies)
Discussion started by: tornow
6 Replies

5. Shell Programming and Scripting

Grep float/integers but skip some integers

Hi, I am working in bash in Mac OSX, I have following 'input.txt' file: <INFO> HypoTestTool: >>> Done running HypoTestInverter on the workspace combined <INFO> HypoTestTool: The computed upper limit is: 11 +/- 1.02651 <INFO> HypoTestTool: expected limit (median) 11 <INFO> HypoTestTool: ... (13 Replies)
Discussion started by: Asif Siddique
13 Replies

6. Shell Programming and Scripting

Comparison treating strings as zero integers

I'm trying to write a bash script to perform basic arithmetic operations but I want to run a comparison on the arguments first to check that they're a number greater than zero. I want an error to pop up if the arguments args aren't >= 0 so I have: if ! ]; then echo "bad number: $1" fi ... (14 Replies)
Discussion started by: TierAngst
14 Replies

7. Shell Programming and Scripting

Arithmetic operation between columns using loop variable

Hi I have a file with 3 columns. say, infile: 1 50 68 34 3 23 23 4 56 ------- ------- I want to generate n files from this file using a loop so that 1st column in output file is (column1 of infile/(2*n+2.561)) I am doing like this: for ((i=1; i<=3; i++)) do a=`echo... (3 Replies)
Discussion started by: Surabhi_so_mh
3 Replies

8. Shell Programming and Scripting

ksh-script "arithmetic syntax error" comparing strings

Hi all, Iīve already searched the forum but canīt find what i am doing wrong. I am trying to compare two variables using ksh under red hat. The error I get is: -ksh: .: MDA=`md5sum /tmp/ftp_dir_after_transfer | cut -d' ' -f1 ` MDB=`md5sum /tmp/ftp_dir_before_transfer | cut -d' ' -f1 `... (3 Replies)
Discussion started by: old_mike
3 Replies

9. Shell Programming and Scripting

sed in while loop producing arithmetic output

Hi all Just wondering if someone can help me with this. I'm trying to write a script that processes the output of another file and prints only the lines I want from it. This is only the second script I have written so please bare with me here. I have referred to the literature and some of the... (3 Replies)
Discussion started by: javathecat
3 Replies

10. Shell Programming and Scripting

For Loop with Strings as parameters

I'm trying to create a script with a for loop that take strings in as the parameters. Some of the strings have spaces in them, and I can't get it to work properly. For example: #!/bin/ksh INFILE=snapshot.log OUTFILE=summary.out a="Lock waits" b="Deadlocks detected" for PARAM in... (6 Replies)
Discussion started by: kadishmj
6 Replies
Login or Register to Ask a Question