Replace a null from grep to a number 0 using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a null from grep to a number 0 using sed
# 1  
Old 08-24-2010
Replace a null from grep to a number 0 using sed

Hi
I have a file which contains count for a code. Code is first field and count is second field.
I am trying to search the code and get correspond count.

File look like this. temp.out
Code:
A 10
B  20

I am searching for C , if C is not there I will have get value 0.

I have written like this to get the count.

Code:
c=`grep "C " temp.out | awk '{print $2}'`

But this c is getting a null. How do I change it so that I will get zero instead of null.

Thanks and Regards
dgmm

Moderator's Comments:
Mod Comment Use code tags please.

Last edited by zaxxon; 08-24-2010 at 04:21 AM..
# 2  
Old 08-24-2010
Code:
c=`grep "^C " temp.out || echo 0`

# 3  
Old 08-24-2010
Thanks for the reply. I understand if it does not find , I will replace with Zero.
But if the C is present , it has to print from the file for second field.
The above solution does not work.
I tried both .
Code:
c=`grep "C " temp.out | awk '{print $2}' | sed "s/^[0-9]/'0'/g"`

Code:
c=`grep "C " temp.out | awk '{print $2}' | sed "s/' '/'0'/g"`

But above does not work.
# 4  
Old 08-24-2010
Try this,

Code:
c=`grep "D" temp.out`; c=`[ -n "$c" ] && echo "$c" | awk '{print $2}' || echo "0"`;echo $c


Last edited by pravin27; 08-24-2010 at 04:54 AM..
# 5  
Old 08-24-2010
Oops here you go:
Code:
c=`awk '/^C / {print $2; i++} END{if(i==0)print "0"}' temp.out`

# 6  
Old 08-24-2010
Hi
It is working now. Both are good solution.

Thank you very much.


Smilie

---------- Post updated at 03:41 PM ---------- Previous update was at 03:09 AM ----------

Hi
I am just modifying the my question little bit.
I have data like this
type code count
010 A 20
005 C 30
005 A 10

in this case I am looking for count for each type.
for 010 I do this.
Code:
a=`grep "010" temp.out | grep "A " | awk '{print $3}'`

but if it does not find , It returns null.
Can you please correct to get 0 if it does not find.

Last edited by dgmm; 08-24-2010 at 05:19 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using sed to replace a range of number

Trying to use SED to replace numbers that fall into a range but can't seem to get the logic to work and am wondering if SED will do this. I have a file with the following numbers 3 26 20 5. For the numbers that are greater than zero and less than 25, SED would add the word range after the... (7 Replies)
Discussion started by: jimmyf
7 Replies

2. Shell Programming and Scripting

sed to replace the matching pattern with equal number of spaces

Hi I have written a shell script which used sed code below sed -i 's/'"$Pattern"'/ /g' $FileName I want to count the length of Pattern and replace it with equal number of spaces in the FileName. I have used $(#pattern) to get the length but could not understand how to replace... (8 Replies)
Discussion started by: rakeshkumar
8 Replies

3. Shell Programming and Scripting

Replace a pattern in a file with a generated number using sed or awk

my file has thousands of line but let me show what i want to achieve... here is one line from that file cat fileName.txt (2,'','user3002,user3003','USER_DATA_SINGLE',1,0,0,'BACKUP',2,NULL,0,450,NULL,NULL,'','2011-05-10... (13 Replies)
Discussion started by: vivek d r
13 Replies

4. Shell Programming and Scripting

How to replace null data using SED

Hi, I have following data in a file 5~6.14~S~N~N~0.~4565~134~6584  ~6.13~H~N~N~0.~4578~0~6587 2~6.14~S~N~N~0.~4565~134~6584  ~3.13~H~N~N~0.~4578~0~6587 -~6.14~S~N~N~0.~4565~134~6584  ~7.13~H~N~N~0.~4578~0~6587 I want the output as 5~6.14~S~N~N~0.~4565~134~6584... (2 Replies)
Discussion started by: sol_nov
2 Replies

5. Shell Programming and Scripting

Replace x Number of String Occurrence with Sed

Ok, So I have a huge file that has over 12000 lines in it. in this file, there are 589 occurrences of the string "use five-minute-interval" spread in various areas in the file. How can i replace the the last 250 of the occurrences of "use five-minute-interval" with "use... (10 Replies)
Discussion started by: SkySmart
10 Replies

6. Shell Programming and Scripting

Using SED to replace a random number?

Hi all, I need to run a number of scripts which have a certain phrase on them so I have identified these by; grep -l 100 *script | sort -u Normally I could just run something along the lines of; for i in `grep -l 100 *script | sort -u`; do ./${i}; done However before I run each of... (0 Replies)
Discussion started by: JayC89
0 Replies

7. Shell Programming and Scripting

Find and replace a column that has '' to NULL in a comma delimited using awk or sed

Hi this is my first time posting ever. I'm relatively new in using AWK/SED, I've been trying many a solution. I'm trying to replace the 59th column in a file where if I encounter '' then I would like to replace it with the word NULL. example 0 , '' , '' , 0 , 195.538462 change it to 0... (5 Replies)
Discussion started by: gumal901
5 Replies

8. UNIX for Dummies Questions & Answers

Sed to replace second number in a random string

I need a sed line that will take STDM111 and change it to STDM161 the STDM will always be constant but the 3 numbers after will be random, I just need it to always replace the middle number with 6 regardless of what the numbers are. (8 Replies)
Discussion started by: glev2005
8 Replies

9. UNIX for Dummies Questions & Answers

sed/grep string replace question

Hi all, I know this question has probably been answered before, but I am struggling with this problem, even after googling a million pages. In a file named rdmt.conf I need a single character replaced, the number in the line below CUR_OC4J_ID=1 It will always appear after... (3 Replies)
Discussion started by: Mike AAA
3 Replies

10. Shell Programming and Scripting

Sed command to find, manipulate and replace a number

Hi, Im very new to the world of sed so I'm really not even sure if this is possible. What i need to do is read from a flat file and every time i see this line: VAL=123,456 I need to change 456 to 457 for every occurence of this line in the file. The numbers 123 and 456 are different for... (6 Replies)
Discussion started by: LT_2008
6 Replies
Login or Register to Ask a Question