LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 08-15-2009, 11:24 PM   #1
okos
Member
 
Registered: May 2007
Location: California
Distribution: Slackware/Ubuntu
Posts: 609

Rep: Reputation: 37
Pipe a variable using grep in a script.


Hi
I am not sure how to ask this.
$NMBSCAN has the following info pigeonholed into it:
Code:
nmbscan version 1.2.5 
  domain MSHOME
  master-browser MOM 192.168.2.98 -
  server MOM
    ip-address 192.168.2.98
      mac-address 
    server-software Windows 2000 LAN Manager
    operating-system Windows 5.1
What I am trying to do is filter the results of one variable and put it into another variable. Namely, I am trying to obtain the server name and ip address. I tried a bunch of different variations but nothing seems to work.

Here are a few of variations that do not produce the results that I am looking for.
Code:
SETARRAY=$[ echo "$NMBSCAN | grep -B1 'ip-address' | awk '{print $2}'"]
Code:
SETARRAY=$[$NMBSCAN | grep -B1 'ip-address' | awk '{print $2}']
Code:
SETARRAY=( $NMBSCAN | grep -B1 'ip-address' | awk '{print $2}' )
I am not sure what is wrong with my syntax. Any suggestions?

Last edited by okos; 08-15-2009 at 11:29 PM.
 
Old 08-15-2009, 11:55 PM   #2
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140
Try
Code:
SETARRAY=( $(echo "$NMBSCAN" | grep -B1 'ip-address' | awk '{print $2}') )
 
Old 08-16-2009, 12:13 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
How about using sed instead?
Code:
SETARRAY=( $(echo "$NBSCAN"|sed -rn '/ip-address/ s/.*[ ](.*)/\1/p') )
 
Old 08-16-2009, 03:41 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Or it can be done within the shell which will run faster FWIW
Code:
#!/bin/bash
shopt -s extglob

NMBSCAN='nmbscan version 1.2.5 
  domain MSHOME
  master-browser MOM 192.168.2.98 -
  server MOM
    ip-address 192.168.2.98
      mac-address 
    server-software Windows 2000 LAN Manager
    operating-system Windows 5.1'
buf="${NMBSCAN#*server }"
server="${buf%%+([$IFS])*}"
buf="${buf#*ip-address }"
ip="${buf%%+([$IFS])*}"
echo "server: '$server', ip-address: '$ip'"
 
Old 08-16-2009, 11:28 PM   #5
okos
Member
 
Registered: May 2007
Location: California
Distribution: Slackware/Ubuntu
Posts: 609

Original Poster
Rep: Reputation: 37
Guys, thanks a bunch for the input.
I think catkin's suggestion would probably work the best. However it is a little advanced for me. I did not know that you could select sections of what was printed and put it in a variable other then using such commands like grep or awk.

Could you explain in detail what you are doing? Thanks
Quote:
Originally Posted by catkin View Post
Or it can be done within the shell which will run faster FWIW
Code:
#!/bin/bash
shopt -s extglob

NMBSCAN='nmbscan version 1.2.5 
  domain MSHOME
  master-browser MOM 192.168.2.98 -
  server MOM
    ip-address 192.168.2.98
      mac-address 
    server-software Windows 2000 LAN Manager
    operating-system Windows 5.1'
buf="${NMBSCAN#*server }"
server="${buf%%+([$IFS])*}"
buf="${buf#*ip-address }"
ip="${buf%%+([$IFS])*}"
echo "server: '$server', ip-address: '$ip'"
 
Old 08-17-2009, 02:16 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,498

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
It is parameter substitution. Take a look at http://www.tldp.org/LDP/abs/html/par...stitution.html. Basically the buf= statements remove the "*server " and "*ip-address " part from the front end of $NMBSCAN. The server= and ip= statements remove every space/tab/newline from the back end of the line and the rest of the content of $buf. IFS is a bash variable, specifying the Input Filed Separator. In this case it has been used as a character list for space/tab/newline.
 
Old 08-17-2009, 02:31 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by colucix View Post
It is parameter substitution.
Beautifully explained, colucix
 
  


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
TcpDump / Grep / Pipe to file Reefcrazed Linux - Networking 7 06-23-2010 07:46 PM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 10:31 PM
grep a shell script variable contents kushalkoolwal Programming 8 02-04-2009 06:15 AM
Pipe inside variable isn't working in bash Reginald0 Linux - General 5 01-30-2004 09:43 AM


All times are GMT -5. The time now is 03:14 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration