img

awk / cut: Skip First Two Fields and Print the Rest of Line

September 8, 2019

I would like to skip first two or three fields at the the beginning of a line and print the rest of line. Consider the following input:
    This is a test
    Giving back more than we take

I want my input file with the following output:
    a test
    more than we take

How do I printing lines from the nth field using awk under UNIX or Linux operating systems?

You can use the awk command as follows. The syntax is as follows to skip first two fields and print the rest (thanks danliston):
awk '{ $1=""; $2=""; print}' filename
To skip first three fields, enter:
awk '{ $1=""; $2=""; $3=""; print}' filename
You can specify the input field separator too. In this example use ‘:’ as the the input field separator:
awk -F':' '{ $1=""; $2=""; print}' filename

Old solution

My old solution to skip first two fileds and print the rest of line:
echo 'This is a test' | awk '{print substr($0, index($0,$3))}'
You can also use substr:
substr(s, i [, n])
It return the at most n-character substring of s starting at i. If n is omitted, use the rest of s. So:
awk '{print substr($0, index($0,$3))}' <<< 'This is a test'
You can also use the cut command:
echo 'This is a test' | cut -d ' ' -f3-
cut -d ' ' -f3- <<<'This is a test'

Finally, process the file using bash while loop:

#!/bin/bash
_input="/path/to/file.name.txt"
while IFS= read -r line
do
    cut -d ' ' -f3- <<<"$line"
    ### same stuff with awk ###
    ### awk '{print substr($0, index($0,$3))}' <<< "$line" ###
done < "${_input}"

How to create HTML table using awk

Say you have data as follows in a text file called data.txt:

-v	Show version
-d	Set delta value
-o	Set output file name
-i	Set input filename for this command

To create html table code run:

awk 'BEGIN{ print "<table>"}
{ printf "<tr><td>%s</td><td>", $1; $1=""; print $0 "</td></tr>"}
END{ print "</table>"}' data.txt

Sample session:

Please note that you can also use Perl, Python or other shell text processing commands to do the same thing.

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Article Tags:
Article Categories:
How To

Comments are closed.