LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-16-2015, 07:39 AM   #1
spatieman
Member
 
Registered: Jan 2004
Location: netherlands, europe, in a crapy house. xD
Distribution: openBSD
Posts: 73

Rep: Reputation: 21
how do i select with grep and awk only ONE text content in a script


i got a nasty isue here.
for my machine i want to make a checkup script to see to what wireless network i am connected to.

if i type iwconfig i get below output.
Code:
 iwconfig
wlan0     IEEE 802.11bgn  ESSID:"APqwerty"
          Mode:Managed  Frequency:2.447 GHz  Access Point: 72:6B:D3:36:29:44
          Bit Rate=54 Mb/s   Tx-Power=20 dBm
          Retry short limit:7   RTS thr:off   Fragment thr:off
          Encryption key:off
          Power Management:off
          Link Quality=55/70  Signal level=-55 dBm
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:7339  Invalid misc:83573   Missed beacon:0

lo        no wireless extensions.

eth0      no wireless extensions.
With a grep awk combo i manage to narow it down.
Code:
  iwconfig | grep ESSID |awk '{ /ESSID/; print $4 }'
lo        no wireless extensions.

eth0      no wireless extensions.

ESSID:"APqwerty"
why it also shows lo and eth0 ,i dont know, but ok.

i tried several combo's on grep, awk, even cut.
i only want to catch the ESSID to where i am connected to, in this case APqwerty.
i know i am missing something, but cant find out what it is, any advice ?
 
Old 04-16-2015, 07:54 AM   #2
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Try this:

Code:
iwconfig | grep ESSID |awk '{ /ESSID/; print $4 }' | tail -n 1
 
Old 04-16-2015, 08:03 AM   #3
spatieman
Member
 
Registered: Jan 2004
Location: netherlands, europe, in a crapy house. xD
Distribution: openBSD
Posts: 73

Original Poster
Rep: Reputation: 21
same output ,but tail can help a bit more to!

i want get lost of the lo and the eth0 stuff
that is my isue
thnxs.

Last edited by spatieman; 04-16-2015 at 08:07 AM.
 
Old 04-16-2015, 08:03 AM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,063

Rep: Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857Reputation: 857
the no wireless extensions. errors are being streamed to stderr (not stdout).
you can redirect them to trash by doing something like:
Code:
iwconfig 2> /dev/null | awk '{ /ESSID/; print $4 }'
 
1 members found this post helpful.
Old 04-16-2015, 08:09 AM   #5
spatieman
Member
 
Registered: Jan 2004
Location: netherlands, europe, in a crapy house. xD
Distribution: openBSD
Posts: 73

Original Poster
Rep: Reputation: 21
schneidz !!!
that did the trick !
 
Old 04-16-2015, 08:40 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,438

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
You might want to revisit the awk as currently it is not doing what you think at all
 
Old 04-16-2015, 09:13 AM   #7
spatieman
Member
 
Registered: Jan 2004
Location: netherlands, europe, in a crapy house. xD
Distribution: openBSD
Posts: 73

Original Poster
Rep: Reputation: 21
well, yes and no ,i only want to know the content for ESSID
it gives me ESSID:"APqwerty"
now i need to bring it down to only APqwerty ,after that ,scripting goes easy (normaly that is)

edit xD
my magic line..
Code:
 iwconfig 2> /dev/null | grep ESSID |awk '{ /ESSID/; print $4 }' | sed 's/"//g' | sed 's/ESSID://g'

Last edited by spatieman; 04-16-2015 at 09:27 AM.
 
Old 04-16-2015, 09:23 AM   #8
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
If you get what you are looking for using:
Code:
iwconfig 2> /dev/null | awk '{ /ESSID/; print $4 }'
Then you can use cut or awk to get that value, eg:

Code:
iwconfig 2> /dev/null | awk '{ /ESSID/; print $4 }' | cut -d ":" -f2
Not near my linux system so can't test it.
 
Old 04-16-2015, 09:27 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,656

Rep: Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501
awk -F'[:" ]*' '/ESSID/{print $5}'
 
Old 04-16-2015, 09:38 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,438

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Well pan64 has given it away, but for the others - the below says do nothing when you find the string 'ESSID'
Code:
/ESSID/;
 
Old 04-16-2015, 09:46 AM   #11
spatieman
Member
 
Registered: Jan 2004
Location: netherlands, europe, in a crapy house. xD
Distribution: openBSD
Posts: 73

Original Poster
Rep: Reputation: 21
well, the solution i posted as last did the trick.
script is working.
i go mark it as solved, thank you all for the tips on this litle isue !
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Filter a text with awk, grep or sed Tchiarot Programming 12 02-12-2014 04:06 PM
[SOLVED] Return text, regex, grep, awk... flackend Programming 8 09-30-2011 12:58 AM
awk/sed to grep the text ahpin Linux - Software 3 10-17-2007 12:34 AM
select all text between a patteren using grep mauran Programming 22 07-14-2007 06:30 PM
Extracting text with grep or awk? UrbanDruid Linux - Newbie 5 04-07-2005 02:57 PM


All times are GMT -5. The time now is 02:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration