Counting the number of characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counting the number of characters
# 1  
Old 02-20-2012
Counting the number of characters

Hi all,

Can someone help me in getting the following o/p

I/p:
1157,306413,R,2009,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,,0,0,0,0,0,0,0,0,0,5,405,0,0,102,102,102,102,102,102,102,103,102,102,102 ,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102 ,102,102,102,,102,102,102,102,102,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

O/p:
1157,306413,R,2009,113,

The First four fields remains the same , after that come the number of Zero or Null characters before the first non-zero or non-null charqacter , in the example given above it is "5" and the count is 113.

Would be really helpful if some one can help me by giving a statement (preferably awk )for this.

Many Thanks in advance
Sri


# 2  
Old 02-20-2012
Try:
Code:
awk -F, '{for(i=5;!$i>0 && i<=NF;i++);NF=5;$5=i-5}1' OFS=, infile

# 3  
Old 02-20-2012
Thank you Scrutinizer,

Can you please explain me the logic , because this is just one step of my requirement i need to do some more steps in this , I would need the o/p of that 5th Coloumn , My final o/p should look something like this

I/p:
1157,306413,R,2009,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,,0,0,0,0,0,0,0,0,0,5,405,0,0,102,102,102,102,102,102,102,103,102,102,102 ,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102 ,102,102,102,,102,102,102,102,102,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

O/p:
1157,306413,R,2009,113,5,405,0,0,102,102,102,102,102,102,102,103,102,102,102,102,102,102,102,102,102 ,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,,102,102,10 2,102,102,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,0,0,0,0,0,0,0,0,0

I need to remove the zeros or null before the non zero character and append it at the end so that my coloumn count remains the same

Many thanks in advance

# 4  
Old 02-20-2012
You means like this?
Code:
awk -F, '{s=x;for(i=5;!($i>0) && i<=NF;i++)s=s OFS $i;sub(s,x);$0=$0s}1' OFS=, infile

-F,Use comma as input field separator
s=xmake s equal to "" (the same value as x, which is initialized)
for(i=5;!($i>0) && i<=NF;i++)s=s OFS $iadd each field together with the output field separator (OFS) to the string s that does not contain a positive value (and stop if the last field is reached)
sub(s,x)replace all those null fields contained in variable s with an empty in the record ($0)
$0=$0spaste them at the end of the record ($0)
1print the record
OFS=,Use comma as output field separator
# 5  
Old 02-20-2012
Sorry,

That is not giving the count of zeros and nulls that was coming before the non null value,

Can you please give me a statement to delete these fields , probably we can append it as the last step of the script
# 6  
Old 02-20-2012
Just combine the two
Code:
awk -F, '{s=x;for(i=5;!($i>0) && i<=NF;i++)s=s OFS $i;sub(s,OFS i-5);$0=$0s}1' OFS=, infile

What had you tried yourself?

Last edited by Scrutinizer; 02-20-2012 at 09:30 AM..
# 7  
Old 02-20-2012
It is not working ,

I was trying to get the o/p of the first command you have given save it as a file , Then remove the coloumns between the 6th coloumn and the coloumn we get from the 5th coloumn , then append this to the last of the file.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Counting characters at each position

Hi All, here's a question from newbie I have a data like this, which set of small DNA sequences separated by new line GAATCCGGAAACAGCAACTTCAAANCA GTNATTCGGGCCAAACTGTCGAA TTNGGCAACTGTTAGAGCTCATGCGACA CCTGCTAAACGAGTTCGAGTTGAANGA TTNCGGAAGTGGTCGCTGGCACGG ACNTGCATGTACGGAGTGACGAAACCI... (6 Replies)
Discussion started by: amits22
6 Replies

2. Shell Programming and Scripting

Counting characters vertically

I do have a big file in the following format >A1 ATGCGG >A2 TCATGC >A3 -TGCTG The number of characters will be same under each subheader and only possible characters are A,T,G,C and - I want to count the number of A's, T's,G's, C's & -'s vertically for all the positions so that I... (5 Replies)
Discussion started by: Lucky Ali
5 Replies

3. Shell Programming and Scripting

counting the number of characters in the filename of all files in a directory?

I am trying to display the output of ls and also print the number of characters in EVERY file name. This is what I have so far: #!/bin/sh for x in `ls`; do echo The number of characters in x | wc -m done Any help appreciated (1 Reply)
Discussion started by: LinuxNubBrah
1 Replies

4. Shell Programming and Scripting

Counting characters with sed

Input: ghw//yw/hw///??u How can i count the slashes("/") using sed? (13 Replies)
Discussion started by: cola
13 Replies

5. Shell Programming and Scripting

counting characters

Hi All, I need some help in counting the number of letters in a big file with separations. Following is the file I have >AB_1 MLKKPIIIGVTGGSGGGKTSVSRAILDSFPNARIAMIQHDSYYKDQSHMSFEERVKTNYDHPLAFDTDFM IQQLKELLAGRPVDIPIYDYKKHTRSNTTFRQDPQDVIIVEGILVLEDERLRDLMDIKLFVDTDDDIRII... (6 Replies)
Discussion started by: Lucky Ali
6 Replies

6. Shell Programming and Scripting

Counting the number of occurances of all characters (a-z) in a string

Hi, I am trying out different scripts in PERL. I want to take a line/string as an input from the user and count the number of occurrances of all the alphabets (a..z) in the string. I tried doingit like this : #! /opt/exp/bin/perl print "Enter a string or line : "; $string = <STDIN>; chop... (5 Replies)
Discussion started by: rsendhilmani
5 Replies

7. UNIX for Dummies Questions & Answers

counting the occurence of particular characters

I want to list the occurence of particular characters in a line. my file looks like this a,b,c,d e,f,g h,y:e,g,y s f;g,s,w and I want to count how many commas are in each line so the file in the end looks like this: a,b,c,d 3 e,f,g 2 h,y:e,g,y s 3 f;g,s,w ... (2 Replies)
Discussion started by: Audra
2 Replies

8. Shell Programming and Scripting

Counting characters between comma's

I have a comma delimited file that roughly has 300 fields. Not all fields are populated. This file is fed into another system, what I need to do is count the amount of characters in each field and give me an output similiar to this: 1 - 6,2 - 25 The first number is the field and the second... (2 Replies)
Discussion started by: dbrundrett
2 Replies

9. Shell Programming and Scripting

counting characters

Dears, I would like to count the number of "(" and ")" that occur in a file. (syntax checking script). I tried to use "grep -c" and this works fine as long as there is only one character (for which I do a search) on a line. Has anyone an idea how I can count the number of specific characters... (6 Replies)
Discussion started by: plelie2
6 Replies
Login or Register to Ask a Question