LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-17-2012, 01:38 PM   #1
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Rep: Reputation: Disabled
Bash troubles


how would i print name one next to port one? Name 2 next to port 2 and so on.

Im using
Code:
netstat -antp | 
awk -F":" '{print $3}' | 
awk 'NF' | 
awk '{print $1}' > test1.txt && 
netstat -antp | 
awk -F"/" '{print $2}' | 
awk 'NF' | 
grep -Ev "Prog" >> test1.txt; 
cat test1.txt > test2.txt; 
cat test2.txt
Here's is the output:
Code:
80
443
80
80
443
80
80
80
C:\users\Publi
chrome     
C:\users\Publi
chrome     
chrome     
wineserver 
wineserver 
C:\users\Publi
Thanks

Last edited by amboxer21; 05-17-2012 at 02:06 PM.
 
Old 05-17-2012, 08:36 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
I don't really use awk but try this:
Code:
netstat -antp | awk '{port=split($4,a,":"); proc=split($7,b,"/"); print a[port],"\t",b[proc]}'
... fixed

Last edited by kbp; 05-17-2012 at 08:42 PM.
 
1 members found this post helpful.
Old 05-17-2012, 09:36 PM   #3
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Original Poster
Rep: Reputation: Disabled
Thanks kbp. Im going to hit google and learn whats going on. Being I have to tweak that to display other information anyway and split being a new tool. I have never used split before. Thanks.

It was as easy as changing the numbers
Code:
netstat -antp | awk '{port=split($5,a,":"); proc=split($7,b,"/"); print a[port],"\t",b[proc]}'
Even still, I am going to learn split. I'm always on the command line and this would be great to add another tool to my list! Thanks again kbp!

Last edited by amboxer21; 05-17-2012 at 09:41 PM.
 
Old 05-17-2012, 09:44 PM   #4
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
NB. 'split' above is part of awk .. not a separate command.
 
Old 05-17-2012, 09:48 PM   #5
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Original Poster
Rep: Reputation: Disabled
It seems it is:
Code:
awk '{port=split($5,a,":");
Sorry. I guess I over looked that.

I had no idea Awk was so powerful/versitile!

Last edited by amboxer21; 05-17-2012 at 10:19 PM.
 
Old 05-18-2012, 10:25 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Didn't I tell you in your previous thread that you should learn how to use awk? It's not just a program, it's an entire text processing language, and can do everything grep and sed can do and more. The basics of it aren't that hard to learn, although it does take a while to truly master it.

Anyway, here's my version of the same command, with a slightly different process flow.

Code:
netstat -antp | awk '{ sub(/.*:/,"",$5) ; sub(/.*[/]/,"",$7) ; printf("%s\t%s\n",$5,$7) }'
kbp's version uses the split function to divide fields 5 and 7 into arrays, then prints out the final entry of the array. Mine, on the other hand, uses the sub function and a regex to simply strip off everything before the final delimiter in those fields, then a printf statement to output what's left (although the standard print could be used too).

While there's not that much real difference between them, I think mine is a bit cleaner because it avoids the need for storing a bunch of stuff in variables.


gnu awk's string functions are described in detail here:
http://www.gnu.org/software/gawk/man...Functions.html


And here's my list of awk references again:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/

Last edited by David the H.; 05-18-2012 at 10:26 AM.
 
1 members found this post helpful.
Old 05-18-2012, 10:55 AM   #7
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Original Poster
Rep: Reputation: Disabled
I am learning awk @David! I've been using it a lot. Its my favorite tool and I use it as much as I can! I can't learn the whole language in a month man lol cut me some slack. I actually learn for the task at hand. Like this time I am going to learn split.

Haven't gotten around to it yet since last night was spent fixing a friends crashed windows box. He replaced the LCD screen on his laptop and didn't know he had to configure a few things. So he was getting a blue screen. I try and convince him to switch but he's set in his ways.

Anywho, let me stick to kbp's example for now. Your code may be cleaner but his appraoch seems simpler. I do agree, the basics aren't hard at all. There is just so much you can do with it and an endless amount of ways you can get the job done. So, yeah.... let me stick with split for now.

I'm rewritting the original script anyway. I wanted to add, as well as remove content. It needs a bit more functionality too. So, split will not be the only tool I will be learning but it is where I am going to start.

Thanks again for the awesome feed back!

