grep. Is there a better way to use it? (Solved)

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
viking777

grep. Is there a better way to use it? (Solved)

Post by viking777 »

A lot of people swear by grep as a search tool and it can be both quick and powerful. Sadly, every time I use it, I find it to be fairly useless because it never gives me the information I want to see. Maybe this means I am using it wrongly, hence this question.

Example.

I want to see when I installed updated gnome-shell last so:

Code: Select all

cat /var/log/apt/history.log | grep gnome-shell
Upgrade: gnome-shell:amd64 (3.2.2.1-1, 3.2.2.1-2), mc:amd64 (4.7.0.9-2, 4.8.1-2), libsemanage-common:amd64 (2.1.0-2, 2.1.6-2), gnome-shell-common:amd64 (3.2.2.1-1, 3.2.2.1-2), libsasl2-2:amd64 (2.1.25.dfsg1-3, 2.1.25.dfsg1-4), libsasl2-modules:amd64 (2.1.25.dfsg1-3, 2.1.25.dfsg1-4), libsemanage1:amd64 (2.1.0-2, 2.1.6-2)
It found gnome-shell no bother, but it doesn't tell me what I want to know which is when did I last update it. So I then have to do:

Code: Select all

cat /var/log/apt/history.log


And use the shell search function to look for gnome-shell which gives me this:

Code: Select all

Start-Date: 2012-03-10  10:23:53
Commandline: /usr/sbin/synaptic
Install: mc-data:amd64 (4.8.1-2, automatic)
Upgrade: gnome-shell:amd64 (3.2.2.1-1, 3.2.2.1-2), mc:amd64 (4.7.0.9-2, 4.8.1-2), libsemanage-common:amd64 (2.1.0-2, 2.1.6-2), gnome-shell-common:amd64 (3.2.2.1-1, 3.2.2.1-2), libsasl2-2:amd64 (2.1.25.dfsg1-3, 2.1.25.dfsg1-4), libsasl2-modules:amd64 (2.1.25.dfsg1-3, 2.1.25.dfsg1-4), libsemanage1:amd64 (2.1.0-2, 2.1.6-2)
Exactly the same as the first entry except that it has 3 extra lines on it and it is those 3 lines that I want to know about.

So am I using it wrongly or is it not capable of giving the information I want in one command?
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Habitual

Re: grep. Is there a better way to use it?

Post by Habitual »

I used to use cat x | grep y, but then I found another way...

Code: Select all

grep gnome-shell /var/log/apt/history.log
Being a systems admin 48 hours a day with dozens of terminals open, varying degrees of work across all those systems, I use grep like so

Code: Select all

sudo grep unresponsive /var/log/ -HnR
or

Code: Select all

sudo grep -HnR unresponsive /var/log/
−H, −−with−filename
−n, −−line−number
−R, −r, −−recursive
Just the file 'hit'...

Code: Select all

grep -HnR gnome-shell /var/log/ -l
/var/log/messages

Ignore case is -i
−i, −−ignore−case

Code: Select all

alias grep='grep --color=auto -iRn'
does most of it for me on just a regular grep command. :) (EXCEPT on sudo grep")

Personally, my switch I use everytime is the -n because I use vi with "goto line number" function, like so
Example grep output is grep with line numbers:
18418:Mar 13 10:28:00 localhost sudo: jj : TTY=pts/1 ; PWD=/home/jj ; USER=root ; COMMAND=/usr/bin/grep -HnR GNOME sHELL /var/log/ -l
18421:Mar 13 10:28:14 localhost sudo: jj : TTY=pts/1 ; PWD=/home/jj ; USER=root ; COMMAND=/usr/bin/grep -HnR GNOME sHELL /var/log/ -l

sudo vi +18418 /var/log/messages
takes me to line number 18418 in /var/log/messages.

HTH.

JJ
Habitual

Re: grep. Is there a better way to use it?

Post by Habitual »

viking777 wrote:...So am I using it wrongly or is it not capable of giving the information I want in one command?
Just not efficiently maybe...?

Code: Select all

grep gnome-shell /var/log/apt/history.log | grep "Upgrade"
grep gnome-shell /var/log/apt/history.log | grep -v "apt-get"
or

Code: Select all

grep gnome-shell /var/log/apt/history.log | tail -1
Other more elegant solutions probably exist. :)
viking777

Re: grep. Is there a better way to use it?

Post by viking777 »

Thanks for that Habitual, it is always interesting to see how other people tackle a particular problem and there is some very interesting information there. I had looked at the -n switch myself, and it is certainly the nearest I can come to what I am looking for, but it still doesn't really do it. The problem comes if you want to grep through more than one file because line numbers are then meaningless.

So I guess that is where

Code: Select all

grep -HnR gnome-shell /var/log/ -l
comes in. I will definitely note that one down, I could find that very useful (although I needed the -i switch as well before it worked for what I wanted to do).

Your first command is sensible too, no point in using cat when grep works on its own.

Interesting though - gave me something to think about :D
Habitual

Re: grep. Is there a better way to use it?

Post by Habitual »

I often tell people "grep the grep grep grep" which should be "self-explanatory". :oops:

It's a nightmare until I find a better way.
User avatar
xenopeek
Level 25
Level 25
Posts: 29597
Joined: Wed Jul 06, 2011 3:58 am

Re: grep. Is there a better way to use it?

Post by xenopeek »

Knowing how the /var/log/apt/history.log shows things, you can easily do what you want with:

Code: Select all

grep -B 2 gnome-shell /var/log/apt/history.log
The "-B 2" tells grep to print the 2 lines before a matching line. You can also print lines after a matching line with "-A".

It helps to know a little about the format of the file you are searching :wink:
Image
viking777

Re: grep. Is there a better way to use it?

Post by viking777 »

xenopeek wrote:Knowing how the /var/log/apt/history.log shows things, you can easily do what you want with:

Code: Select all

grep -B 2 gnome-shell /var/log/apt/history.log
The "-B 2" tells grep to print the 2 lines before a matching line. You can also print lines after a matching line with "-A".

It helps to know a little about the format of the file you are searching :wink:
Ah! Now that is a real gem Vincent thank you very much, that goes straight into my command line cheat sheet :lol: . Adaptable too - I like it :D

(It actually needs -B 3 to get the date for me, but no matter - it works! Of course the number of lines that you need to print before the search string is dependent on where "gnome-shell" occurs in the update and how big the update is, a bit of a guessing game, but still a solution as far as I am concerned).
Locked

Return to “Scripts & Bash”