extracting non-zero pairs of numbers from each row


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extracting non-zero pairs of numbers from each row
# 1  
Old 08-09-2012
extracting non-zero pairs of numbers from each row

Hi all,

I do have a tab delimited file
Code:
a1	a2	b1	b2	c1	c2	d1	d2	e1	e2	f1	f2
0	0	123	546	0	0	0	0	0	0	0	0
0	0	345	456	765	890	902	1003	0	0	0	0
534	768	0	0	0	0	0	0	0	0	0	0
0	0	0	0	456	765	0	0	0	0	0	0
0	0	0	0	0	0	12	102	0	0	0	0
456	578	789	1003	678	765	345	400	801	1003	134	765
0	0	0	0	0	0	456	809	0	0	0	0

I want to examine each of the rows one by one and see whether there is only one-pair of non zero values across that row. If there is one, then output that value in a file in the following format:
Code:
b1 b2 123	546
a1 a2 534	768
c1 c2 456	765
d1 d2 12	102
d1 d2 456	809

If there are more than one pair of non-zero values in a row ignore that row.
my original file has thousands of rows and columns.

Please let me know the best way in awk or sed to extract this
# 2  
Old 08-09-2012
By 'pair' do you mean 'two in a row', or do they not have to be directly after each other?
# 3  
Old 08-09-2012
they have to be directly after each other. The number of columns will be an even number

---------- Post updated at 08:13 PM ---------- Previous update was at 08:10 PM ----------

I guess the two in a row - side by side..if it makes it clear
# 4  
Old 08-09-2012
Perhaps:
Code:
awk '{P=0; for(N=1; N<=NF; N++) { if(($N == "0") && ($(N+1) == "0")) { P++; N++ } } (P<2)' filename

# 5  
Old 08-09-2012
thanks..but I am getting an error message
Code:
awk: syntax error at source line 1
 context is
	 >>>  <<< 
awk: illegal statement at source line 1
	missing }

# 6  
Old 08-09-2012
I assume that you're really looking for a non-zero value in an odd column immediately followed by a non-zero value in an even column. If that is correct, I think something like:
Code:
awk 'NR == 1	{for (i=1; i<=NF; i++) hdr[i]=$i}
NR > 1	{
	found=0
	for (i=1; i<NF; i+=2)
		if ($i != 0 && $(i+1) != 0)
			if (found) next
			else	found=i
	if (found)
		printf "%s\t%s\t%s\t%s\n",
			hdr[found],hdr[found+1],$found,$(found+1)
}' file

will do what you want.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sum of numbers in row

Need help in coding: File with several rows incl. numbers like 1 2 3 4 5 6 7 8 ... How can i build the sum of each row seperately? 10 26 ... Thx for help. Please use CODE tags as required by forum rules! (13 Replies)
Discussion started by: smitty11
13 Replies

2. Shell Programming and Scripting

Extracting data between two tag pairs

In a huge log file (43MB, 43k lines) I am trying to extract data between two tag pairs on same line and export it to a file so I can pull it into Excel for a report. One Pair is <Text>data I need</Text> Other pair follows on same line and is <TimeStamp>more data I need</TimeStamp> I would need... (2 Replies)
Discussion started by: NanookArctic
2 Replies

3. Shell Programming and Scripting

Multiply a row of numbers

the following is used to add numbers: echo 7 47 47 44 4 3 3 3 3 3 | awk '{ for(i=1; i<=NF;i++) j+=$i; print j; j=0 }' how do i multiply OR subtract a row of numbers using the above tactic? (8 Replies)
Discussion started by: SkySmart
8 Replies

4. Shell Programming and Scripting

Extracting key/value pairs in awk

I am extracting a number of key/value pairs in awk using following: awk ' /xyz_session_id/ { n=index($0,"xyz_session_id"); id=substr($0,n+15,25); a=$4; } END{ for (ix in a) { print a } }' I don't like this Index + substr with manually calculated... (5 Replies)
Discussion started by: migurus
5 Replies

5. Shell Programming and Scripting

Extracting numbers

Hi I am part of a academic organization and I want to send a fax to the students however there must be a quicker way to get the fax numbers extracted from the online forms they sent me. The file looks like this (numbers are fake in order to protect identity): Biochemistry Major Michael... (3 Replies)
Discussion started by: phil_heath
3 Replies

6. UNIX for Dummies Questions & Answers

Print last row of numbers

I have a spreadsheet of extremely long rows of numbers. I want to print only the last column. Tried using printf but there seems to be too many rows. example: 3 100 34 7 23 0 8 ..... X 400 203 778 1 ..........Y 58 3 9 0 100 ..........Z I only want to print X, Y and... (1 Reply)
Discussion started by: jimmyf
1 Replies

7. Shell Programming and Scripting

Adding row of numbers

is there another way of doing the below: echo "7 3 8 2 2 1 3 83.4 8.2 4 8 73 90.5" | bc shell is bash. os is linux and sunos. bc seems to have an issue with long range of numbers (12 Replies)
Discussion started by: SkySmart
12 Replies

8. Shell Programming and Scripting

counting the numbers in a row

File A aa <space> --D--A--D---DDY---M--UM-M--MY Another file D3 M9 So output shud be Here in FileA D which is 3 after removing dash after we have counted dash D is position at 9 and for M is 23 final output will be D9 M23 (2 Replies)
Discussion started by: cdfd123
2 Replies

9. Shell Programming and Scripting

Extracting numbers from a string

Hello Everyone, i have quick question. I have file names like: bin_map300.asc and I would like to extract grid300. My approach so far: name=bin_map300.asc echo ${name%%.*} echo ${name##*_} I am stuck combining the two. Any help would be appreciated. (3 Replies)
Discussion started by: creamcheese
3 Replies

10. UNIX for Dummies Questions & Answers

extracting a row from a file

hi... i want to extract single row from a file at a time and i don't want to specify which row to be extracted in the command. i mean i want to use a loop, so that i will fetch all the rows one after the another. i used sed -n '$count p' filename but this is not working. please give me the... (2 Replies)
Discussion started by: Usha Shastri
2 Replies
Login or Register to Ask a Question