If grep value is null then assign 0 to variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If grep value is null then assign 0 to variable
# 8  
Old 09-18-2014
The following works with all Posix shells
Code:
ins=0
del=0
upd=0
a_i=0
set -f
while read line
do
  set -- $line
  eval lastA=\$$#
  case `echo "$line" | tr '[:lower:]' '[:upper:]'` in
  INSERT*) ((ins=ins+lastA));;
  DELETE*) ((del=del+lastA));;
  UPDATE*) ((upd=upd+lastA));;
  "AFTER IMAGES"*) ((a_i=a_i+lastA));;
  esac
done <"$logfile1"
set +f
echo "INSERT=$ins"
echo "DELETE=$del"
echo "UPDATE=$upd"
echo "AFTER_IMAGES=$a_i"

# 9  
Old 09-18-2014
Quote:
Originally Posted by MadeInGermany
The following works with all Posix shells
Code:
ins=0
del=0
upd=0
a_i=0
set -f
while read line
do
  set -- $line
  eval lastA=\$$#
  case `echo "$line" | tr '[:lower:]' '[:upper:]'` in
  INSERT*) ((ins=ins+lastA));;
  DELETE*) ((del=del+lastA));;
  UPDATE*) ((upd=upd+lastA));;
  "AFTER IMAGES"*) ((a_i=a_i+lastA));;
  esac
done <"$logfile1"
set +f
echo "INSERT=$ins"
echo "DELETE=$del"
echo "UPDATE=$upd"
echo "AFTER_IMAGES=$a_i"

Actually, you need some extensions to POSIX requirements to process the above script. The current standard specifies the behavior of $((expression)), but not the behavior of ((expression)). (ksh88 does not support ((expression)).)

To be portable to any POSIX shell, lines like:
Code:
  INSERT*) ((ins=ins+lastA));;

would have to change to something like:
Code:
  INSERT*) ins=$((ins+lastA));;

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. UNIX for Beginners Questions & Answers

Need to pass variable in a command and assign value to a variable

Hello All, Hope you're doing well ! I am trying below command to be passed in a shell script, header_date_14 is a variable and $1 is the name of a file I intend to pass as a command line argument, however command line argument is not being accepted. header_date_14=$(m_dump... (8 Replies)
Discussion started by: ektubbe
8 Replies

3. Shell Programming and Scripting

Assign grep match to variable question

Hi, I'm trying to assign a grep result to a variable but instead of having all grep result's assigned to the variable, would it be possible to assign the first match, do something, then move onto the next match and assign it to that variable and so on until all matches have been completed I... (4 Replies)
Discussion started by: Jazmania
4 Replies

4. Shell Programming and Scripting

Question on NULL and zero value of variable

Hi all, I have a stupid question on NULL and zero(0). In a script I've been working with, one of the lines is: if && then The problem I seem to have is when $Current_csm2 is null, this if block is not triggered, and I don't get why because I was under the impression that NULL!=0 Can... (7 Replies)
Discussion started by: spynappels
7 Replies

5. Shell Programming and Scripting

Shell assign variable to another variable

How can I assign a variable to an variable. IE $car=honda One way I can do it is export $car=honda or let $car=2323 Is there any other ways to preform this task (3 Replies)
Discussion started by: 3junior
3 Replies

6. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

7. Programming

Assign NULL to stream objects

Hi, I am trying to initialize fstream objects to NULL but its throwing an error.Could somebody tell me better way to do this. I have code in C++ & compiling it using g++ compiler on a Linux box. ifstream file; file=NULL; error omega.cpp:38: error: no match for 'operator='... (4 Replies)
Discussion started by: forstudy3
4 Replies

8. Shell Programming and Scripting

grep and assign it to variable

Hi , Help required i want to grep a pattern from a file using "grep -n" command then cut the field (i.e line number return by cut command) and assign it to a variable e.g var=grep -n "end" FILE1 | cut -f1 -d":" But i am not able to perform this operation. i am performing all... (6 Replies)
Discussion started by: xyz123
6 Replies

9. Shell Programming and Scripting

assign subst|grep|sed command result to a variable

Hi, I'm quite new to scripting and I want to modify following line of an existing script: MYVAR=`subst |grep 'L:\\\:' | sed -e 's/.*\\\//'`; What I have to do is to use the content of a variable instead of the constant expression 'L:\\\:' as the grep string to be matched. Assuming I already... (5 Replies)
Discussion started by: snowbiker99
5 Replies

10. Shell Programming and Scripting

How to filter a null variable.

I have the necessity to use this command: cat file1 | grep $A | grep $B where sometime A and, or, B are not set. For example: I set A=@ never defined or set B The above command send a cat error. Any idea? Thanks. Giovanni (1 Reply)
Discussion started by: gio123bg
1 Replies
Login or Register to Ask a Question