column


bash: Simple way to get n-th column

Using cut you can select any column and define a custom delimiter to support multiple input formats you can select a column (or more) with barely minimum code.

cut -d',' -f2 myFile.csv

The above command will read the file myFile.csv (which is a CSV file) break it down to columns using the ‘,‘ character and then get the second column.

The option -f specifies which field (column) you want to extract, and the option -d specifies what is the field delimiter (column) that is used in the input file.

The -f parameter allows you to select multiple columns at the same time. You can achieve that by defining multiple columns separated using the ‘,‘ and by defining ranges using the - character.

Examples

  • -f1 selects the first column
  • -f1,3,4 selects columns 1, 3 and 4
  • -f1-4 selects all columns in the range 1-4
  • -f1,3,5-7,9 selects columns 1,3,8 and all the columns in the range 5-7

Use awk to print the last N columns of a file or a pipe

In this post we will describe a way to print the last N number of columns in awk.

We will use this code as example, where we will print the last 2 columns only:


awk '{n = 2; for (--n; n >= 0; n--){ printf "%s\t",$(NF-n)} print ""}';
'

In the awk script we use the variable n to control how many columns we want to print. In the above example we initialized it  to the value 2 as that is the number of columns we want printed.

After, we use a for loop to iterate over the fields (in this case the last two fields) and we print them to the screen using printf "%s\t",$(NF-n) to avoid printing the new line character and to separate them with a tab character.

NF is a special variable in awk that holds the total number of fields available on that line. If you do not change the delimiter, then it will hold the number of words on the line.

$(NF-n) is the way we ask awk to gives us the variable value that is n places before the last.

Outside the loop we print "" to print the new line character between input rows.

Examples:

If we want to print the last two columns of the ls -l command we can do it as follows:


ls -l | awk '{i = 2; for (--i; i >= 0; i--){ printf "%s\t",$(NF-i)} print ""}';

If we want to print the last two columns of the /etc/passwd file we can do it as follows:


awk -F ':' '{i = 2; for (--i; i >= 0; i--){ printf "%s\t",$(NF-i)} print ""}' /etc/passwd;

Note that we change the delimiter with the command line argument -F ":"


Create a sortable ‘Modified Date’ sortable column for posts and pages in wordpress admin area 7

2016-07-14: Post updated to support both pages and posts without redundant/useless code

Paste the following in the functions.php file of your theme:

// Register the column for modified date
function bf_post_modified_column_register( $columns ) {
    $columns['post_modified'] = __( 'Modified Date', 'mytextdomain' );
    return $columns;
}
add_filter( 'manage_edit-post_columns', 'bf_post_modified_column_register' );
add_filter( 'manage_edit-page_columns', 'bf_post_modified_column_register' );

// Display the modified date column content
function bf_post_modified_column_display( $column_name, $post_id ) {
    if ( 'post_modified' != $column_name ){
        return;
    }
    $post_modified = get_post_field('post_modified', $post_id);
    if ( !$post_modified ){
        $post_modified = '' . __( 'undefined', 'mytextdomain' ) . '';
    }
    echo $post_modified;
}
add_action( 'manage_posts_custom_column', 'bf_post_modified_column_display', 10, 2 );
add_action( 'manage_pages_custom_column', 'bf_post_modified_column_display', 10, 2 );

// Register the modified date column as sortable
function bf_post_modified_column_register_sortable( $columns ) {
    $columns['post_modified'] = 'post_modified';
    return $columns;
}
add_filter( 'manage_edit-post_sortable_columns', 'bf_post_modified_column_register_sortable' );
add_filter( 'manage_edit-page_sortable_columns', 'bf_post_modified_column_register_sortable' );

When you refresh http://<Your Domain>/wp-admin/edit.php or http://<Your Domain>/wp-admin/edit.php?post_type=page the ‘Modified Date’ column will be visible and sortable.


Get the first column of a file in bash

awk '{print $1}' someFile;

The command awk '{print $1}' someFile is a command that is used to extract specific data from a file in Unix/Linux systems. It uses the awk scripting language, which is a powerful tool for text processing and data manipulation.

The syntax of the command is as follows: awk ‘{print $1}’ someFile. Here, the awk command is followed by a set of instructions in single quotes. The instructions specify what to do with the data in the file someFile. In this case, the instruction is print $1 which means that awk will print the first field or column of each line in the file.

The $1 in the instruction refers to the first field of each line in the file. Fields in an awk file are separated by whitespace or any other specified delimiter. In this case, the default delimiter is whitespace, so each field is separated by a space or tab.

The someFile in the command is the name of the file that the awk command will process. The file can be any text file and can contain any type of data. The awk command will extract the first field of each line in the file and print it on the screen.

In conclusion, the command “awk ‘{print $1}’ someFile” is a powerful tool for extracting specific data from a file in Unix/Linux systems. The awk scripting language provides a flexible and efficient way to process text data and manipulate it to meet specific requirements.

*NOTE: If you want the second column change $1 to $2 etc. $1 can be replaced by a variable and used in a more elaborate way that applies to more cases/problems.