LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 09-20-2016, 12:39 PM   #1
elalexluna83
Member
 
Registered: Mar 2012
Posts: 55

Rep: Reputation: Disabled
BASH / NETSTAT / SED / AWK Script (ESTABLISHED connections)


Hello Everyone.
Running the next command i got:

Code:
/usr/bin/netstat -neopa | /usr/bin/grep 'ESTABLISHED' | awk '{ printf ("%-20s %-13s %-25s %-8s\n",$9,$1,$4,$5) }' > established.out

cat established.out
20740/firefox        tcp           10.4.4.120:40140          74.125.192.100:443
20740/firefox        tcp           10.4.4.120:49344          173.194.207.136:443
6791/taskldr         tcp           10.5.75.82:41658          50.97.124.80:1352
6390/notes           tcp           10.5.75.82:41743          169.45.4.170:1352
The question here is.
How can i separate port from the ip. i'd like something like ...

Code:
20740/firefox        tcp           10.4.4.120  40140          74.125.192.100   443
20740/firefox        tcp           10.4.4.120  49344          173.194.207.136  443
6791/taskldr         tcp           10.5.75.82  41658          50.97.124.80    1352
6390/notes           tcp           10.5.75.82  41743          169.45.4.170    1352
It will be a plus removing the PID from the process.

Any help will be appreciated.
 
Old 09-20-2016, 12:45 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Consider using sed to replace the first colon with two blanks.

Daniel B. Martin

Last edited by danielbmartin; 09-20-2016 at 12:46 PM. Reason: Clarify wording
 
Old 09-20-2016, 12:57 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
You don't need the grep just have awk perform all the tasks you want. You can use sub, gsub, gensub or split to get rid of the colons and similar to remove the pid
 
Old 09-20-2016, 01:20 PM   #4
elalexluna83
Member
 
Registered: Mar 2012
Posts: 55

Original Poster
Rep: Reputation: Disabled
FOund the solution.

Code:
/usr/bin/netstat -neopa | /usr/bin/grep 'ESTABLISHED' | awk '{ print $9,$1,$4,$5 }' | sed -e 's/\// /g' -e 's/:/  /g' | awk -F " " '{ printf ("%-18s %-17s %-13s %-18s %-20s %-20s\n",$2,$3,$4,$5,$6,$7) }'
Ple share anything faster or better.
 
Old 09-20-2016, 01:52 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Code:
/usr/bin/netstat -neopa | awk '/ESTABLISHED/{sub(/[^a-z]*/,"",$9);sub(/:/,"  ",$4);sub(/:/,"  ",$5);printf ("%-20s %-13s %-25s %-8s\n",$9,$1,$4,$5)}'
 
1 members found this post helpful.
Old 10-12-2016, 04:10 AM   #6
ctsgnb
LQ Newbie
 
Registered: Nov 2012
Posts: 3

Rep: Reputation: Disabled
Just another way ...
Quote:
netstat -neopa | awk '/ESTAB/{split($4,S,":");split($5,T,":");printf ("%-25s %-s7 %-15s %-5s\t%-15s%-5s\n",$9,$1,S[1],S[2],T[1],T[2])}'

Last edited by ctsgnb; 10-12-2016 at 04:11 AM.
 
  


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
Scripting using a Mix of Bash, Awk, and Sed rafir Programming 5 10-12-2011 01:59 PM
[SOLVED] call awk from bash script behaves differently to awk from CLI = missing newlines titanium_geek Programming 4 05-26-2011 09:06 PM
[SOLVED] bash- sed, awk, grep schneidz Programming 10 01-31-2011 08:56 PM
bash script using sed or awk? houms Linux - Newbie 10 06-21-2009 09:14 AM
Help with BASH script and AWK and SED NickJH Linux - Newbie 11 03-07-2009 04:08 PM

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

All times are GMT -5. The time now is 01:13 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
Open Source Consulting | Domain Registration