Checking a single digit in a file using Grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking a single digit in a file using Grep
# 1  
Old 06-29-2016
Checking a single digit in a file using Grep

Hi All,

I have a file which keeps count based on completion of a certain activity. I am using the following grep command to return a '1' in case the count is zero

Code:
grep -ic "0" abc_count.txt

Now the issue happens when the count is '10', '20' etc .. in these cases as well it returns a '1' because it sees a zero at the end of the count ..
Kindly, help me, how can I get a '1' using grep only when there is a zero count in abc_count.


Thanks
# 2  
Old 06-29-2016
I have no idea what do you intent to put in the file but if it is plain number only you probably want to check if the line begins with 0 or not.

Code:
grep -ic "^0" abc_count.txt

# 3  
Old 06-29-2016
Not sure if grep is the right tool for this request. Try to play with the -w and -o options. As you are dealing with numbers only, the -i option is pointless.
# 4  
Old 06-29-2016
Quote:
Originally Posted by dev.devil.1983
Hi All,

I have a file which keeps count based on completion of a certain activity. I am using the following grep command to return a '1' in case the count is zero

Code:
grep -ic "0" abc_count.txt

Now the issue happens when the count is '10', '20' etc .. in these cases as well it returns a '1' because it sees a zero at the end of the count ..
Kindly, help me, how can I get a '1' using grep only when there is a zero count in abc_count.


Thanks
Please, try the following and see if your grep has the support for it:

Code:
grep "\<0\>" abc_count.txt

If you only see the expected results, then you can add the -c flag to return the count. The -i is not necessary because 0 will not make a difference for case insensitivity
# 5  
Old 06-29-2016
If the file consists on a single line and that line only contains the number and the line-terminating <newline> character, a simple way to test would be:
Code:
grep -Fx 0 file

or for ancient versions of grep that don't have a -F option:
Code:
fgrep -x 0 file

# 6  
Old 06-30-2016
The -i (case insensitive search) is slower and does not make sense when searching for 0.
The left/right boundaries in grep "\<0\>" are not known in HP-UX. grep -w 0 seems more portable.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

convert two digit in to single digit...

Hi Guys. My Input: ABCD 12 00 KL ABCD 12 08 DL ABCD 12 10 KK ABCD 12 04 LL ABCD 13 00 LP ABCD 13 1O LS Output: ABCD 12 0 KL ABCD 12 8 DL ABCD 12 10 KK ABCD 12 4 LL ABCD 13 0 LP (2 Replies)
Discussion started by: pareshkp
2 Replies

2. Shell Programming and Scripting

how to delete the line if the first letter is a single digit

Hi, I'm trying to acheive the following, I have a dat file in which i have several addresses, If the address starts with a single digit then i have to delete the line, if it starts with 2 or more digits then i have to keep the line Here is a sample of my file: 377 CARRER DE LA... (5 Replies)
Discussion started by: ramky79
5 Replies

3. Shell Programming and Scripting

Grep multiple words in a single file

Hello All, I'm a newbie/rookie in Shell scipting. I've done oracle export of a table using Export utility. When I do export, it generates 2 files. 1> .dmp file 2> .dmp.log file. In .dmp.log file I have to search for a sentence which goes like '0 records have been inserted' and then... (2 Replies)
Discussion started by: samfisher
2 Replies

4. Shell Programming and Scripting

how to add single digit in front of the word and line in the file.

Hi , how to add the single digit to front of the word and front of the lines in the one file with compare pattern file and get digit. like example pattern file pattern.txt pattern num bala 2 raja 3 muthu 4 File Name: chennai.dat muthu is good boy raja is bad boy selvam in super... (6 Replies)
Discussion started by: krbala1985
6 Replies

5. UNIX for Dummies Questions & Answers

list all files containing 4 digit number using grep

how can i list all files in my home directory that have a 4 digit id number, the line number where the id is located and the id itself not printing the entire line? (5 Replies)
Discussion started by: hobiwhenuknowme
5 Replies

6. Shell Programming and Scripting

Single digit date to double digit date.

I have a var storing date var=`date` Now the date is returned as Mon Feb 2 00:25:48 PST 2009 Is there any way to check the date field alone ("2" in above case) and if its a single digit then add a prefix 0 to it and store the result in same variable "var" My intention in above case is... (3 Replies)
Discussion started by: villain41
3 Replies

7. Shell Programming and Scripting

month in single digit

hi all, how do i get the month in single digit in ksh script ?? my command for date is : /usr/bin/date +%Om/%Oe/%y | sed 's/ //g' which returns "01/16/09" but i need the output to be "1/16/09" i.e the month without leading zero. thanks in advance. (2 Replies)
Discussion started by: cesarNZ
2 Replies

8. Shell Programming and Scripting

single digit for day and month on date

hi all, how do i format the date command so it displays day and month in single digits i.e 8 instead of 08 ?? am using the command (in a ksh) : date +%D output i get is 10/08/08 thanks in advance. (5 Replies)
Discussion started by: cesarNZ
5 Replies

9. UNIX for Dummies Questions & Answers

Append 0 for single digit entered from command line

I have a script like this-- #!/bin/ksh echo "To pad a 0 before digits from 1-9" for i in $* do echo $i | sed 's//'0'/g' done I run this script as ksh name 1 2 23 34 The output should be 01 02 23 34 Help me in modifying this script. Thanks Namish (2 Replies)
Discussion started by: namishtiwari
2 Replies

10. Shell Programming and Scripting

How to print all lines that has exactly 3 occurences of digit 4 using grep?

How to print all lines that has exactly 3 occurences of digit 4 using grep? I am able to find the pattern if it is for atleast 3 occurences but with exactly 3 occurences? Can anybody help me on this. (3 Replies)
Discussion started by: sivasai83
3 Replies
Login or Register to Ask a Question