LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 02: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, 04:47 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 05: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, 11:47 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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



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 07:26 PM
[SOLVED] BASH: Assign a variable to a variable name... c_henry Programming 9 06-04-2012 11:33 AM
Search and replace Pattern preceeding another pattern nbkisnz Programming 3 05-13-2012 01:50 PM
[SOLVED] /bin/bash if statement pattern search, end of pattern special character? headhunter_unit23 Programming 3 04-29-2010 08:05 AM
How do you assign a variable to be a variable file name in a directory? David_Elliott Programming 4 04-14-2009 10:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:19 PM.

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
Open Source Consulting | Domain Registration