txt3rob
Posts: 365
Joined: Sat Aug 11, 2012 3:45 pm
Contact: Website

Grep help

Mon Jun 10, 2013 1:08 pm

Hi can any one help with this

When i grep i would like this line to appear but i would like to remove the Enc=WEP and still display the signal strength

Code: Select all

[email protected]:~/wconfig# grep "Enc=WEP" wifi.txt
O2guestF2C49F Enc=WEP 100%
so it should display.

Code: Select all

O2guestF2C49F 100%
any help would be appreicatied
The Raspberry Pi Hell Guy - Random Ramblings to assist me and others.
http://raspberrypihell.blogspot.com
My Github - http:/www.github.com/random-robbie/
http://www.smspi.co.uk - send free uk sms via your raspberry pi from here

User avatar
PeterO
Posts: 5351
Joined: Sun Jul 22, 2012 4:14 pm

Re: Grep help

Mon Jun 10, 2013 1:13 pm

Code: Select all

grep "Enc=WEP" wifi.txt | awk '{print $1 " " $3}'
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

txt3rob
Posts: 365
Joined: Sat Aug 11, 2012 3:45 pm
Contact: Website

Re: Grep help

Mon Jun 10, 2013 1:19 pm

You sir are a legend thank you very much
The Raspberry Pi Hell Guy - Random Ramblings to assist me and others.
http://raspberrypihell.blogspot.com
My Github - http:/www.github.com/random-robbie/
http://www.smspi.co.uk - send free uk sms via your raspberry pi from here

scrapheap
Posts: 20
Joined: Wed Feb 13, 2013 5:13 pm

Re: Grep help

Mon Jun 10, 2013 3:51 pm

If you are going to be using awk then you don't have to use grep, just tell awk what pattern you want to match first.

Code: Select all

awk '/Enc=WEP/ {print $1 " " $3}' wifi.txt
You could also use sed to filter the file as well.

Code: Select all

sed -e '/Enc=WEP/ !d' -e's/Enc=WEP //' wifi.txt

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 24641
Joined: Sat Jul 30, 2011 7:41 pm

Re: Grep help

Mon Jun 10, 2013 4:36 pm

I like AWK, I just wish I used it enough to actually remember how to use it each time instead of having to resort back to man EVERY SINGLE TIME.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“I own the world’s worst thesaurus. Not only is it awful, it’s awful."

User avatar
PeterO
Posts: 5351
Joined: Sun Jul 22, 2012 4:14 pm

Re: Grep help

Mon Jun 10, 2013 6:45 pm

jamesh wrote:I like AWK, I just wish I used it enough to actually remember how to use it each time instead of having to resort back to man EVERY SINGLE TIME.
Yeah, I managed my solution without reference to the manual page, but I couldn't have produced scrapheap's without reference to it.
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
jojopi
Posts: 3110
Joined: Tue Oct 11, 2011 8:38 pm

Re: Grep help

Mon Jun 10, 2013 7:38 pm

I do not think there is much benefit to learning sed or awk these days. Anything that can be done with either of those can be done just as easily with perl. Perl is (slightly) more like a real language, and does not have the awkward distinction between basic and extended regular expressions. Even though awk and sed are both POSIX and perl is not, in practice Perl is more standard and more portable.

Code: Select all

perl -ne'print if s/ Enc=WEP//' wifi.txt

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 24641
Joined: Sat Jul 30, 2011 7:41 pm

Re: Grep help

Mon Jun 10, 2013 8:27 pm

jojopi wrote:I do not think there is much benefit to learning sed or awk these days. Anything that can be done with either of those can be done just as easily with perl. Perl is (slightly) more like a real language, and does not have the awkward distinction between basic and extended regular expressions. Even though awk and sed are both POSIX and perl is not, in practice Perl is more standard and more portable.

Code: Select all

perl -ne'print if s/ Enc=WEP//' wifi.txt
Perls OK, but not as readable to the uninitiated in this particular circumstance (compared with AWK)
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“I own the world’s worst thesaurus. Not only is it awful, it’s awful."

User avatar
PeterO
Posts: 5351
Joined: Sun Jul 22, 2012 4:14 pm

Re: Grep help

Mon Jun 10, 2013 8:43 pm

jojopi wrote:I do not think there is much benefit to learning sed or awk these days.
perl is a nightmare unless you use it day in day out.
sed and awk are still being covered on Unix/Linux courses so they are still considered to be useful tools.
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

scrapheap
Posts: 20
Joined: Wed Feb 13, 2013 5:13 pm

Re: Grep help

Tue Jun 11, 2013 8:41 am

Perl is great and I do use it pretty much every day, but for some tasks it is a bit overkill and I find it easier and quicker to just use grep, sed or awk. Especially if I am writing a shell script.

Generally if all I am doing is looking for things then I use grep (usually with the -P option so that I can use Perl regular expressions for the matching). If all I would be doing is filtering a file with one or more regular expressions then I would use sed, and if the filtering is a bit more complex then I would fall back to awk.

Anything beyond filtering files though and Perl would be my scripting language of choice, and it is definitely my first choice for cgi-scripts.

Return to “General programming discussion”