LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-22-2011, 12:20 AM   #1
verse123
LQ Newbie
 
Registered: Oct 2011
Posts: 19

Rep: Reputation: Disabled
Bash loops


Hello,

I have a file with 4 columns resembling this:

Col1. Col2. Col3. Col4. Quality
0 1200 3. 500. 40
1201 3000. 1220. 2030. 35
3001 9050. 3400. 5000. 3


If you notice col3 and 4 are contained within col1 and 2 (reading them by row).

I need to create a loop for this. First i need it to check if the quality is above a certain threshold (for example 30). If that is true, then I need the loop to determine if col3 is less than col2 but greater than col 1 and that col4 is less than or equal to col 2. How can I do this?


I would strongly appreciate your help.
 
Old 10-22-2011, 12:44 AM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by verse123 View Post
Hello,

I have a file with 4 columns resembling this:

Col1. Col2. Col3. Col4. Quality
0 1200 3. 500. 40
1201 3000. 1220. 2030. 35
3001 9050. 3400. 5000. 3


If you notice col3 and 4 are contained within col1 and 2 (reading them by row).

I need to create a loop for this. First i need it to check if the quality is above a certain threshold (for example 30). If that is true, then I need the loop to determine if col3 is less than col2 but greater than col 1 and that col4 is less than or equal to col 2. How can I do this?


I would strongly appreciate your help.
Hi,

how about
Code:
while read line;do
 set -- $line
 if [[ $(echo "$5 > 30 && $3>$1 && $3<$2 && $4<=$2" | bc) = 1 ]];then
  echo $line
  # or whatever you need to do
 fi
done < file
[EDIT]
However, I just saw that you have floating point numbers. In this case an awk solution might be more appropriate:
Code:
awk '{if ($5 > 30 && $3>$1 && $3<$2 && $4<=$2){print}}' file

Last edited by crts; 10-22-2011 at 01:02 AM. Reason: adjusted script for floating point operation
 
1 members found this post helpful.
Old 10-22-2011, 03:19 PM   #3
verse123
LQ Newbie
 
Registered: Oct 2011
Posts: 19

Original Poster
Rep: Reputation: Disabled
Hi crts,

Your second recommendation worked - thanks a ton!

If I wanted to take this a step further and replace the values in col2 with the values in col3 if they pass the awk criteria would I do something like this:



awk '{if ($5 > 30 && $3>$1 && $3<$2 && $4<=$2){print}}' file | sed '{if ($2=$4) s/$2/$3/} > output
 
Old 10-22-2011, 04:25 PM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
Originally Posted by verse123 View Post
Hi crts,

Your second recommendation worked - thanks a ton!

If I wanted to take this a step further and replace the values in col2 with the values in col3 if they pass the awk criteria would I do something like this:



awk '{if ($5 > 30 && $3>$1 && $3<$2 && $4<=$2){print}}' file | sed '{if ($2=$4) s/$2/$3/} > output
Do it within AWK: awk '{if ($5 > 30 && $3>$1 && $3<$2 && $4<=$2){if ($2==$4) {$2=$3};print > output}}' file

Last edited by PTrenholme; 10-22-2011 at 04:27 PM. Reason: Typo
 
1 members found this post helpful.
  


Reply



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
[SOLVED] bash loops not behaving timl Linux - Software 5 07-20-2011 09:12 AM
Bash Loops multiplex A/V lpn1160 Linux - General 1 02-23-2008 06:01 AM
Bash Loops UnitedWeFall Programming 8 08-21-2007 06:33 AM
bash scripting and loops phoeniks Programming 5 01-24-2005 04:00 PM
bash - while + until loops grouping? trees Linux - General 2 02-19-2004 02:29 PM

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

All times are GMT -5. The time now is 09:07 AM.

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