Colum number where value is >0


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Colum number where value is >0
# 1  
Old 08-04-2016
Colum number where value is >0

Hi All
I want to findout coloum number where value is >0 from below string
Can we do using awk or any command ?
Thanks in advance

Code:
2016-08-04-0000,619,619,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,89,0,0,0,0,5,8,0,0,0,0,0,0,0,0,0,3,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,211,1,0,0,0,39,0,105,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,14,0,0,0,0,0,0,0,0,0,
0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

# 2  
Old 08-04-2016
Hello aaysa123 ,

I have a few to questions pose in response first:-
  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.


We're all here to learn and getting the relevant information will help us all.


Kind regards,
Robin
This User Gave Thanks to rbatte1 For This Post:
# 3  
Old 08-04-2016
I have tried but it is not giving required o/p

Code:
awk -F',' ' { for (i = 1; i <= NF; ++i) print i, $i; exit } ' test

# 4  
Old 08-04-2016
What about adding a comparisson if $i is greater than 0? If so, maybe print $i and NR so you know which line it is found at.
If you can write an awk statement like this, you can surely add this. Try it.
This User Gave Thanks to zaxxon For This Post:
# 5  
Old 08-04-2016
Longhand, using Macbook Pro, circa August 2012, OSX 10.7.5, default bash terminal.
Assumption made that the OP added the newlines in the string to sh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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0' > /tmp/text
NUM_ARRAY=( $( cat /tmp/text ) )
ARRAY_LEN=${#NUM_ARRAY[@]}
# Deliberately miss out the date stamp.
n=1
while [ $n -le $((ARRAY_LEN-1)) ]
do
	if [ "${NUM_ARRAY[$n]}" -gt "0" ]
	then
		echo "${NUM_ARRAY[$n]}"
	fi
	n=$((n+1))
done
IFS="$ifs_org"
exit 0

Results...
Code:
Last login: Thu Aug  4 13:33:49 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> ./numextract.sh
619 
619 
89 
5 
8 
3 
2 
3 
211 
1 
39 
105 
14 
5 
AMIGA:barrywalker~/Desktop/Code/Shell> _

You now know that a comparison test is required somewhere, so apply this technique to your awk code.

Last edited by wisecracker; 08-04-2016 at 11:42 AM.. Reason: Added, assumption.
# 6  
Old 08-04-2016
Quote:
Originally Posted by wisecracker
Code:
NUM_ARRAY=( $( cat /tmp/text ) )
ARRAY_LEN=${#NUM_ARRAY[@]}

This will work with several shells and won't with others. Some shells (honestly, i don't know if bash is among them) have limits on the maximum numbers of elements an array can hold: ksh88 for instance 1023, ksh93 more than that, even if i can't remember how many.

It is usually best to stay as far away as possible from the limits, so i wouldn't use arrays for any data which could even come into the vicinity of 1000, but arguably my scripting style is far too defensive to be optimal. Having had to administrate abour 20k servers worldwide once did that for me: every conceivable nightmare was sure to be encountered on at least half a dozen systems and one gets to take absolutely nothing for granted.

How about this, using shell methods:

Code:
(( iCnt = 1 ))
string="0,0,0,0,0,3,0,0,1,0," # [....]
buf=""
rest="$string"

while [ -n "$rest" ] ; do
     rest="${string#[0-9]*,}"
     buf="${string%,$rest}"

     if (( $buf )) ; then
          echo "Element $iCnt is $buf"
     fi

     (( iCnt = iCnt + 1 ))
     string="$rest"
done


I hope this helps.

bakunin

Last edited by bakunin; 08-04-2016 at 03:03 PM.. Reason: corrected typo
This User Gave Thanks to bakunin For This Post:
# 7  
Old 08-04-2016
Well I know my version of bash can do 192000 elements as I have used it in the past:-
(Whistling into the built-in microphone...)
Code:
#!/bin/bash
# arraylimit.sh
SECS=1
"$HOME"/sox-14.4.2/sox -q -V0 -d -t raw -r 96000 -b 8 -c 2 -e unsigned-integer -> /tmp/rawfile trim 0 00:"$SECS"
arraylimit=( $( hexdump -v -e '1/1 "%u "' /tmp/rawfile ) )
echo "${#arraylimit[@]}"
echo "${arraylimit[191999]}"

Results...
Code:
Last login: Thu Aug  4 16:53:26 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin15)
Copyright (C) 2007 Free Software Foundation, Inc.
AMIGA:barrywalker~/Desktop/Code/Shell> ./arraylimit.sh
192000
121
AMIGA:barrywalker~/Desktop/Code/Shell> _

