Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show particular column for command [duplicate]

I want to show only specific columns for all the records in the command.

Example: docker ps shows me data in 10 columns. It can have space in between the column headers. My requirement is to get only 2-4 columns in some sequence.

Is there a direct way to do that in any of the commands that respond in a tabular way?

I am new to Linux and thinking if this can be feasible. Thanks.


For example:

$ docker ps
CONTAINER ID  IMAGE   COMMAND     CREATED             NAMES
123           image   "ABC DEF"   10 hours ago        image ABC

Here in the above scenario, CONTAINER ID is one header column but there is space, and in the row for NAMES columns can have space in between.

Using AWK, CUT etc it is trickier to write a common script for all the commands because they work on "space" logic.

like image 943
stackjohnny Avatar asked Dec 22 '18 14:12

stackjohnny


People also ask

How do I find duplicates in a column in SQL?

One way to find duplicate records from the table is the GROUP BY statement. The GROUP BY statement in SQL is used to arrange identical data into groups with the help of some functions. i.e if a particular column has the same values in different rows then it will arrange these rows in a group.

How do I show only duplicate values in Excel?

Select one or more cells in a range, table, or PivotTable report. On the Home tab, in the Styles group, click the arrow for Conditional Formatting, and then click Manage Rules to display the Conditional Formatting Rules Manager popup window. Under Select a Rule Type, click Format only unique or duplicate values.

How do I filter duplicates in Excel?

Select the range of cells, or make sure that the active cell is in a table. On the Data tab, in the Data Tools group, click Remove Duplicates. Select one or more of the check boxes, which refer to columns in the table, and then click Remove Duplicates.


1 Answers

Piping the output to tr and cut:

docker ps | tr -s " " | cut -d " " -f 2-4

-s flag on tr squeezes characters and leaves only one " "

-d on cut tells to split field by " ".

like image 191
Gonzalo Matheu Avatar answered Sep 27 '22 18:09

Gonzalo Matheu