I'd 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:
echo 'This is a test' | awk '{print substr($0, index($0,$3))}'
OR
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-
OR
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}"
Please note that you can also use Perl, Python or other shell text processing commands to do the same thing.
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop









![Linux Copy File Command [ cp Command Examples ]](http://s13.cyberciti.org/images/shared/rp/3/20.jpg)



{ 5 comments… read them below or add one }
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.
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! :-)
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.
Awk… how I love thee.
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?