PDA

View Full Version : Searching through a file using Bash scripting



tjd_maximum
April 12th, 2010, 06:32 AM
Hey,

I'm pretty bad with shell scripting. I find it pretty cryptic and have never really found a good guide on it. Anyway, what I'm trying to do is pretty simple I think.

I have a file with some couple hundred lines of data in two columns. Each line looks like:

NUMBER1 NUMBER2

Now, if NUMBER1 is greater then a given value, call it MAXVAL, then NUMBER1 goes to file1, and NUMBER2 goes to file2. Otherwise, I want to ignore the line completely.

Any ideas on a complete or partial solution? I'm sure for someone this is pretty easy. Any help would be appreciated. Thanks.

iponeverything
April 12th, 2010, 06:46 AM
cat file |awk '$1 > 100 {print "echo "$1" >> /tmp/value1; echo "$2" >> /tmp/value2"}'

Where 100 is your maxvalue. As it is it will just print screen, pipe it to sh for it to work.

tjd_maximum
April 12th, 2010, 06:55 AM
Thanks for the speedy reply! I really need to get better with this. . .

geirha
April 12th, 2010, 07:18 AM
And here's how you can do it in bash



#!/bin/bash

maxval=100

while read -r number1 number2; do
(( number1 > maxval )) || continue
echo "$number1" >&3
echo "$number2" >&4
done <file 3>file1 4>file2


I recommend this guide for learning bash: http://mywiki.wooledge.org/BashGuide

Cracauer
April 13th, 2010, 04:11 AM
The shellscript solution won't do floating point, though.