Assign grep match to variable question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assign grep match to variable question
# 1  
Old 07-19-2012
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 don't have the grep -m option and this is on Solaris
# 2  
Old 07-19-2012
use while or for loop for this...
# 3  
Old 07-19-2012
Thats what I've been trying but it keeps assigning all match to the variable where as I want it to cycle through each match and assign it to the variable

Think this will do it

Code:
counter=1
total=$(grep "AA1kc3MAAACBAK5/lvn4AxBeOBv2HEy27OC8YpTs+vAy" authorized_keys | wc -l)
while (( ${total} >= ${counter} ))
do
dis_var=$(grep "AA1kc3MAAACBAK5/lvn4AxBeOBv2HEy27OC8YpTs+vAy" AIX_jptdc1up1318_authorized_keys | sed -n "$counter"p
echo "$dis_var"
counter=$(( counter + 1 ))
done


Last edited by Jazmania; 07-19-2012 at 07:06 AM..
# 4  
Old 07-19-2012
try this way...

Code:
 cat test_file_tmp
12
2525351
36524
989
25

grep "25" test_file_tmp | while read line
do
echo $line
done

2525351
25

# 5  
Old 07-19-2012
Would this do the task:

Code:
for dis_var in $(grep "AA1kc3MAAACBAK5/lvn4AxBeOBv2HEy27OC8YpTs+vAy" AIX_jptdc1up1318_authorized_keys)
do echo "$dis_var"; # do sth else to dis_var
done


Last edited by Scott; 07-19-2012 at 10:57 AM.. Reason: Code tags
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. Shell Programming and Scripting

If grep value is null then assign 0 to variable

DELETE=`cat $logfile1 | egrep -i "Delete" | sed 's/ */ /g' | cut -d" " -f2` INSERT=`cat $logfile1 | egrep -i "Insert" | sed 's/ */ /g' | cut -d" " -f2` UPDATE=`cat $logfile1 | egrep -i "Update" | sed 's/ */ /g' | cut -d" " -f2` I need soming like below: if value is null... (8 Replies)
Discussion started by: Veera_V
8 Replies

3. Shell Programming and Scripting

match and assign variables

Hi guys, I'm currently writing a script for automating a FreeBSD ZFS setup (ZFSonRooT). I got stuck at one point for raidz(1,2 a.k.a raid5,6) and am in need of assistance. This is what I need. example: #!/bin/sh <- must stay sh echo -n "hdd list: " read hdd_list echo -n "hdd label list:... (2 Replies)
Discussion started by: da1
2 Replies

4. 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

5. Shell Programming and Scripting

Need help to grep for a title match and then make some queries after the match

Here is the sample of my file address.txt Address 1 1234 Drive way New Orleans, LA Zipcode :- 12345 Address 2 4567 Spring way Chicago, IL Zipcode :- 67890 I would like to grep for an Address title (Ex :- Address 2) , then get its zipcode and echo both in a single line. Ex :- ... (3 Replies)
Discussion started by: leo.maveriick
3 Replies

6. Shell Programming and Scripting

BASH: How do I grep on a variable? (or simmilar question that makes sense)

Hi, I've been running code which very frequently calls books.csv. e.g: grep -i horror books.csv > tempExcept, I'm trying to move away from using temporary files or frequently calling books.csv to improve efficiency. So I tried something like bookfile=$(cat books.csv) grep -i horror... (4 Replies)
Discussion started by: Quan
4 Replies

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

8. UNIX for Dummies Questions & Answers

grep question - match a url

I'm trying to find files which have urls such as "services/amf/fri/home.html" and "services/amj/fri/air.html" - so the pattern I want to match with grep, logically, is one that has 1. "services/" 2. stuff in between including slashes, numbers, underscores etc 3. ending in "html" Can... (5 Replies)
Discussion started by: pauljohn
5 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

question on sed grep awk from variable

i am still confusing on how to use sed, grep and awk if the input is not a file but a variable. such as: a="hello world" b="how are you" c="best wish to you" d="222,333,444" what if i want to check which variable $a,$b,$c,$d have contain "you" what if i want to replace the word "you"... (9 Replies)
Discussion started by: 3Gmobile
9 Replies
Login or Register to Ask a Question