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

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

ADVERTISEMENTS

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:
awk cut Skip First Two Fields and Print the Rest of Line
Please note that you can also use Perl, Python or other shell text processing commands to do the same thing.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
10 comments… add one
  • daixtr Sep 1, 2011 @ 16:49

    thanks.. this is the most elegant, simplest and my preferred way of doing this. The other way of doing this is with awk’s NF variable in a for-loop. This non-trivial situation is best explained in http://awk.freeshell.org/RangeOfFields.

  • James Valentine Jan 13, 2012 @ 12:30

    Very useful, thanks. I went for the first Awk option to trim the permissions and filesize off the output of ls -l

    Didn’t know about cut. Time to read the man page! :-)

  • jfaske Oct 3, 2012 @ 13:44

    The awk method works perfectly well if the first three fields are unique. However, if your string was as follows:

    “This This This will break the awk method”

    …. the output will be unchanged since when it indexes the third field, it finds the first.

    The cut method works perfectly well as long as your fields are not separated by more than a single space. It treats multiple spaces as separate fields.

  • John Murray Nov 21, 2012 @ 13:31

    Awk… how I love thee.

  • frhling Dec 10, 2012 @ 12:20

    If in 9th field($9) there is sth like: A/B/C and one needs C. how can we extract C and put it in output?

  • danliston Apr 16, 2014 @ 0:08

    I see this thread is a few years old, but I could not resist adding for the sake of searching the internet for a simple (but less elegant) answer.

    awk '{ $1=""; $2=""; print}' filename

    The above simply uses the default field separator, replaces the content of the first two fields with “nothing”, and prints the modified line. No need to use redirection, awk expects a file as input.

    Note1: The field separators are left in tact, so your output lines begin with two blank spaces. This can be compensated for using substr($0,3) behind the print command.

    Note2: The advantage to this syntax, is “any” field(s) may be ommitted, not just the first X number of fields.

    • Muthu Dec 20, 2016 @ 12:04

      Excellent Mr. danliston , this was easy and powerful.
      Appreciate your comments.

    • 🐧 Vivek Gite Dec 21, 2016 @ 9:30

      Thanks. The faq has been updated.

    • Raf Apr 6, 2017 @ 15:49

      The with this approach is that it prints leading spaces – the substr() way doesn’t.

  • Sarthak Jun 4, 2014 @ 18:49

    sed -n ‘3,$’p infile.txt

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.