Locate a field in a record predicated on locating previous value (awk)
Linux - NewbieThis 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.
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.
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?
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.