Handling Variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Handling Variables
# 1  
Old 04-29-2011
Handling Variables

Experts & Learned Folks,

Im asking for a solution that Im not sure as how to handle. Owing to reasons of anonymity, I masked some of the text that Im searching for here. Ok. Here it goes.

I have an output of script like below -
Code:
THIS IS THE  DATALEFT ON FIRST LINE WITH COUNT 75.
THIS IS THE  DATALEFT ON SECOND LINE WITH COUNT 483.
THIS IS THE  DATALEFT ON THIRD LINE WITH COUNT 84839.
THIS IS THE  DATALEFT ON FOURTH LINE WITH COUNT 4939.


Now the above lines can be either 2 or 15 and based on the above lines, what Im doing right now is storing each line into a variable value and then massage the variable and run a "grep" on the variable values stored on a different file.

For example,
STEP 1-
Im storing var1 as below -
Code:
VAR1=`echo $VAR | awk -F"," '{print $5}' | sed 's/DATALEFT//g' | tr -d "'" | sed -e 's/^ *//g;s/ *$//g' | awk '{print $1, $2, $3}'| tr "a-z" "A-Z" `

STEP 2 -
And this is how Im using this $VAR1 is below -
Code:
if [ "$VAR1" != "" ]
then
echo "$VAR1" | egrep -qi "NEGATION TEXT|NOT TO SEARCH"
if [[ $? -ne 0 ]] ; then
        echo "The Phone numbers That Have The LOG Searched as \"$VAR1\" - "
        cat $ANOTHERVAR |  grep -i "$VAR1" | cut -d ':' -f4 | cut -d ')' -f1 | uniq | head -10
fi
fi

STEP 3 -

Code:
echo $VAR | grep -qi 'NOT TO SEARCH'
if [ $? -eq 0 ]; then
        echo "The Phone numbers That Have The NEGATION TEXT NOT TO SEARCH Are - "
        cat $ANOTHERVAR | grep -i 'NEGATION TEXT NOT TO SEARCH' | sed "s/^.*(\([^)]*\)).*(....\(.*\)).*'\(.*\)'.*/\1 \2 \3/" | awk '{print $1, $2, $3"-"$4"-"$5}' | sed 's/-$//g' | sed 's/-$//g' 
fi


My code above works fine if there are 4 variables declared and there are 4 lines as output of the script that I initially read from. However, In Production that is not the case and it might have sometimes 10 lines and sometimes only 2 lines but the maximum it can go is unto 15 lines and the minimum value it can hit is 0.

My code is not handling this properly and it is searching for empty spaces stored in variables like VAR1, VAR2, VAR3, VAR4 … VAR10 and then display whatever it can.

So Im thinking of declaring an Array so that the size of the array is of 15 elements and depending upon the value stored in the array which might be either 0 or 15 and then assign each line to each var and then run "Grep" on that variable that is stored dynamically.

Im not sure as how to do that.

Can anyone help ?

regards,
Manohar.

Last edited by Yogesh Sawant; 05-01-2011 at 07:12 AM.. Reason: added code tags
# 2  
Old 04-29-2011
Hi Manohar,
please use code tags, it makes it much easier to read.
Quote:
I have an output of script like below -

THIS IS THE DATALEFT ON FIRST LINE WITH COUNT 75.
THIS IS THE DATALEFT ON SECOND LINE WITH COUNT 483.
THIS IS THE DATALEFT ON THIRD LINE WITH COUNT 84839.
THIS IS THE DATALEFT ON FOURTH LINE WITH COUNT 4939.
Is this the input that you are processing?

I'd recommend you don't store each line into a separate variable, but design a filter that goes through the input line-by-line and ooutputs what you want. Most of the tools you are using (sed, awk, grep) are filters that do exactly that: take one line, process it, output, take next. That way you don't have to worry whether your input has 15, 0 or 1500 lines.

I don't understand what exactly are you trying to accomplish, so am unable to help with specifics at the moment. Please try to post input and desired output.
# 3  
Old 04-29-2011
Mirni,

