Output of a Grep(Which already has a variable) into a Variabale


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Output of a Grep(Which already has a variable) into a Variabale
# 1  
Old 04-01-2013
Output of a Grep(Which already has a variable) into a Variabale

Hi All,
I have a an array, in which i need to loop through all the variable and check each variable individually for a pattern.

For ex
Code:
#!/bin/ksh

set -A FileNames $(</Temp/AllNames)
echo "Total array elements :" ${#FileNames[*]}

for i in ${FileNames[@]}
do
# if the "${i} | grep Last" returns true for this then i have #to do some manipulations
#Something like below
Temp = `${i} | grep Last` 
 
done

# 2  
Old 04-01-2013
Use CODE TAGS

you should not use Temp = `${i} | grep Last`. that should be Temp=$(echo ${i} | grep Last)
This User Gave Thanks to PikK45 For This Post:
# 3  
Old 04-01-2013
I noticed that and changed, But u were too fast Smilie
# 4  
Old 04-01-2013
I would have avoided those ` u use. Rather having an array created for the whole file, I would have done like

Code:
#!/bin/ksh

echo "Total elements : $(cat /Temp/AllNames | wc -l)"

while read i
do
# if the "${i} | grep Last" returns true for this then i have #to do some manipulations
#Something like below
Temp=$(echo ${i} | grep Last)
done < /Temp/AllNames

This would be simple than your code, 'cause it needs memory for allocation Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

2. UNIX for Dummies Questions & Answers

How to have the output in grep?

Hi guys - I am trying to have a certain file display information horizontally divided by a pipe. for example file name: foo.dat has information like this: name1 name2 name3 namenamename4 namene5 I would like it to display like this: name1|name2|name3|namenamename4|namene5 ... (6 Replies)
Discussion started by: DallasT
6 Replies

3. Shell Programming and Scripting

How to grep the desired output and output to a file?

currently I have process from a raw file to this stage ALTER TABLE "EXCEL_ADMIN"."TC_TXN_VOID" ADD CONSTRAINT "PK_TC_TXN_VOID" PRIMARY KEY ("TC_TXN_IID") ALTER TABLE "EXCEL_ADMIN"."TC_TXN_AMT" ADD CONSTRAINT "PK_TC_TXN_AMT" PRIMARY KEY ("TC_TXN_AMT_IID") ALTER TABLE... (10 Replies)
Discussion started by: jediwannabe
10 Replies

4. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

5. Shell Programming and Scripting

declaring a variabale as string type

I know that directly we can assign a string to a variable but is there any other way to declare a variable as string type. I am using tcsh shell where I am using a function which would return a value of string type but when I am using return keyword , it is returning only integer value.pls help. (3 Replies)
Discussion started by: maitree
3 Replies

6. Shell Programming and Scripting

ENV Variabale changes are not retained

ENV varibles set using a script are not retailed once the script execution is done. But if the same ENV variable is set in command line, it is retained. Consider the below case $ cat ti export MDS_CODE_HOME=`pwd` echo $MDS_CODE_HOME rsamadde@pcasvs04 ~/R2ANew $ ti... (3 Replies)
Discussion started by: gogol
3 Replies

7. Shell Programming and Scripting

Problem with assigning output of grep + awk to a variable

Hi All, I am getting the output for the following command when i run it on the unix console. --------------------------- grep `whoami` /etc/passwd | awk '{print ($1);}' | cut -d ":" -f3 ---------------------------- But i made it into a script and tried to print the variable, its... (5 Replies)
Discussion started by: meheretoknow
5 Replies

8. Shell Programming and Scripting

Put the output of grep in a variable

Hi, in a shell script how can I put the result of a grep command in a variable : myvariable=grep mystring myfilename Thank you. (3 Replies)
Discussion started by: big123456
3 Replies

9. Shell Programming and Scripting

redirect the grep output into the variable

i want to redirect the grep output into the variable but i am not able to get it i tried veri=`grep -i $1 "${logdir}"* | grep -i adding | grep -iv equation | tail -1 | cut -d ':' -f 1` vari=$(grep -i $1 "${logdir}"* | grep -i adding | grep -iv equation | tail -1 | cut -d ':' -f 1) BUT NOT... (1 Reply)
Discussion started by: mail2sant
1 Replies

10. UNIX for Dummies Questions & Answers

capturing the output of grep as integer variable

Hi, I have an expression using grep and nawk that captures the ID number of a given Unix process. It gets printed to screen but I don't know how to declare a variable to this returned value! For example, ps -ef|grep $project | grep -v grep | nawk '{print $2}' This returns my number. How... (2 Replies)
Discussion started by: babariba
2 Replies
Login or Register to Ask a Question