LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-08-2009, 01:50 PM   #16
utahnix
Member
 
Registered: Dec 2006
Location: Utah, USA
Distribution: openSUSE
Posts: 72

Original Poster
Rep: Reputation: 15

I have actually used awk, sed, grep, head, tail, cut and many of those utilities before. But I haven't used some of them in a long time, and as you know, it's easy to forget things after a long while.

Okay, well if I use head to grab the first line, that gets me somewhere (I use head, but usually without parameters - I forgot it could do that). How would you suggest I yank PCNAME from that line?

I tried using cut, but I was left with white space on the left and right of the word. I tried using sed to nuke the white spaces that cut left behind, but it wasn't working. I thought maybe awk would be the way to go (instead of cut/sed), but I couldn't get it to do what I wanted either. I tried perl, but my perl skills are so rusty I couldn't remember how to get what I wanted in a one-liner. It's just been too long since I've dabbled with some of these utilities.

So assume I'm down to one line. What would be your suggestion in terms of yanking that PCNAME from that line? I need it in a BASH variable so I can print it later with some other info.

And again, thanks to everyone for the help! You're all awesome.
 
Old 10-08-2009, 08:54 PM   #17
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Like I said, awk handles whitespaces automatically
Code:
t="      PCNAME          <00> -         M <ACTIVE>"
echo $t|awk '{ print $1 }'
PCNAME
 
Old 10-08-2009, 10:01 PM   #18
utahnix
Member
 
Registered: Dec 2006
Location: Utah, USA
Distribution: openSUSE
Posts: 72

Original Poster
Rep: Reputation: 15
I think I have the info I need now. The script seems to be doing what it needs to do. Thanks all of you for the help... it truly is appreciated!
 
Old 10-09-2009, 03:26 AM   #19
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by utahnix View Post
I have actually used awk, sed, grep, head, tail, cut and many of those utilities before. But I haven't used some of them in a long time, and as you know, it's easy to forget things after a long while.
Jup human brain is just not what it's suppose to be
I just wanted to scotch the upcoming battle of awk vs sed and whats best for what.

While you now have your script ready and good just a bit of hint. Just use man program name from time to time. It always provided me with some additional ideas how to solve things.
 
Old 10-09-2009, 11:00 AM   #20
utahnix
Member
 
Registered: Dec 2006
Location: Utah, USA
Distribution: openSUSE
Posts: 72

Original Poster
Rep: Reputation: 15
Oh, I've used man pages many times before. I'm not a Linux newbie. But most of what I've done the past few years is more systems administration, which hasn't included sed or awk. As I said, I've used perl quite a bit in the past too, but it's been quite a long time since then.

The man pages, in my opinion anyway, are often hit and miss. Sometimes they are extremely helpful, and in other cases, they're very little help. And in the case of sed vs awk (which have larger man pages), my schedule is so fragmented that I end up having to re-read sections because I can't remember what I read from my last 'session.'

In any event, the script does what it is meant to do, and all is good. I've added code that handles queries from nmblookup which don't produce results, and the script handles those IPs accordingly.

All in all, I do appreciate the assistance from all of you.
 
Old 10-09-2009, 01:34 PM   #21
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by utahnix View Post
In any event, the script does what it is meant to do, and all is good. I've added code that handles queries from nmblookup which don't produce results, and the script handles those IPs accordingly.
What does it look like? I'm intrigued by all the talk of sed and awk when a pure bash solution was given here (and it worked after zhjim had fixed the typos)
 
Old 10-12-2009, 03:59 AM   #22
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by utahnix View Post
The man pages, in my opinion anyway, are often hit and miss. Sometimes they are extremely helpful, and in other cases, they're very little help. And in the case of sed vs awk (which have larger man pages), my schedule is so fragmented that I end up having to re-read sections because I can't remember what I read from my last 'session.'
I am totaly with you in this point. sed and awk man pages are nearly good for nothing. But most man pages will give you a good idea what purpose they have and that most of the time in the first paragraph.
As it's always hard to predict what level of skill/knowledge a person has it did not wanted to insult you. Man pages are for me just like a quick talk to a co-worker about a problem. 2 minutes of talking/reading can save hours of work.
 
Old 10-12-2009, 04:01 AM   #23
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by catkin View Post
What does it look like? I'm intrigued by all the talk of sed and awk when a pure bash solution was given here (and it worked after zhjim had fixed the typos)
I guess there are just hosts that don't have a netbios name and so return an emtpy set.
And truly spoken I like your solution best cause it's pure bash and using only one external programm. This got array usage in bash on my list
 
Old 10-12-2009, 04:28 AM   #24
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by zhjim View Post
I guess there are just hosts that don't have a netbios name and so return an emtpy set.
Can you post the nmblookup output for one of those?
 
  


Reply

Tags
bash, extract, hostname, nmap, script, variable



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help on my Linux Homework ! bash shell script / input-output data etc Bebelindo Programming 2 03-03-2009 12:51 PM
run script to extract data to put to web cghcgh Programming 9 06-10-2008 02:11 AM
awk/perl script for extract data mruknown1 Programming 3 09-11-2007 09:19 AM
awk/perl script for extract data mruknown1 Linux - Newbie 2 09-11-2007 04:42 AM
How to extract data and for loop it into an array? (shell) WeiSomething Programming 6 11-17-2006 03:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:46 AM.

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
Open Source Consulting | Domain Registration