LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-23-2012, 03:56 AM   #1
uk.engr
Member
 
Registered: Apr 2012
Posts: 131

Rep: Reputation: Disabled
Search pattern and assign to Variable


Assalam o Alaikum!

I used this command to search particular pattern from file:

grep -n "em16" abc.txt

--> em16 value1d 234


I want to assign this random value "234" to variable? for every time this value changes, I want to filter out other text and want this random value 234 to be assigned to variable.

thanks!
 
Old 10-23-2012, 05:47 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
If that's exactly one space between the fields, then
Code:
a=$(grep -n "em16" t.t|cut -d' ' -f3)
echo $a
234
For more spaces between each field, use awk
Code:
b=$(grep -n "em16" t.t|awk '{print$3}')
echo $b
234
 
Old 10-23-2012, 06:11 AM   #3
uk.engr
Member
 
Registered: Apr 2012
Posts: 131

Original Poster
Rep: Reputation: Disabled
Thanks dear, this helps me to proceed ahead
 
Old 10-23-2012, 12:47 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
There's no need to combine grep and awk (or grep and sed). Just use the one that has the greater flexibility.

Code:
b=$( awk '/em16/ { print $NF }' abc.txt )
b=$( sed -n '/em16/ s/.* //p' abc.txt )
NF in awk represents the final field, no matter what it is. In the sed expression "s/.* //" removes everything up to and including the final space in the line.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash search for a pattern within a string variable nutthick Programming 8 03-17-2015 08:26 PM
[SOLVED] BASH: Assign a variable to a variable name... c_henry Programming 9 06-04-2012 12:33 PM
Search and replace Pattern preceeding another pattern nbkisnz Programming 3 05-13-2012 02:50 PM
[SOLVED] /bin/bash if statement pattern search, end of pattern special character? headhunter_unit23 Programming 3 04-29-2010 09:05 AM
How do you assign a variable to be a variable file name in a directory? David_Elliott Programming 4 04-14-2009 11:19 AM


All times are GMT -5. The time now is 08:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration