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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
06-12-2012, 02:58 PM
|
#1
|
LQ Newbie
Registered: Jun 2012
Location: New York City
Posts: 5
Rep: 
|
Locate a field in a record predicated on locating previous value (awk)
Hi,
I have a file with several thousand records. I need to extract the value (from a field) within the records that is not always in the same position but does come after a unique, fixed value.
The records are of variable length - min 9 fields, max 12 fields. Somewhere in the record there's a field that contains the string "host". The very next field in the record is the variable length field whose value I need to extract. For example, the records could look like:
date time value value value value host sys12 secure client MAC status
date time value value host server16 client MAC status
date time value value value value host intl33-a client MAC status
So, from the records shown above I need to extract the values sys12, server16 & intl33-a.
I have only the most basic knowledge of awk but I have to believe there's a straightforward way to extract what I need. Can someone be so kind as to assist?
Thanks in advance for any help you can provide!!
|
|
|
06-12-2012, 03:05 PM
|
#2
|
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326
|
Code:
[schneidz@hyper ~]$ grep -o host.*" " oldfogey.txt | awk '{print $2}'
sys12
server16
intl33-a
there probably is a way to do it in one awk maybe usng indexes.
edit: here is the awk solution:
Code:
[schneidz@hyper ~]$ awk '
{ for(i=1;i<=NF;i++){
if ($i ~ /\<host\>/)
{print $(i+1)} }
}' oldfogey.txt
sys12
server16
intl33-a
Last edited by schneidz; 06-12-2012 at 03:24 PM.
|
|
1 members found this post helpful.
|
06-12-2012, 03:16 PM
|
#3
|
LQ Newbie
Registered: Jun 2012
Location: New York City
Posts: 5
Original Poster
Rep: 
|
Awesome! Works perfectly.
Thanks for the quick response.
Regards,
Anthony
|
|
|
06-13-2012, 09:29 AM
|
#4
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
Here's a sed solution too:
Code:
sed -rn '/host/ s/.*host ([^ ]+).*/\1/p' infile
BTW, I'd also use a simple string comparison in the awk command, rather than a regex. And you can condense it into a one-liner like this:
Code:
awk '{ for (i=1;i<=NF;i++) { if ($i=="host") { print $(i+1) } } }' infile
|
|
1 members found this post helpful.
|
06-13-2012, 11:48 AM
|
#5
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,037
|
Code:
grep -Eo 'host [^ ]*' file | cut -d' ' -f2
awk -F"host " '{print gensub(/ .*/,"","1",$2)}' file
awk 'match($0,/host ([^ ]*)/,f){print f[1]}' file
awk '{split(RT,a);print a[2]}' RS="host [^ ]*" file
|
|
|
06-13-2012, 07:28 PM
|
#6
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,434
|
From scheidz:
Code:
grep -o host.*" " oldfogey.txt | awk '{print $2}'
I like; didn't know/realise about the -o option.
Having done some testing for my own edification, I would use
Code:
grep -o " "host" ".*
otherwise it can match the string 'host' with either/both of trailing/prefix non-space chars
Code:
# extended test file
cat t.t
date time value value value value host sys12 secure client MAC status
date time value value host server16 client MAC status
date time value value value value host intl33-a client MAC status
date time value value value value hostx intl33-a client MAC status
date time value value value value zhost intl33-a client MAC status
# orig code match
grep -o host.*" " t.t
host sys12 secure client MAC
host server16 client MAC
host intl33-a client MAC
hostx intl33-a client MAC
host intl33-a client MAC
# new code match
grep -o " "host" ".* t.t
host sys12 secure client MAC status
host server16 client MAC status
host intl33-a client MAC status
|
|
|
All times are GMT -5. The time now is 06:11 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|