Shell script to count number of ~ from each line and compare with next line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to count number of ~ from each line and compare with next line
# 1  
Old 09-28-2010
MySQL Shell script to count number of ~ from each line and compare with next line

Hi,
I have created one shell script in which it will count number of "~" tilda charactors from each line of the file.But the problem is that i need to count each line count individually, that means. if line one contains 14 "~"s and line two contains 15 "~"s then it should give an error msg.each line having same number of "~"s. so i need to check each line is having same "~" or not . my script gives me output like
**output**
Code:
total =        2
linelength=       62
14
linelength=       62
28

my script
Code:
#!/bin/bash
DATA_DIR=/export/opt/rtrupld/autosys/scripts_old
data=`ls $DATA_DIR/count.txt`
#     count1=1
var=`wc -l < $data`
#var2=`expr $var + 1`
echo "total = $var"
#var3=14
tildacount=0
while read line
do
   count=0
   linelen=`echo "$line"|wc -c`
 
echo "linelength= $linelen"
   if [ $linelen -gt 0 ]
     then 
# echo $linelen
       until [ $count -ge $linelen ]
     do
         count=`expr  $count + 1 `
         char=`echo "$line"|cut -b"$count"`
         if [ "$char" = "~" ]
         then
            tildacount=`expr  $tildacount + 1`
 
     fi
 
     done
     echo $tildacount
  fi
       
      
 
done < $data
#echo $tildacount

----Actually Outpur Required

Code:
if line 1 have number of ~=14
line 2  = 14
the correct file else
if line1 =14
line2= 15

then should give an error msg like ~ count is not same as prevoius line.


Thanks & Regards
Ganesh

Moderator's Comments:
Mod Comment Please use code tags for scripts and other listings.

Last edited by pludi; 09-28-2010 at 06:07 PM.. Reason: added some additional things for better understanding
# 2  
Old 09-28-2010
Code:
awk ' {         sum=0; 
        for(i=1; i<= length($0); i++) {if (substr($0,i,1)=="~") {sum++} }
        if(FNR==1) {oldsum=sum}
        else
        {
           if(oldsum!=sum) 
              {print "error in ", FILENAME, " at line ", FNR, " tilde count=",sum}
        }  
      }' filename

# 3  
Old 09-29-2010
MySQL Need something more in this script

Hi Jim,
Thanks your script is working fine.But i have one more problem like. your script every time goes into else loop. eve if oldsum is not eqal to sum then also its giving an error in count. i am getting result as below.

output----
Code:
error in  INTELLECT~EUR~EOD~40140~2010-09-21 00:19:20~20100920~~~~~~~~~  at line    tilde count= 14 
error in  INTELLECT~EUR~EOD~40140~2010-09-21 00:19:20~20100920~~~~~~~~~  at line    tilde count= 14

I need to cout number od tilda and if the count is not equal then it should give an error count are not equal. e.g
Code:
line 1 count= 14
line 2 count= 15

then it should give error msg like line 1 and 2 are not same also i dont want line to be print only count need.
also what is FNR i didnt get it. I am weak in awk script.
could you please reply me if possible. My Boss is shouting on me how much time you are taking to solve this problem.

Thanks & Regards,
Ganesh

Last edited by Scott; 10-04-2010 at 11:24 AM.. Reason: Code tags
# 4  
Old 09-29-2010
Try that.
Code:
awk ' {         sum=0; 
        for(i=1; i<= length($0); i++) {if (substr($0,i,1)=="~") {sum++} }
        printf( "line %d tilde count=%d ", FNR, sum)
        if (FNR==1){ oldsum=sum; next}
        if(oldsum != sum) {print " error"; errcnt++ }
        else { print "" }
        END { print "total errors", errcnt }
      }' filename > testfile

It sounds like you are in a job where you need to pick a skillset fast.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get the line number in shell script

I have one text file 1 2 3 a 5 4 4 3 where i want to print the line number while read line do line_no=`awk '{print NR, $0}'` echo 'In line no $line_no' done <$txt_file If i run the above code, it will print 'In line no 1 1 2 3' It prints the line number with the whole... (3 Replies)
Discussion started by: RJG
3 Replies

2. Shell Programming and Scripting

Count the pipes "|" in line and delete line if count greter then number.

Hello, I have been working on Awk/sed one liner which counts the number of occurrences of '|' in pipe separated lines of file and delete the line from files if count exceeds "17". i.e need to get records having exact 17 pipe separated fields(no more or less) currently i have below : awk... (1 Reply)
Discussion started by: ketanraut
1 Replies

3. Shell Programming and Scripting

Compare file1 header count with file2 line count

What I'm trying to accomplish. I receive a Header and Detail file for daily processing. The detail file comes first which holds data, the header is a receipt of the detail file and has the detail files record count. Before processing the detail file I would like to put a wrapper around another... (4 Replies)
Discussion started by: pone2332
4 Replies

4. Shell Programming and Scripting

Want to count the number of lines after the first line

hi, How can i count the number of lines after the first line in a flat file in unix? Say i have a flat file with a header like: Student Name Student ID .... Tnx (7 Replies)
Discussion started by: reignangel2003
7 Replies

5. Shell Programming and Scripting

Count number of occurrence at each line

Hi I have the following file ENST001 ENST002 4 4 4 88 9 9 ENST004 3 3 3 99 8 8 ENST009 ENST010 ENST006 8 8 8 77 8 8 Basically I want to count how many times ENST* is repeated in each line so the expected results is 2 1 3 Any suggestion please ? (4 Replies)
Discussion started by: fuad_
4 Replies

6. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

7. Shell Programming and Scripting

How to read from specific line number in shell script?

I have a text file which is having 30000 lines in it. I have to create a xml file for each 10000 lines until all the lines in the text files are written. Also please help how can i get number of lines in the text file in a shell variable? (19 Replies)
Discussion started by: vel4ever
19 Replies

8. Shell Programming and Scripting

[ask]how to compare line number

dear all, i need help about this case i have line number extract in file fileA.txt like this: 231 123 444 231 213 ... now i want to compare the line number fileA.txt to find other file in fileB.txt exp: if i use one by one like this: cat fileB.txt | sed -n -e "444"p huff...is not... (3 Replies)
Discussion started by: zvtral
3 Replies

9. Shell Programming and Scripting

current line number in shell script

Hi I am using korn shell is there a built in or ny other way to get the current line number so i can write the current line number to the logfile to aid in debugging like logmsg $lineno $date logmsg is a helper function that helps in logging messages to a log file regards (3 Replies)
Discussion started by: xiamin
3 Replies

10. Shell Programming and Scripting

Compare number of line

Hi expert, I using csh and i trying to compare no. of line of the file. Example: I have 2 files. file1 ONE TWO THREE FOUR FIVE file 2 ONE TWO THREE FOUR file1 have 5 lines but file2 have 4 lines. (1 Reply)
Discussion started by: vincyoxy
1 Replies
Login or Register to Ask a Question