Thanks for your response.

Yes, I have an input like this below -
<
THIS IS THE DATALEFT ON FIRST LINE WITH COUNT 75.
THIS IS THE DATALEFT ON SECOND LINE WITH COUNT 483.
THIS IS THE DATALEFT ON THIRD LINE WITH COUNT 84839.
THIS IS THE DATALEFT ON FOURTH LINE WITH COUNT 4939.
/>

Now, I wish I could be more specific but Im not sure as how to ?

<I'd recommend you don't store each line into a separate variable, but design a filter that goes through the input line-by-line and ooutputs what you want. Most of the tools you are using (sed, awk, grep) are filters that do exactly that: take one line, process it, output, take next. That way you don't have to worry whether your input has 15, 0 or 1500 lines.
/>
Yes, I dont want to store each line in a separate variable but Im not sure as how to go about that ?
For example, on Line 1 I want to extract <FIRST LINE/> and then run a grep on a file and but the word "FIRST LINE" is not expected in the first line always and it can be even in the 15 th line.
So my search should be intelligent enough to parse and grep on the variable "FIRST LINE".
How do I do that ?

regards,
ManoharMa.
# 4  
Old 04-29-2011
Just like you're doing here:
Code:
echo $VAR | awk -F"," '{print $5}' | sed 's/DATALEFT//g' | tr -d "'" | ...

but instead of $VAR run the filter on your input file:
Code:
awk -F"," '{print $5}' < input |  sed 's/DATALEFT//g' | tr -d "'" | ...

when your filter extracts what you want, you can read it into a variable through pipe and grep the extracted stuff against some other file, somthing like this:
Code:
awk -F"," '{print $5}' < input |  sed 's/DATALEFT//g' | tr -d "'" | ... | while read extracted ; do 
  if [ X"`grep $extracted $fileToFindExtarctedIn`" = X ] ; then 
    echo "$extracted not found in $fileToFindExtarctedIn 
  fi 
done

# 5  
Old 04-29-2011
Mirni,

I started off my code more or less like the way you wrote it in here & it was not splitting the strings in the way I wanted it to & hence it fed incorrect searches for the "GREP".

Having said that, you gave me a new hope and belief that I was initially headed in the right direction & was a bit apprehensive but now I will try your approach.

I shall try to code & run it the way you mentioned in here & see what happens as my program is bit more complex than what I wrote in here.

I will certainly post my comments as well on here.

Thanks a lot Mirni & I appreciate your site. This place has some very well informed experts like you.

HTML Code:
Btw, while you are at it, how do we code in here so that each separate line stays in an array element and retrieve each of the array elements ? Can you show as how to do that as well Please ?
regards,
ManoharMa.
# 6  
Old 04-30-2011
I'm happy to be part of Linux community and help when I can. And I am not an expert by no means, although it sounds nice Smilie

Suggestions:
- Divide & conquer: break down what you are trying to accomplish into smaller pieces; and put the partials together when you're sure they work as you intended. You can use functions to encapsulate code.
- Redirect into temporary files for easier debugging. E.g.:
Code:
awk -F"," '{print $5}' < input |  sed 's/DATALEFT//g' | tr -d "'" >> $tmp
while read line ; do 
   grep $line $someFile
done < $tmp

Now you can look into $tmp file to check that the filters are pulling what you want.
- Make use of patterns in your input. If you know, that a particular pattern always appears somewhere, you can use it to your advantage. E.g., if I want to pull a phone number, and I know it's always in the form (xxx)-xxx-xxxx, where x are digits, you can easily build a regex that pulls just that.


Ok, now to the arrays in bash. (What shell are you using?)
Code:
$ cat input 
first line
second longer line
my line number three

