Friday, January 18, 2008

HowTo: Print Only Lines of Text File with N characters

Let us assume we have a large text configuration file, and we need to display and print ONLY the lines that has more than 20 characters per line, how can we do that from command line terminal?

Selective Display of Lines From Text File

Here's how to print the lines of a text file that only exceeds the specified N characters.

Say an example text file.
# cat testfile.txt
Result:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1234567890
1234567890123456789012345
welcome to ilovetux.com
the quick red cat jump over the black hole
abc
abcde
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To print ONLY the lines that has at least 20 characters per line, this could be done like so

# cat testfile.txt | awk 'length > 19'
Result:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1234567890123456789012345
welcome to ilovetux.com
the quick red cat jump over the black hole
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Enjoy.

Related Readings:
Print and Display Leading or Trailing Lines from Text File
Display FileName Extension Only
Deleting New and Return Lines from Text File

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy