write a matrix element, if the "if" condition satisfies


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting write a matrix element, if the "if" condition satisfies
# 1  
Old 08-28-2012
write a matrix element, if the "if" condition satisfies

Hi All,

I would like to check a condition in a nxm matrix and if the condition satisfies then, I need to write the matrix element in the output file.

For example, I have a matrix like this:
1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4

and i need to test if the difference with the previous column as well as with the first column is more than 2; then I need to write that particular matrix element in the output file.

I tried this:

Code:
awk '{for(i=1; i<=NF; i++) {if ((($i-$1) > 2) && (($(i+1)-$i > 2)) print $i; else 0}}' inputmatrix > output

but filed. Can anyone help me in this regard? your replies are highly appreciated.

Warm regards
# 2  
Old 08-28-2012
I think your condition is wrong...

Tried this getting all 0..

Code:
awk '{for(i=2; i<=NF; i++) {if ((($i-$1) > 2) && (($(i)-$(i-1) > 2))) print $i; else { print "0" }} }' file

This User Gave Thanks to pamu For This Post:
# 3  
Old 08-28-2012
Hi Pamu,

Thanks for your reply.

But the problem here is, irrespective of the value in the condition, I need to write the matrix element (that means, the row and column number) in the output file. The difference with the first column and the difference with the previous column both should satisfy the conditions. I didn't get your code, why you have started with i=2?

A small modification in my previous code:

Code:
awk '{for(i=1; i<=NF; i++) {if (($i-$1) && ($(i+1)-$i) > 1) print i; else { print "0"}}}' input > output

The catch here is, I need to write the matrix element not the value at the matrix element, which satisfy the condition?

your replies are highly appreciated.

Warm regards
# 4  
Old 08-28-2012
What's the expected output? Do you want to consider the absolute differences?
This User Gave Thanks to elixir_sinari For This Post:
# 5  
Old 08-28-2012
Thanks elixir sinari, The output should be the number., like [1,2] or [4,5]..etc. It should be the matrix element number.

---------- Post updated at 01:28 PM ---------- Previous update was at 01:18 PM ----------

Thanks elixir sinari, If the difference is more than "1", then I need to write the matrix element (in which row and which column) is satisfying the condition. I tried the following, but getting 0 for all in my output.

Code:
awk '{for(i=1; i<=NF; i++) {for(j=1; j<=NR; j++) {if (($i-$1) && ($(i+1)-$i) > 1) print "[i,j]"; else { print "0"}}}}' input > output

[i,j] is the m,n element which is satisfying the condition. your replies are highly appreciated.

warm regards
# 6  
Old 08-28-2012
If I got you right, try this:
Code:
awk '{for (i=2;i<=NF;i++) if (($i-$1)>2 && $i-$(i-1)>2) print "line ", NR, ", field ", i}' infile

Hey, you're changing targets!
Code:
awk '{for (i=2;i<=NF;i++) if (($i-$1)>1 && $i-$(i-1)>1) print "[", NR, ",", i,"]"}' infile

This User Gave Thanks to RudiC For This Post:
# 7  
Old 08-28-2012
Hi

I have started with i=2 because, for i=1 while subtracting with column 1 its value is never greater than 2.

Try below code..

I have modified file to check the output....

Code:
cat file
1 2 5 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 8 4

$  awk '{for(i=2; i<=NF; i++) {if ((($i-$1) > 2) && (($(i)-$(i-1) > 2))) print NR,i; else { print "0" }} }' file
0
3 1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
4 5
0


Last edited by pamu; 08-28-2012 at 08:52 AM..
This User Gave Thanks to pamu For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Shell Programming and Scripting

Problems with "write" and "wall"

Hello, I am using VirtualBox to simulate a small network with two Linux computers, the host is Mac OS X. My problem is that I can't send "write" and "wall" messages from the host to one of those Linux computers. Here is what works: - The virtual Linux computer answers "ping" messages that have... (5 Replies)
Discussion started by: 123_abc
5 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

Perl Foreach add "4" to the end of each element

I wrote the following script configuring cisco router:#!/usr/bin/perl use strict; use warnings; use Getopt::Long; use Opsware::NAS::Connect; my @IP = split(/\./,"$tc_device_ip$"); my $O3 = $IP; my($host, $port, $user, $pass) =... (3 Replies)
Discussion started by: ahmed_zaher
3 Replies

6. Shell Programming and Scripting

Error to "find" a matching array element in a directory

Hi, I have defined an array which holds a couple of elements which are nothing but files names. I want to find the files in a directory for the matching file name(array elements) with less than 1 day old. When I am trying to execute the code (as below), it gives an error. Your help in this... (1 Reply)
Discussion started by: mkbaral
1 Replies

7. Shell Programming and Scripting

acessing awk array element while getline < "file"

I am attempting to write a awk script that reads in a file after awk array elements are assigned and using those elements while reading in the new file. Does this make sense? /pattern/ {tst=$3} (( getline < "file" ) > 0 ) { x=x " "tst } When I print tst in the END statement it... (9 Replies)
Discussion started by: timj123
9 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

9. Shell Programming and Scripting

communicating wth another user aside from "wall" and "write"

Hi, Can anyone suggest a Unix command or c-shell algorithm to simulate to behavior of "wall" command minus the "all users"? What I'm trying to do is to send a notice to just one particular user but i dont want other remotely-logged-on users to receive the message (on the pseudo-terminals). I... (6 Replies)
Discussion started by: Deanne
6 Replies
Login or Register to Ask a Question