ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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:
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.
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.
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.
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.
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 !
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.