• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / AWK / How to Get a Specific Line or Lines From a Big File? Hint: Use One Liner AWK

How to Get a Specific Line or Lines From a Big File? Hint: Use One Liner AWK

December 21, 2011 by cmdlinetips

AWK One Liner to Get a Specific Line From a Huge Text FileIf you are working with a huge text file with millions of lines and want to know the content of a specific line, there are a few tricks you can play to get the line you want.  The naive approach of reading the file line by line (say in python) and keeping a count on the lines is very slow,  time consuming, and not convenient.

That is where the good old “AWK” (or SED) comes in.   AWK is created to deal with text files before the Perl or Python scripting era.  The name AWK is after the first letters AWK’s creators Alfred Aho, Peter Weinberger, and Brian Kernighan.

Extracting any specific line of interest is just a line of code using AWK from the terminal, if you know the line number or a specific pattern unique to the line.

Let us say you want the line “lineNumber=23482364” from a huge text file “myHugeTextFile.txt”, the awk script to get the line and display on the terminal is

awk ‘NR == lineNumber’ myHugeTextFile.txt

Here, NR is short for “Number of Records” and refers to the line number of a file. As you can easily see, the above awk script can be modified to do more things.

For example, if you wanted to get all lines between a range of line numbers, the awk script can be modified by adding more conditions as

awk ‘NR >= lineNumber1 && NR <= lineNumber2’  myHugeTextFile.txt

If you are interested in learning more of awk, check out the e-book by catonmat.net and the blog post series on awk.

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow to Count the Number of lines, Words, and, Characters in a Text File from Terminal? Default ThumbnailHow To Loop Through Lines in a File in Bash? Default Thumbnail3 Ways to Read A Text File Line by Line in Python Default ThumbnailHow to Fold a Long Single Line Text to Multiple Lines (in Python and Linux)?

Filed Under: AWK, AWK Tips, Extract a Line from Terminal, Linux Tips Tagged With: AWK Tips, Extract a Line from Terminal, Linux Tips

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version