Bash is as useful as you are able to manipulate your hand-off. Some of us like one-liners, heck, we love one liners. This isn’t one of those, this requires you to make a script file and then punch it. It works using layered loops and some process substitution. You have to cd to the dir where the search is going to go down. Then it will find files in a dir with full path, which you then grep a string out it of based on a search and awk a field, then you look through those files for that field and all items around it. This saves the pain a manual -A -B that will still falter you in your one line “quick” search.

This can be useful for grabbing various PID information for specific processes when other items might pertain to those PIDs but not the actual process name or you are looking to filter by PID and have an event to localize the PID by.

Another use is to find all email logs in the mail.log relating to a specific address or user.

filename: aboutthat.sh

#!/bin/bash
cd /var/log/
while read file
do
  while read line
  do
    echo 'Data element: ' $line
    zgrep "$line" $file
  done < <(zgrep dataelement $file | awk '{ print $1 }')
done < <(ls -d -1 $PWD/*)

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.