This is how you can push each line into an array:
Code:
$ unset a ; while read ln ; do  
    a[${#a[@]}]=$ln 
done < input

Code:
${#a[@]}

is number of elements of array.

Access particular element:
Code:
$ echo ${a[1]}
second longer line

Loop through all array:
Code:
$ for i in "${a[@]}" ; do echo $i ; done
first line
second longer line
my line number three

What you can also do, is take a line and read it into an array, separating on whitespaces (or something else, if you redefine variable $IFS):
Code:
$ echo "one two three" | while read -a a ; do 
   echo first: ${a[0]} second: ${a[1]} third: ${a[2]}
 done
first: one second: two third: three

You can do so much more with arrays: matching, substitutions... check out some examples here:
http://tldp.org/LDP/abs/html/arrays.html
Good luck!

Last edited by mirni; 04-30-2011 at 02:25 AM..
# 7  
Old 05-02-2011
Mirni,

Quite thankful on your well understood & quite thoughtful answer/reply.

I shall try to follow your lead here and see what I can come up with.

Yes, Im using Bash for all future references.

Have a great day ~

ManoharMa.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to pass variables into anothother variables?

Below are three variables, which I want to pass into variable RESULT1 username1=userid poihostname1=dellsys.com port1=8080 How can I pass these variables into below code... RESULT1=$((ssh -n username1@poihostname1 time /usr/sfw/bin/wget --user=sam --password=123 -O /dev/null -q... (4 Replies)
Discussion started by: manohar2013
4 Replies

2. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

3. Shell Programming and Scripting

BASH arrays and variables of variables in C++

Sometimes it is handy to protect long scripts in C++. The following syntax works fine for simple commands: #define SHELLSCRIPT1 "\ #/bin/bash \n\ echo \"hello\" \n\ " int main () { cout <<system(SHELLSCRIPT1); return 0; } Unfortunately for there are problems for: 1d arrays:... (10 Replies)
Discussion started by: frad
10 Replies

4. Shell Programming and Scripting

Running a script with multiple variables like 25 variables.

Hi All, i have a requirement where i have to run a script with at least 25 arguements and position of arguements can also change. the unapropriate way is like below. can we achieve this in more good and precise way?? #!/bin/ksh ##script is sample.ksh age=$1 gender=$2 class=$3 . . .... (3 Replies)
Discussion started by: Lakshman_Gupta
3 Replies

5. Shell Programming and Scripting

Error handling

Hello fellow UNIX gurus :) I have a problem regarding the script below: # Variables used in this shell. power=0 # Stores squared integer total=0 # Sum of all squared integers num=0 # Stores command line arguements # Provides error handling if command line... (5 Replies)
Discussion started by: Learn4Life
5 Replies

6. Shell Programming and Scripting

handling with grep

commad is like this cat filecount.txt | awk -F " " '{print $9}'| grep rcm*200212* the problem is with * its not taking * for grep. (11 Replies)
Discussion started by: sagar_1986
11 Replies

7. Shell Programming and Scripting

Handling null Integer/string variables

I kind of found out the hard way that I am not able to manipulate the null value, the long silence that happens when there is no value returned. I am looking for PIDs, and when there is no PID return, I wanted to handle this special scenario. Here is my script. #!/bin/bash LAN_VARIABLE=... (7 Replies)
Discussion started by: lan123
7 Replies

8. UNIX for Advanced & Expert Users

please help me in file handling

sir i have to get first line from a file for example >cat file1 abc zxc asd adsf from that file1 i need only first line expected result >abc please help me ! (1 Reply)
Discussion started by: ponmuthu
1 Replies

9. Programming

How to convert byteArray variables to HexaString variables for Linux?

Hello everybody, I am having problem in converting byte array variables to Hexa String variables for Linux. I have done, converting byte array variables to Hexa String variables for Windows but same function doesn't work for linux. Is there any difference in OS ? The code for Windows is given... (2 Replies)
Discussion started by: ritesh_163
2 Replies

10. UNIX for Dummies Questions & Answers

Parameter handling

Hi, i would like to ask that: If u need to do something like this: counter = 1; so that $($counter) = $1, and when u counter++, $($counter) will become $2. How can we do this? (1 Reply)
Discussion started by: AkumaTay
1 Replies
Login or Register to Ask a Question