Last edited by amboxer21; 05-18-2012 at 11:03 AM.
 
Old 05-19-2012, 12:22 AM   #8
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Original Poster
Rep: Reputation: Disabled
So @David, I was reading one of the links you have provided and was learning more about Awk's built in variables. I have never had a need for more than FS. So, I came across NF. So I gave my previous issue a go and came up with this solution:
Code:
netstat -antp | awk 'NF="7" {print $5"\t"$7}' | awk 'FS=":" {print $2}' | awk '!x[$0]++' | awk 'NF'
What do you think?

Last edited by amboxer21; 05-19-2012 at 12:33 AM.
 
Old 05-19-2012, 01:14 AM   #9
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
That's a lot of separate processes .. not very efficient. If you were coding a loop that ran say 10000 times the difference would be more noticable.
 
Old 05-19-2012, 01:43 AM   #10
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Original Poster
Rep: Reputation: Disabled
Well couldn't I trim it down by taking the pipes away and using an awk script? It does do the job though. Its only a 12 or 15k text file, nothing big. I just didn't want to take your or @David's code because it would not be mine.

So what should I do to clean it up a bit?
 
Old 05-19-2012, 09:15 AM   #11
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Theres nothing wrong with using other peoples code, thats what libraries are. This is also how we all move forward - by building on the work that people before us have done. Would you go to the extent of writing your own printf function for example ? .. not really.

Personally, I would take Davids code and break it down so you understand exactly what he did, then replicate it yourself or even try to improve it if possible (unlikely I know ). Reading and understanding other peoples code is a great way to learn.
 
Old 05-19-2012, 10:56 PM   #12
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Original Poster
Rep: Reputation: Disabled
Well you have a very valid point there kbp. Improve @David's code haha I wish man! Maybe some day soon though. I am actually going to cut back on the work with AWK for the moment. I am going to work with NASM and GTK some more. Maybe learn ruby or Haskell, who knows. I get bored after awhile and need to switch it up. I am satisfied with the current position my program is at anyway. Well, with the exception of being able to kill unwanted running processes(GTK button needed). So I will come back to this at a later time.

Thanks @kbp and @David!

Last edited by amboxer21; 05-19-2012 at 10:58 PM.
 
Old 05-20-2012, 09:32 AM   #13
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Oh, I don't know about that. It's quite likely that my command could be improved quite a bit. grail is the real awk guru around here, and could probably stroll in here and post a line half as long that runs twice as fast without blinking an eye.

I said almost the exact same thing as kbp in our last thread too. Taking what others have given you, and learning from it and building on it, is quite all right. Indeed, the whole concept of Free Software depends on the concept. We can tell that you aren't trying to just leech off us.

Truthfully, I WANT you to use what I (or kbp) gave you, instead of that chained monstrosity you're using now. If there's one thing true hackers cringe at, it's inefficient code. But of course, don't just stop there, take it in and use it to advance to the next level.

I have the feeling you're going to be one of the ones giving the advice here before too long.
 
Old 08-01-2012, 06:33 AM   #14
PierreB.
Member
 
Registered: Apr 2012
Location: Grenoble
Distribution: Rhel[456],CentOS[56],fedora18
Posts: 33

Rep: Reputation: Disabled
Apparently awk is the tool of choice here (and yes it is a good tools), but just in case, here is a bash only solution, should be quite efficient even in a 10000 read loop :

As one line :
Code:
while read _ _ _ local_Address _ _ Program ; do echo ${Program##*/} uses ${local_Address##*:} ; done <<< "$(netstat -antp)"
Or indented :
Code:
while read _ _ _ local_Address _ _ Program ; do 
  echo ${Program##*/} uses ${local_Address##*:}
done <<< "$(netstat -antp)"
I usually prefer bash only script when possible, but i do believe in tim TOWDI !

Happy scripting to y'all!
 
  


Reply



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] Bash RegExp troubles implosions Linux - Newbie 2 01-21-2012 05:15 AM
BASH Scripting Troubles with IF and FUNCTION semaja2 Linux - Software 10 01-26-2006 02:04 AM
troubles with SATA, troubles with NVIDIA aevangelica Linux - Hardware 6 10-17-2005 02:39 AM
cron bash script troubles zzyzx Slackware 3 04-12-2005 12:27 PM
Newbie troubles with Bash if/then statements jimieee Programming 4 12-04-2003 06:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:34 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