Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When I used this command on Red Hat Linux

/usr/sbin/ss -i

I get the following output below:

State       Recv-Q Send-Q                                            Local Address:Port                                                Peer Address:Port
ESTAB       0      0                                                    <IP_ADD:PORT>                                                  <IP_ADD:PORT>
         ts sack wscale:2,2 rto:204 rtt:4.5/6.5 ato:40 cwnd:3
ESTAB       0      0                                                    <IP_ADD:PORT>                                                  <IP_ADD:PORT>
         ts sack wscale:2,2 rto:213 rtt:13.875/18.5 ato:40
ESTAB       0      0                                                    <IP_ADD:PORT>                                                  <IP_ADD:PORT>
         ts sack wscale:2,2 rto:201 rtt:1.875/0.75 ato:40
ESTAB       0      0                                                    <IP_ADD:PORT>                                                 <IP_ADD:PORT>
         ts sack wscale:9,2 rto:201 rtt:1.875/0.75 ato:40

Whenever I try to pipe grep to that command ex:

/usr/sbin/ss -i | grep <SOME_IP_ADD>

I have this output

ESTAB      0      0              <IP_ADD:PORT>           <IP_ADD:PORT>
ESTAB      0      0              <IP_ADD:PORT>           <IP_ADD:PORT>
ESTAB      0      0              <IP_ADD:PORT>           <IP_ADD:PORT>

Notice that grep didn't include this:

ts sack wscale:2,2 rto:204 rtt:4.5/6.5 ato:40 cwnd:3

because its on another line. So how do I adjust the column width whenever I use this command or other Linux commands for that matter. So that the output will not word-wrap or go to the next line? Are their better ways of doing this?

share|improve this question

2 Answers 2

up vote 5 down vote accepted

If your grep has it, try the -A1 option.

It looks like it is not a case of wrapping, but that the entry is on a separate line.

/usr/sbin/ss -i | grep -A1 <SOME_IP_ADD>

Look at Context Line Control in man grep.

An alternative would be to use

-P Perl-regex 
-z suppress-newline
-o print only matching

as in:

ss -i | grep -Pzo '.*IPADDRESS.*\n.*'

Then you won't get the surrounding dashes which context gives.


An alternative could be sed:

sed -n '/IPADDRESS/{N;p}'
# Or joining the two lines by:
ss -i | sed -n '/IPADDRESS/N;s/\n/ /p'

awk:

awk '/IPADDRESS/{print; getline; print}'
# Or as joined lines:
awk '/IPADDRESS/{printf "%s ", $0; getline; print}'
share|improve this answer
    
Hey thanks sukminder your suggestions worked!!! But I don't know where to give the points though since both of you answered at the same time :s –  dimas Apr 24 '13 at 1:15
    
Yes, – that can be a dilemma but thanks. I'll up-vote @drs ;) –  Sukminder Apr 24 '13 at 1:19

The ts sack ... line is placed below the ESTAB ... line because that is how ss always formats this type of information. It's not being line-wrapped. You can include both lines in grep by using the -A1 flag:

ss -i | grep <IP ADDRESS> -A1

You can combine each match to one line by passing it through a sed command:

ss -i | grep <IP ADDRESS> -A1 | sed '/^--$/d;N;s/\n/ /g'
share|improve this answer
    
Hey thanks, -A1 works perfectly. I don't know where to give the points though since both of you answered at the same time :s –  dimas Apr 24 '13 at 1:14
    
Ps. That sed command does not works as intended (I believe) because of the dashes added by grep in context mode. Try sed '/--/d;N;s/\n/ /' instead. –  Sukminder Apr 24 '13 at 1:31
    
@Sukminder, I missed those. Nice catch. –  drs Apr 24 '13 at 1:48
    
Ah. or sed -e '/^--$/d;N;s/\n/ /' would probably be a good idea. –  Sukminder Apr 24 '13 at 1:49
    
i'll just upvote drs coz sukminder already have a lot of points lol. thanks again –  dimas Apr 24 '13 at 3:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.