Lum – How to adjust column width in Linux command output

columnsgreptext processing

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?

Best Answer

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}'
Related Question