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.
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
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.
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 ?
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
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 ?
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
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
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)
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)
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)
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)
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)
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)
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)
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)