Visit Our UNIX and Linux User Community

grep attribute value pair and store it a variable

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep attribute value pair and store it a variable
# 1  
Old 09-08-2009
grep attribute value pair and store it a variable


I have a file contains attribute value pair like.. rollno=583.0 pass=recon..

From the above line, i need to grep for only "rollno" and store "rollno=583.0" in a variable.

Pls suggest

Last edited by skraja1982; 09-08-2009 at 02:30 AM..
# 2  
Old 09-08-2009
What shell/language?
# 3  
Old 09-08-2009
in ksh
# 4  
Old 09-08-2009
Something like this :

var=`echo "name=erick rollno=583.0 pass=recon" | sed 's/.* \(rollno=[0-9.]*\) .*/\1/'`

echo $var

Using awk :

echo "name=erick rollno=583.0 pass=recon" | awk -F"[ ]" -v va="rollno" '$0 ~ va {for(i=1;i<=NF;i++) { if($i ~ va) print $i }}'

Last edited by panyam; 09-08-2009 at 03:54 AM..
# 5  
Old 09-08-2009
Thanks Panyam, it works fine.. need one more help..
Suppose if the file looks below rollno=ADsn:583 pass=recon..

then how to grep for rollno and store "ADsn:583" in a variable
# 6  
Old 09-08-2009
A bit modification to the above sed :

echo "name=erick rollno=ADsn:583 pass=recon" | sed 's/.* rollno=\(.*\) .*/\1/'

# 7  
Old 09-08-2009
Thanks a lot Panyam.. it works for me.. Smilie

Previous Thread | Next Thread
Test Your Knowledge in Computers #1001
Difficulty: Medium
Grace Brewster Murray Hopper was an American computer scientist and United States Navy rear admiral who found a moth in a computer stuck in a relay in 1947 and was later referred to as "Grandma COBOL" for her pioneering work in machine-independent programming languages.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I can't store value in the variable

!#bin/bash clear var= grep email.txt | wc -l echo $var echo $var exit 0 OUTPUT: 1000 _ _ Where _ represent space (no value or nothing) (4 Replies)
Discussion started by: Muhammad Rehan
4 Replies

2. Shell Programming and Scripting

How to store result of grep into a variable?

In a directory i have a file *output* whose contents are changing for every simulation. zgrep "trace file is" *output* | zgrep g o/p: trace file is Int_01.1352176388z4f56ec33.0.trace.gz I want to extract "Int_01.1352176388z4f56ec33.0.trace.gz" from the above result into a variable. i... (2 Replies)
Discussion started by: twistedpair
2 Replies

3. UNIX Desktop Questions & Answers

How to store the value of grep in a variable

Hi , I was trying to store the value of a grep command to store in a variable so i can use it somewhere else in my script but i am getting error, i am using bash shell in linux: var= `grep -n "$DATE" testfile.log | head -n 1 | sed -n 's/^\(*\).*/\1/p` echo "$var"I get the error: unexpected... (5 Replies)
Discussion started by: learninguser235
5 Replies

4. UNIX for Dummies Questions & Answers

How to grep and store in a file

Guys, I was wondering what command can be used to parse the "LaxOrdID" field into a separate file? These messages are in thousands and I need to perform a comparision. (6 Replies)
Discussion started by: DallasT
6 Replies

5. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

6. Shell Programming and Scripting

read xml tag attribute and store it in variable

Hi, How to read xml tag attributes and store into variable in shell script? Thanks, Swetha (5 Replies)
Discussion started by: swetha123
5 Replies

7. UNIX for Dummies Questions & Answers

Using GREP/AWK to extract a number and store it as a variable

Hello, I have a question regarding the awk command. Here is the line I need to grep: 1 F= -.13250138E+03 E0= -.13249556E+03 d E =-.174650E-01 mag= 35.2157 Instead of displaying the number in red I would like to store it as a variable such as X. Is there a way to do this? Thanks for any... (3 Replies)
Discussion started by: modey3
3 Replies

8. Shell Programming and Scripting

how to store grep command in a Variable

I have a variable A echo $A 5060 I am exporting the value X,Y,Z and it id fetching right thing and When I run C=`${X} -l ${Y} ${Z} "trap '' INT;. ~/.profile >/dev/null 2>/dev/null; netstat -na | grep \$A`" here it is going to same directory and also running netstat -na | grep 5060 ... (4 Replies)
Discussion started by: madhusmita
4 Replies

9. Shell Programming and Scripting

Grep results to store in a shell variable

I have the results of a grep with -n store in a shell variable ie VAR=`grep -n -e 'PATTERN' file` How ever I am missing the line breaks in the variable , How do I store the resualts of grep with many lines in to a variables. I want the varable should be the sawmway as we do the grep grep... (3 Replies)
Discussion started by: jojan
3 Replies

10. Shell Programming and Scripting

grep and store

When i grep a file and store it in a file it is storing as a zero byte file any way to avoid that..... (6 Replies)
Discussion started by: arunkumar_mca
6 Replies

Featured Tech Videos