This is a test.
Unix is Best.
No Linux is the Best.
Space in simple understanding is an area or volume.
Outer space .
I need the output:
Unix is Best.
Outer space .
| Tutorial details | |
|---|---|
| Difficulty | Easy (rss) |
| Root privileges | No |
| Requirements | awk and bash |
| Estimated completion time | N/A |
How do I print all lines that have three (3) words only?
The awk command is well suitable for this kind of pattern processing text file. Awk set the variable called NF. It is set to the total number of fields in the input record. So if NF equal to three print the line. The syntax is as follows:
awk '{ if ( NF == 3 ) print } ' /path/to/input
It is also possible to emulate awk command output using a shell script while loop and IFS (internal field separator) in loops:
#!/bin/bash # AWK NF if condition (awk '{ if ( NF == 3 ) print } ' $_input) emulation using bash # Author: nixCraft <www.cyberciti.biz> # ----------------------------------------------------------------------------------- _input="/path/to/data.txt" _word=3 while IFS= read -r line do set -- $line [ $# -eq $_word ] && echo "$line" done < "$_input"
Sample outputs:
Unix is Best. Outer space .
- 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












{ 2 comments… read them below or add one }
Since the newer awks “print” by default, you can just do:
awk ‘NF ==3′ filename
Hi Seshadri,
It should be awk ‘NF == 3′ filename. Yes it will work and no need to use printf too.