Loading...
X

How to print from specific column to last in Linux command line

In this note, we will consider how to display from a specific column to the last one. For example:

  • how to output from second column to last
  • how to display from third column to last
  • how to display from the fourth column to the last
  • how to output from nth column to last
  • how to display the last column

To display from a certain column to the very last, the “cut” command is most convenient. If for some reason you prefer the “awk” command, then here will be shown examples of output from a specific column to the last using the awk command.

cut

How to output from the nth column to the last one:

Use the following command, in which “N” is replaced by the number of the column from which you want to start the output:

cut -fN- -d' '

How to output from the second column to the last:

cut -f2- -d' '

Example:

echo 'vddp vddpi vss cb0 cb1 cb2 cb3 ct0 ct1 ct2 ct3' | cut -f2- -d' '

How to output from the third column to the last:

cut -f3- -d' '

Example:

echo 'vddp vddpi vss cb0 cb1 cb2 cb3 ct0 ct1 ct2 ct3' | cut -f3- -d' '

How to display from the fourth column to the last:

cut -f4- -d' '

Example:

echo 'vddp vddpi vss cb0 cb1 cb2 cb3 ct0 ct1 ct2 ct3' | cut -f4- -d' '

awk

How to output from the nth column to the last one:

In awk you can use a construct like:

awk '{$1=$2=$3="";print $0}'

This example will clear the contents of the first three columns and show all columns starting from the fourth column.

But at the same time, the spaces that are between the first and second columns, the second and third columns, as well as the third and fourth columns will not be removed and will be shown. To remove them, additionally use the “sed” command:

awk '{$1=$2=$3="";print $0}' | sed -r "s/^\s+//g"

Or you can use the following awk command to clean up spaces between deleted columns:

awk '{$1=$2=$3=""; $0=$0; $1=$1; print}'

How to output from the second column to the last:

awk '{$1="";print $0}' | sed -r "s/^\s+//g"
awk '{$1=""; $0=$0; $1=$1; print}'

Examples:

echo 'vddp vddpi vss cb0 cb1 cb2 cb3 ct0 ct1 ct2 ct3' | awk '{$1="";print $0}' | sed -r "s/^\s+//g"
echo 'vddp vddpi vss cb0 cb1 cb2 cb3 ct0 ct1 ct2 ct3' | awk '{$1=""; $0=$0; $1=$1; print}'

How to output from the third column to the last:

awk '{$1=$2="";print $0}' | sed -r "s/^\s+//g"
awk '{$1=$2=""; $0=$0; $1=$1; print}'

Examples:

echo 'vddp vddpi vss cb0 cb1 cb2 cb3 ct0 ct1 ct2 ct3' | awk '{$1=$2="";print $0}' | sed -r "s/^\s+//g"
echo 'vddp vddpi vss cb0 cb1 cb2 cb3 ct0 ct1 ct2 ct3' | awk '{$1=$2=""; $0=$0; $1=$1; print}'

How to display from the fourth column to the last:

awk '{$1=$2=$3="";print $0}' | sed -r "s/^\s+//g"
awk '{$1=$2=$3=""; $0=$0; $1=$1; print}'

Examples:

echo 'vddp vddpi vss cb0 cb1 cb2 cb3 ct0 ct1 ct2 ct3' | awk '{$1=$2=$3="";print $0}' | sed -r "s/^\s+//g"
echo 'vddp vddpi vss cb0 cb1 cb2 cb3 ct0 ct1 ct2 ct3' | awk '{$1=$2=$3=""; $0=$0; $1=$1; print}'

How to display the last column

To display the last column with awk, use the command:

awk '{print $NF}'

For example:

echo 'vddp vddpi vss cb0 cb1 cb2 cb3 ct0 ct1 ct2 ct3' | awk '{print $NF}'

If you want to do without awk, then use the following set of commands:

rev | cut -d' ' -f1 | rev

For example:

echo 'vddp vddpi vss cb0 cb1 cb2 cb3 ct0 ct1 ct2 ct3' | rev | cut -d' ' -f1 | rev

Leave Your Observation

Your email address will not be published. Required fields are marked *