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

in Categories , , , , last updated December 21, 2016

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.
awk field manipulation

Updated solution

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

echo 'This is a test' | awk '{print substr($0, index($0,$3))}'


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:

while IFS= read -r line
    cut -d ' ' -f3- <<<"$line"
    ### same stuff with awk ###
    ### awk '{print substr($0, index($0,$3))}' <<< "$line" ###
done < "${_input}"

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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Share this on (or read 10 comments/add one below):

10 comment

  1. 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! :-)

  2. 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.

  3. 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.

    Have a question? Post it on our forum!