Sponsored Content
Full Discussion: range in if using awk
Top Forums Shell Programming and Scripting range in if using awk Post 302396792 by Fredrick on Friday 19th of February 2010 10:00:27 AM
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...)
 

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
lpshut(1M)                                                System Administration Commands                                                lpshut(1M)

NAME
lpshut - stop the LP print service SYNOPSIS
lpshut DESCRIPTION
The lpshut command stops the LP print service. Printers that are printing when lpshut is invoked stop printing. Start or restart printers using lpsched(1M). EXIT STATUS
The following exit values are returned: 0 Successful completion. non-zero An error occurred. FILES
/var/spool/lp/* LP print queue. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | |Availability |SUNWpsu | +-----------------------------+-----------------------------+ SEE ALSO
lp(1), lpstat(1), lpadmin(1M), lpmove(1M), lpsched(1M), attributes( 5) System Administration Guide: Basic Administration SunOS 5.10 8 Jan 1998 lpshut(1M)
All times are GMT -4. The time now is 08:55 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy