Sponsored Content
Full Discussion: range in if using awk
Top Forums Shell Programming and Scripting range in if using awk Post 302396825 by drl on Friday 19th of February 2010 11:44:12 AM
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:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
IGAWK(1)							 Utility Commands							  IGAWK(1)

NAME
igawk - gawk with include files SYNOPSIS
igawk [ all gawk options ] -f program-file [ -- ] file ... igawk [ all gawk options ] [ -- ] program-text file ... DESCRIPTION
Igawk is a simple shell script that adds the ability to have ``include files'' to gawk(1). AWK programs for igawk are the same as for gawk, except that, in addition, you may have lines like @include getopt.awk in your program to include the file getopt.awk from either the current directory or one of the other directories in the search path. OPTIONS
See gawk(1) for a full description of the AWK language and the options that gawk supports. EXAMPLES
cat << EOF > test.awk @include getopt.awk BEGIN { while (getopt(ARGC, ARGV, "am:q") != -1) ... } EOF igawk -f test.awk SEE ALSO
gawk(1) Effective AWK Programming, Edition 1.0, published by the Free Software Foundation, 1995. AUTHOR
Arnold Robbins (arnold@skeeve.com). Free Software Foundation Nov 3 1999 IGAWK(1)
All times are GMT -4. The time now is 03:43 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy