Finding multiple zero's in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding multiple zero's in a file
# 1  
Old 10-11-2010
Finding multiple zero's in a file

Hi all,

i have a text file like the below example---

Code:
  146      7600     147    23996
43024  50700581   28998  1767165
   10      3784      12     1344
   0          0         0         545
641    166646     723    90136
   24      1046      46     2948

OR

Code:
4340  17659287   12632   994356
 281    218255     262    22841
   0   2272753    1452    84955
   0         0       0        0
   0     4121      17     1680
  63     21011      67     7947




i want to check this file for multiple zeros in between and echo "too many zeros" .Thanxx in advance

Last edited by radoulov; 10-11-2010 at 07:14 AM.. Reason: Code tags, please!
# 2  
Old 10-11-2010
How many is "too many" ?

Do you want to match when every field in a row is a zero ? or when 3 of 4 are ?

Does your input always have 4 fields ?
# 3  
Old 10-11-2010
Something complete, can be simplified (suppose your files are file1.txt and file2.txt)
bash code:
  1. #!/bin/bash
  2. for F in file1.txt file2.txt
  3. do
  4.    while read -a L
  5.    do
  6.       ((N++))
  7.       Z=0
  8.       for ((i=0; i<${#L[@]}; i++))
  9.       do
  10.          if &#91; ${L[$i]} -eq 0 ]
  11.          then
  12.             ((Z++))
  13.             ((Z>1)) && { echo "Too many zeroes in file $F line $N : ${L[*]}"; break; }
  14.          fi
  15.       done
  16.    done <$F
  17.    N=0
  18. done

Last edited by frans; 10-11-2010 at 09:10 AM..
# 4  
Old 10-11-2010
a liitle addition Smilie

Quote:
Originally Posted by frans
Something complete, can be simplified (suppose your files are file1.txt and file2.txt)

....
....
done <$F
N=0
done
This User Gave Thanks to ygemici For This Post:
# 5  
Old 10-11-2010
Quote:
Originally Posted by ygemici
a liitle addition Smilie
Ooops !
I edit my post !!
# 6  
Old 10-11-2010
Quote:
Originally Posted by frans
Ooops !
I edit my post !!
Smilie

Another one Smilie

Code:
# ./justdoit
Too many(->3) zeroes in file "file1.txt" line 4 -->      0          0         0         545
 
Too many(->4) zeroes in file "file2.txt" line 4 -->      0         0       0        0
Too many(->4) zeroes in file "file2.txt" line 5 -->      0         0       0        0

Code:
## justdoit ##
#!/bin/bash
for file in file1.txt file2.txt
 do
  c=1
  while read -r l
   do
    if [[ $( echo "$l" | sed 's/[^ ]0[^ ]\|[^0 ]\|[^ 0]\|[^ ]0\|0[^ ]//g' | wc -w ) -gt 1 ]] ; then
     echo -e "Too many(->$(echo "$l" | sed 's/[^ ]0[^ ]\|[^0 ]\|[^ 0]\|[^ ]0\|0[^ ]//g' | wc -w)) zeroes in file $file line $c --> \t $l "
    fi
    ((c++))
   done <$file
   echo -e "\nProcessing Other File.."
   c=1
 done

# 7  
Old 10-11-2010
Anyone for the awk way ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding the right file with multiple sort criteria

Hello, I have files in a directory with names like, ./f0/84.40_E1200_85.39_E1300_f0_r00_1300-ON-0.25_S7A_v4_47.19.1.out.txt ./f0/84.40_E1200_85.83_E1200_f0_r00_1200-ON-0.25_S7A_v4_47.19.1.out.txt ./f0/84.60_E1100_86.45_E1100_f0_r00_1100-ON-0.25_S7A_v4_47.19.1.out.txt... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

2. Shell Programming and Scripting

finding matches between multiple files from different directories

Hi all... Can somebody pls help me with this... I have a directory (dir1) which has many subdirectories(vr001,vr002,vr003..) with each subdir containing similar text file(say ras.txt). I have another directory(dir2) which has again got some subdir(vr001c,vr002c,vr003c..) with each subdir... (0 Replies)
Discussion started by: bramya07
0 Replies

3. Shell Programming and Scripting

Finding strings through multiple lines

Hi, I need to search for a multiple line pattern and remove it the pattern is search for (ln number) <TABLE name=*> and if 3 lines below that the line is (ln number) </TABLE> Then remove those 4 lines. Thank you (14 Replies)
Discussion started by: legolad
14 Replies

4. UNIX for Dummies Questions & Answers

Finding nth line across multiple files

I have several files (around 50) that have the similar format. I need to extract the 5th line from every file and output that into a text file. So far, I have been able to figure out how to do it for a single file: $ awk 'NR==5' text1.txt > results.txt OR $ sed -n '5p' text1.txt > results.txt... (6 Replies)
Discussion started by: oriqin
6 Replies

5. Shell Programming and Scripting

Finding multiple column values and match in a fixed length file

Hi, I have a fixed length file where I need to verify the values of 3 different fields, where each field will have a different value. How can I do that in a single step. (6 Replies)
Discussion started by: naveen_sangam
6 Replies

6. Shell Programming and Scripting

multiple regex finding in files

Hello folks, I have a text file aa.txt that contains below text (\')|(\-\-) ((\%3D)|(=)) 20%0d% i want to search each line pattern in /opt/1.log and /opt/2.log. Can some one suggest (1 Reply)
Discussion started by: learnbash
1 Replies

7. UNIX for Dummies Questions & Answers

Finding names in multiple files - second attempt

I couldn't find the original thread that I created and since I didn't get a definitive answer, I figured I'd try again. Maybe this time I can describe what I want a little better. I've got two files, each with thousands of names all separated by new line. I want to know if 'name in file1'... (2 Replies)
Discussion started by: Rally_Point
2 Replies

8. UNIX for Dummies Questions & Answers

Finding Names in multiple files

What's the best way to see if a common name exists in two separate files? (3 Replies)
Discussion started by: Rally_Point
3 Replies

9. Shell Programming and Scripting

Expression for Finding Multiple Directories..??

I am writing a shell script to search for previous versions of an application...the application is called TAU and basically i want to search the users home directory and /Applications for any instances of a "TAU" folder.. If found i want to give the user the option to remove the old folders and if... (3 Replies)
Discussion started by: meskue
3 Replies

10. UNIX for Dummies Questions & Answers

finding multiple file types with "-o"

i was just wondering if any one had a good example of finding mutliple file types with the -o option or any other alternatives. find . \( -name "*.txt" -o -name "*.tag" \) for some reason i'm not having much luck and the man page isn't very descriptive. what i am trying to do is find all... (6 Replies)
Discussion started by: Shakey21
6 Replies
Login or Register to Ask a Question