ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
This works but may not be the most elegant solution:
Code:
for NEXTWORD in `awk -FPATTERN '{print $2}' test |awk '{print $1}' |sort`
do grep " PATTERN $NEXTWORD " filename
done
In the above "filename" would be replaced by whatever your file's name is. "PATTERN" would be whatever your pattern is.
NEXTWORD is an abitrary name for the variable - you can call it BILLYBOB or anything else you prefer.
awk -FPATTERN '{print $2}' says to print anything that occurs after your PATTERN in the file. This of course starts with the next word following PATTERN. (-F tells it to use PATTERN as the delimiter instead of white space).
This is then piped into the next awk which prints only the first word from the previous awk which is the word you were interested in sorting on. (Note this uses white space as the delimiter because as noted above that is the default for awk - if your next word contains any white space you'd have to figure out a different delimiter to use.)
It then sorts the list of next words alphabetically using the sort command.
Finally it greps for any line that contains the next word found by the awk/awk/sort combo that follows directly after your PATTERN (and for good measure puts a space between those and surrounding words so it doesn't accidentally hit on an embedded word).
This will work fine so long as you only have the next word following pattern in your file once. If they appear twice it will still work relative to other next words but the two lines themselves may not be in the order you want.
A small remark on jlightner's solution: It seems to me that a NEXTWORD appearing twice will also make each corresponding line appear twice in the result, as the file will be grepped twice for NEXTWORD. You can avoid this by piping the output of "sort" through "uniq".
A small remark on jlightner's solution: It seems to me that a NEXTWORD appearing twice will also make each corresponding line appear twice in the result, as the file will be grepped twice for NEXTWORD. You can avoid this by piping the output of "sort" through "uniq".
It won't appear twice unless it is in the file twice. I think you're confusing this with the standard "ps -ef |grep WORD" solution where you have to remember to grep out the word grep itself. As an FYI I had tested it against his example before posting it.
Only in a Judo sort of way - you used my awk against me instead of your sed
Actually you made a good point. I was confused by you saying "NEXTWORD twice" because I was thinking you meant I used the variable twice - you meant the word the variable represented could have appeared twice.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.