range in if using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting range in if using awk
# 1  
Old 02-19-2010
range in if using awk

Hi All,

I would like to assign the following values to each column in my file.

if $i is between 1 and -1 (ie -1 < $i < 1) then print A;
if $i is between -2 and -1 && 1 and 2 (ie. -2 < $i < -1 && 1 < $i < 2) then print B;
if $i is between -3 and -2 && 2 and 3 (ie. -3 < $i < -2 && 2 < $i < 3) then print C;
else print D;

for this i used the following code

Code:
awk '{for(i=1; i<NF; i++) {if($i > -1 && $i < 1) {printf "A "} else if($i > -2 && $i < -1 && $i >1 && \
$i < 2)  {printf "B "} else if($i > -3 && $i < -2 && $i > 2 && $i < 3) {printf "C "} else \
{printf "D "}}} {print "\n"}' inputfile > outputfile

I am getting the output, but its printing only A and D; but there are some value which are in the range of B and C. But its not printing B and C, instead its printing D.

Can anyone help me in this regard?

Expecting your reply and thanks in advance.

Warm regards
Fredrick.

Last edited by vbe; 02-19-2010 at 12:21 PM.. Reason: beautify... (what is in code tags...)
# 2  
Old 02-19-2010
Hi.

Structuring the code and re-writing the tests:
Code:
#!/usr/bin/env sh

# @(#) s1	Demonstrate if structure and tests.

set -o nounset
echo

## Use local command version for the commands in this demonstration.

set +o nounset
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) awk
set -o nounset

echo

FILE=${1-data1}
echo " Input file $FILE:"
cat $FILE

# Use nawk or /usr/xpg4/bin/awk on Solaris.

echo
echo " Results from awk:"
awk '
 # { print "line " NR " is " $0 }
 { for (i = 1; i <= NF; i++) {
     # print " Field " i " is " $i
     if ($i > -1 && $i < 1) {
       printf "A "
     }
     # else if ($i > -2 && $i < -1 && $i > 1 && $i < 2) {
     else if ($i > -2 && $i < 2) {
       printf "B "
     }
     # else if ($i > -3 && $i < -2 && $i > 2 && $i < 3) {
     else if ($i > -3 && $i < 3) {
       printf "C "
     }
     else {
       printf "D "
     }
   }
 }
 {printf "\n"}
' $FILE

exit 0

producing:
Code:
% ./s1

(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash, version 3.2.39(1)-release (x86_64-pc-linux-gnu)
GNU Awk 3.1.5

 Input file data1:
-3
-2
-1
0
1
2
3

 Results from awk:
D 
C 
B 
A 
B 
C 
D

Suggestion: think about the original tests for B & C, and follow a value through them.

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to lookup value in one file in another range

I am trying to update the below awk, kindly provided by @RavinderSingh13, to update each line of file1 with either Low or No Low based on matching $2 of file1 to a range in $2 and $3 of file2. If the $2 value in file1 matches the range in file2 then that line is Low, otherwise it is No Low in the... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. UNIX for Dummies Questions & Answers

How to print specific range using awk?

I want to print specific range of rows and then its columns using awk command. lets say if a file contain 15 line then i need to print line 5 to 9. awk '{print;for( NR>=5&&NR<=9); do; print "<tr>\n<td>"$1"</td><td>"$2"</td><td>"$3"</td>\n</tr" done}' xyz.csv not sure what's wrong... (9 Replies)
Discussion started by: amyt1234
9 Replies

3. Shell Programming and Scripting

awk with range but matches pattern

To match range, the command is: awk '/BEGIN/,/END/' but what I want is the range is printed only if there is additional pattern that matches in the range itself? maybe like this: awk '/BEGIN/,/END/ if only in that range there is /pattern/' Thanks (8 Replies)
Discussion started by: zorrox
8 Replies

4. Shell Programming and Scripting

awk field range

I was reading about different methods of awk field ranges. Does the second method have any advantage? Its quite a bit longer so I am wondering what the advantage is. awk '{for(i=5;i<=9;i++)printf "%s ",$i;print ""}' filename ... (2 Replies)
Discussion started by: cokedude
2 Replies

5. UNIX for Dummies Questions & Answers

Use of character range in awk

Hi all, I am having a bit of a hard time using awk. I must do something wrong, but I don't know what... Any help would be greatly appreciated! I read a file, as follows :... ATOM 21 C THR A 4 23.721 -26.194 1.909 1.00 32.07 C ATOM 22 O THR A 4 ... (2 Replies)
Discussion started by: hypsis
2 Replies

6. Shell Programming and Scripting

range patterns in awk

Hi All, I am new to awk command and I had a question in using it. I want to filter a code file and print specific functions (that contain menu word in the function name). for example, if the file has: function menu1() { } function f2() { } function menu3() { }so I want... (5 Replies)
Discussion started by: ghoda2_10
5 Replies

7. Shell Programming and Scripting

awk search within a range.

I have this kind of file ABC UUIIIIIIIIIIII , HJHKJKL XYZ HHJJJJJJMMM ABC BBOOIO, PPLIOJK XYZ NMJKJKK ABC MMMM ABC OPOPO XYZ LLKLKLL I need to get all data from ABC till XYZ so output should be UUIIIIIIIIIIII (6 Replies)
Discussion started by: dinjo_jo
6 Replies

8. Shell Programming and Scripting

Range of records using comparision \awk

Hi Gurus, I have to fetch the records from a logs as per the time stamp . I am comfortable to use awk and sed in the script . But the logic to fetch the records as per comparison is the problem. $cat my_log.log <Jul 30, 2010 7:01:12 AM EEST> <Error> <WebLogicServer> <Jul 30, 2010 8:04:12 AM... (3 Replies)
Discussion started by: posix
3 Replies

9. UNIX for Dummies Questions & Answers

AWK, read a range of coloumns

Hi, I've a file with some coloumns (separated by coma) and some lines, like so: A,B,C,D 1,2,3,4 5,6,7,8 9,1,2,3 I want to print a range of coloumns: for example all coloumns between the coloumn with the charachter B in the first line, and the coloumn with the char D in the first... (5 Replies)
Discussion started by: pippo
5 Replies

10. Shell Programming and Scripting

match range of different numbers by AWK

if the column1 and 2 in both files has same key (for example "a" and "a1") compare each first key value(a1 of a) of input2 (for example 1-4 or 65-69 not 70-100 or 44-40 etc) with all the values in input1. if the range of first key value in input2 is outof range in input1 values named it as out... (54 Replies)
Discussion started by: repinementer
54 Replies
Login or Register to Ask a Question