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

Forum rules
Before you post please read this

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

Postby viking777 on Tue Mar 13, 2012 7:37 am

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?
Fujitsu Lifebook AH532. Intel i5 processor, 6Gb ram, Intel HD3000 graphics, Intel Audio/wifi. Realtek RTL8111/8168B Ethernet.Lubuntu 13.10,Ubuntu12.10 (Unity), Mint16 (Cinnamon), Manjaro (Xfce).
Image
User avatar
viking777
Level 14
Level 14
 
Posts: 5153
Joined: Mon Dec 01, 2008 11:21 am

Linux Mint is funded by ads and donations.
 

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

Postby Habitual on Tue Mar 13, 2012 10:47 am

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
What's a landing but a take off in reverse?
My DorkBlog
User avatar
Habitual
Level 8
Level 8
 
Posts: 2001
Joined: Sun Nov 21, 2010 8:31 pm
Location: Slackware

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

Postby Habitual on Tue Mar 13, 2012 10:53 am

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. :)
What's a landing but a take off in reverse?
My DorkBlog
User avatar
Habitual
Level 8
Level 8
 
Posts: 2001
Joined: Sun Nov 21, 2010 8:31 pm
Location: Slackware

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

Postby viking777 on Tue Mar 13, 2012 11:39 am

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
Fujitsu Lifebook AH532. Intel i5 processor, 6Gb ram, Intel HD3000 graphics, Intel Audio/wifi. Realtek RTL8111/8168B Ethernet.Lubuntu 13.10,Ubuntu12.10 (Unity), Mint16 (Cinnamon), Manjaro (Xfce).
Image
User avatar
viking777
Level 14
Level 14
 
Posts: 5153
Joined: Mon Dec 01, 2008 11:21 am

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

Postby Habitual on Tue Mar 13, 2012 2:52 pm

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.
What's a landing but a take off in reverse?
My DorkBlog
User avatar
Habitual
Level 8
Level 8
 
Posts: 2001
Joined: Sun Nov 21, 2010 8:31 pm
Location: Slackware

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

Postby xenopeek on Wed Mar 14, 2012 5:40 am

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:
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14544
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

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

Postby viking777 on Wed Mar 14, 2012 6:52 am

Vincent Vermeulen 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).
Fujitsu Lifebook AH532. Intel i5 processor, 6Gb ram, Intel HD3000 graphics, Intel Audio/wifi. Realtek RTL8111/8168B Ethernet.Lubuntu 13.10,Ubuntu12.10 (Unity), Mint16 (Cinnamon), Manjaro (Xfce).
Image
User avatar
viking777
Level 14
Level 14
 
Posts: 5153
Joined: Mon Dec 01, 2008 11:21 am

Linux Mint is funded by ads and donations.
 

Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 2 guests