parsing rows

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting parsing rows
# 1  
Old 11-04-2009
parsing rows


I have a file that looks like this (tab seperated):

Barry  -3  -4  -5  -10  -4  6  -8  20  -6  NaN  NaN  NaN
Brend -2   4   -3  -7   -3  8   -9  -10  -6 NaN  NaN  NaN  NaN  NaN
Harry  -10 -9 -40  6  -7  3  -7  -2  -5  NaN NaN NaN NaN NaN NaN NaN

I want to print the first column (name) then print the column where theres value equal to or greatr than 0 (along with its position where the counts start from right to left AND also could how many NaN's there are per row.

so the output would look like this

name position|value #of NaN's

Barry 4|6  2|20  3
Brend 7|4  4|8  5
Harry 5|6  7|3  7

hope i was clear


Last edited by gisele_l; 11-04-2009 at 05:03 PM..
# 2  
Old 11-04-2009
second and third line's positions in your sample output are wrong. Here is the code:

$ awk '{printf $1}
{for (i=2;i<=10;i++) if ($i>=0) {printf " "11-i"|"$i} }
{for (j=11;j<=NF;j++) tol++} {print " " tol}
{tol=0} ' urfile

Barry 4|6 2|20 3
Brend 8|4 4|8 5
Harry 6|6 4|3 7

Last edited by rdcwayx; 11-04-2009 at 11:14 PM..
# 3  
Old 11-05-2009
	my @tmp=split;
	my @t1 = grep { $_ eq "NaN" } @tmp;
	print @tmp[0]," ";
	for (my $i=0;$i<=$#tmp;$i++){
		print $#tmp+1-$i,"|",$tmp[$i]," " if $tmp[$i]>0;
	print " ",$#t1+1,"\n";
Barry  -3  -4  -5  -10  -4  6  -8  20  -6  NaN  NaN  NaN
Brend -2   4   -3  -7   -3  8   -9  -10  -6 NaN  NaN  NaN  NaN  NaN
Harry  -10 -9 -40  6  -7  3  -7  -2  -5  NaN NaN NaN NaN NaN NaN NaN

# 4  
Old 11-06-2009

$ awk '{printf $1}
{for (i=2;i<=10;i++) if ($i>=0) {printf " "11-i"|"$i} }
{for (j=11;j<=NF;j++) tol++} {print " " tol}
{tol=0} ' urfile

works but I have decimal places for some of the #'s so the output comes out a bit different

so the order is all wrong

ex. of input
joe	-2	-4	-6	-3	-1	9	3	NaN	NaN	NaN

output is
joe 4|9 3|3 2|NaN 1|NaN 1

Last edited by gisele_l; 11-06-2009 at 11:39 PM..
# 5  
Old 11-07-2009
awk '{s=0;p=2;z=0
      for (i=2;i<=NF;i++) {
        if ($i !~ /[-.0-9]/) {s++; if (z==0) z=i}
        else if ($i>0) {$(p++)=i;$(p++)=$i}
      printf "%s %s|%s  %s|%s  %s\n",$1,z-$2,$3,z-$4,$5,s}' infile

Barry 4|6  2|20  3
Brend 8|4  4|8  5
Harry 6|6  4|3  7
joe 2|9  1|3  3

Last edited by Scrutinizer; 11-07-2009 at 05:33 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Moving or copying first rows and last rows into another file

Hi I would like to move the first 1000 rows of my file into an output file and then move the last 1000 rows into another output file. Any help would be great Thanks (6 Replies)
Discussion started by: kylle345
6 Replies

2. UNIX for Dummies Questions & Answers

merging rows into new file based on rows and first column

I have 2 files, file01= 7 columns, row unknown (but few) file02= 7 columns, row unknown (but many) now I want to create an output with the first field that is shared in both of them and then subtract the results from the rest of the fields and print there e.g. file 01 James|0|50|25|10|50|30... (1 Reply)
Discussion started by: A-V
1 Replies

3. Shell Programming and Scripting

Parsing a CSV file and deleting all rows on condition

Hello list, I am working on a csv file which contains two fields per record which contain IP addresses. What I am trying to do is find records which have identical fields(IP addresses) which occur 4(four) times, and if they do, delete all records with that specific identical field(ip address). ... (4 Replies)
Discussion started by: landossa
4 Replies

4. Shell Programming and Scripting

Split single rows to multiple rows ..

Hi pls help me out to short out this problem rm PAB113_011.out rm: PAB113_011.out: override protection 644 (yes/no)? n If i give y it remove the file. But i added the rm command as a part of ksh file and i tried to remove the file. Its not removing and the the file prompting as... (7 Replies)
Discussion started by: sri_aue
7 Replies

5. Shell Programming and Scripting

Parsing the string into several rows

I'm getting the input row in this format it contains 2 alphabets followed by numbers between 1 and 7 and again 2 alphabets followed by numbers between 1 and 7. Now I need to parse this input into several output rows in this format 2 alphabets followed by each number occurrence suppose... (5 Replies)
Discussion started by: abhijith321
5 Replies

6. Shell Programming and Scripting

Remove 1st two rows and last 2 rows

Hi All, I need to remove 1st 2 line from head and last 2 line from last. I thought it would be possible by using the Head and tail command. But after i am using it is not possible by it. Example:Input file 1 2 3 4 5 Example: Output file 3 But my head and tail command are not... (12 Replies)
Discussion started by: kam786sim
12 Replies

7. Shell Programming and Scripting

Deleting specific rows in large files having rows greater than 100000

Hi Guys, I need help in modifying a large text file containing more than 1-2 lakh rows of data using unix commands. I am quite new to the unix language the text file contains data in a pipe delimited format sdfsdfs sdfsdfsd START_ROW sdfsd|sdfsdfsd|sdfsdfasdf|sdfsadf|sdfasdf... (9 Replies)
Discussion started by: manish2009
9 Replies

8. Shell Programming and Scripting

Parsing of file for Report Generation (String parsing and splitting)

Hey guys, I have this file generated by me... i want to create some HTML output from it. The problem is that i am really confused about how do I go about reading the file. The file is in the following format: TID1 Name1 ATime=xx AResult=yyy AExpected=yyy BTime=xx BResult=yyy... (8 Replies)
Discussion started by: umar.shaikh
8 Replies

9. Shell Programming and Scripting

Perl parsing compared to Ksh parsing

#! /usr/local/bin/perl -w $ip = "$ARGV"; $rw = "$ARGV"; $snmpg = "/usr/local/bin/snmpbulkget -v2c -Cn1 -Cn2 -Os -c $rw"; $snmpw = "/usr/local/bin/snmpwalk -Os -c $rw"; $syst=`$snmpg $ip system sysName sysObjectID`; sysDescr.0 = STRING: Cisco Internetwork Operating System Software... (1 Reply)
Discussion started by: popeye
1 Replies

10. UNIX for Advanced & Expert Users

Parsing String, Search then display rows

Get occurence of "open" considering duplicates(get the last open). Once you are pointing to the last open count 2 rows to get the correct data. Every begin and end statement, there is a "close" and "open". There can be many "close" and "open" within the begin and end statement but we are... (9 Replies)
Discussion started by: buddyme
9 Replies
Login or Register to Ask a Question