IIRC I have had an array with double that, (384000 elements), for another reason.

Last edited by wisecracker; 08-04-2016 at 01:16 PM.. Reason: Simplify SOX.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Vlookup using awk without exact match for two colum input

Source file 1 335 R1-snapfound 0098F RDFType:R1 R2-Dev R2-snapfound ,010C0 RemoteSymmetrixID:345 335 R1-snapfound 00990 RDFType:R1 R2-Dev R2-snapfound ,010C1 RemoteSymmetrixID:345 335 R1-snapfound 009C0 RDFType:R1 R2-Dev R2-snapfound ,009C1 RemoteSymmetrixID:345 335 R1-snapfound 009C1... (5 Replies)
Discussion started by: ranjancom2000
5 Replies

2. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

3. Shell Programming and Scripting

Script by row colum

Hi All, how can output file1 to file2 by colum, e.g. $cat file1 1 2 3 $cat file2 aaa bbb ccc ddd eee fff ggg hhh iii then i need new file1 (4 Replies)
Discussion started by: aav1307
4 Replies

4. Shell Programming and Scripting

Sorting based on a particular colum

Hi, I want a flat file(pipe delimited) to be sorted based on 2nd column only. Below is input file to be sorted. AVERS|K50034|A|Y|N|N|Y|Y|Y|||N|N AVERS|K50035|A|Y|N|N|Y|Y|Y|||N|N... (11 Replies)
Discussion started by: Nikhath
11 Replies

5. Linux

Doubt on vmstat swpd colum

Dear Gurus, When i ran vmstat, i am getting swpd value,but si, and so beneath the swap is o, My doubt what is the difference between these values,i,e swpd and si and so OS is OEL 5.5 procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------ r b swpd free buff cache... (2 Replies)
Discussion started by: dave234
2 Replies

6. UNIX for Dummies Questions & Answers

joining files with different rows and colum

Hi all, I have two files: file one (9 rows, 3 columns): A 1 x1 B 2 f1 C 3 f3 D 4 u5 E 5 l9 F 6 h6 G 7 g4 H 8 r4 I 9 s2 file two (4 rows, 1 column): A B (2 Replies)
Discussion started by: anjas
2 Replies

7. Shell Programming and Scripting

sed command to delete points in a certain colum

Dear Unix users, I have a (I think simple) question; I have a file (out.dat) like below, the file contains some line which include 'LOS' string. . LOS 46.5360 91.0220 200708.2515.4900. 5400 64 1100010 . . I would like to delete the points in 4th... (4 Replies)
Discussion started by: mgunduz
4 Replies

8. Shell Programming and Scripting

find lines have 2nd colum value greater than 40

i have a file a.txt 12345,20 34567,10 23123,50 123456,45 how to find lines which hav 2nd entry greater than 40 o/p 23123,50 123456,45 pls help to get o/p (5 Replies)
Discussion started by: devesh123
5 Replies

9. Shell Programming and Scripting

add multiple colum using awk

hi i need help in my script i have a file a.txt 3,4,5,13 6,7,8,45 9,0,1,67 i want to add 2nd and 3rd colum like o/p will be by adding all values of colum2(4+7+0) and colum 3(5+8+11) o/p: colum 2: 11 colum 3: 14 (6 Replies)
Discussion started by: anish19
6 Replies

10. Shell Programming and Scripting

sed or awk to convert text files with recurring headings to rows and colum

I have many text file reports generated by a Information Assurance tool that I need to get into a .CSV format or Excel tab delimited format. I want to use sed or awk to grab all the information in the sample text file below and create column headings:Risk ID, Risk Level, Category, Description, How... (5 Replies)
Discussion started by: Bjoeboo
5 Replies
Login or Register to Ask a Question