LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-22-2022, 12:42 PM   #1
csj79
LQ Newbie
 
Registered: Aug 2022
Posts: 17

Rep: Reputation: 0
unable to extract specific string in output


Hello Everyone,

I am trying to extract a specific string (3128) from the command below. I am actually trying to extract only port number 3128 from the output. How can I proceed?

ss -tlpn | grep squid | awk '{print $4}'

Running the above command outputs *:3128

I would like to have the command only print 3128 . How do I proceed?

I am using ubuntu 22.04.

Let me know if you have any questions.

Cheers,
 
Old 11-22-2022, 01:03 PM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,599

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546

Use sub() to replace it.

You can either remove the explicit *: part, or remove "everything not a digit", like so:
Code:
awk '/squid/ {sub("\*:","",$4);print $4}'
Code:
awk '/squid/ {sub("[^0-9]*","",$4);print $4}'
note how there's a filter for squid, removing the need for grep.

 
Old 11-23-2022, 08:43 AM   #3
csj79
LQ Newbie
 
Registered: Aug 2022
Posts: 17

Original Poster
Rep: Reputation: 0
thank you for your response. I was able to find a simpler way of doing it . See below. I didn't know that grep could print exactly what you want with the -o option

ss -tlpn | grep -i squid | grep -o 3128
 
Old 11-23-2022, 09:04 AM   #4
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,599

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546

If you already know the port number then just filter ss on it directly!
Code:
ss -tlpn src :3128 or dst :3128
What precisely are you trying to achieve?

Maybe you actually just want "systemctl status squid" or "pgrep squid" ?

 
1 members found this post helpful.
Old 11-23-2022, 10:18 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I agree with boughtonp, if you already know all the information, what is the point?
 
Old 11-23-2022, 10:23 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
Code:
ss -tlpn | grep 'squid.*3128'
probably will do the same, but as it was mentioned this is not the right way to check if squid is working properly.
 
Old 11-23-2022, 12:15 PM   #7
csj79
LQ Newbie
 
Registered: Aug 2022
Posts: 17

Original Poster
Rep: Reputation: 0
thanks boughtonp . I was actually trying to see how I can extract info from a cvs like file or extract a string for the ss output and display only the port number.
during my research on it, I cam across the grep -o option and also the awk as well.

Thanks again for all you help
 
Old 11-23-2022, 10:48 PM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
CSV (aka RFC 4180) is a surprisingly complex format. There is a new version of AWK in the works which can parse it, but it may be a while off. Otherwise you're better off going with Perl or Python.

Based on the example in the manual page:

Code:
#!/usr/local/bin/perl                                                           

use Text::CSV;
use warnings;
use strict;

my @rows;

# Read/parse CSV                                                                
my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1 });

open(my $fh, "<:encoding(utf8)", "test.csv")
    or die "test.csv: $!";

while (my $row = $csv->getline ($fh)) {
    # check 3rd field for a pattern
    $row->[2] =~ m/pattern/ or next;                   
    push(@rows, $row);
}

close $fh;

print join("\n", @rows), "\n";

exit(0);
 
  


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
How to capture 1000 lines before a string match and 1000 line a string match including line of string match ? sysmicuser Linux - Newbie 12 11-14-2017 05:21 AM
In if test the list contains only one specific string or multiple same specific string Mike_Brown Linux - Newbie 1 01-27-2016 10:52 PM
extract a string within a string using a pattern adshocker Linux - Newbie 1 11-04-2010 10:44 PM
[SOLVED] copy string a to string b and change string b with toupper() and count the chars beep3r Programming 3 10-22-2010 07:22 PM
read string after specific string from a text file using C++ programing language badwl24 Programming 5 10-08-2009 05:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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