Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extracting specific lines from a text file

Tags:

bash

sed

awk

I have a data in my .txtfile as below, I want to extract the line that have value as 12 and copy it into new .txt file. I tried with sed but could get the result, any help would be appreciated . Thanks

"944760 1939"   10
"944760 1940"   12
"946120 1940"   2
"946370 1939"   10
"946370 1940"   12
"946460 1940"   6
"946530 1939"   10
like image 206
saikiran Avatar asked Feb 22 '18 03:02

saikiran


People also ask

Which command is used to extract specific lines records from a file?

The cut command offers many ways to extract portions of each line from a text file. It's similar to awk in some ways, but it has its own advantages and quirks. One surprisingly easy command for grabbing a portion of every line in a text file on a Linux system is cut.

How do I extract a specific line from a text file in Python?

Use readlines() to Read the range of line from the File You can use an index number as a line number to extract a set of lines from it. This is the most straightforward way to read a specific line from a file in Python. We read the entire file using this way and then pick specific lines from it as per our requirement.

How do I display a specific line in a file in Linux?

Using the head and tail Commands Let's say we want to read line X. The idea is: First, we get line 1 to X using the head command: head -n X input. Then, we pipe the result from the first step to the tail command to get the last line: head -n X input | tail -1.


1 Answers

Why don't just search forum, before posting here, so many posts repeated

awk '$3 == 12' infile > outfile

which is same as

awk '$3 == 12 { print }' infile > outfile 

Explanation

  • $3 == 12 if 3rd column is equal to 12, print such record/row
like image 170
Akshay Hegde Avatar answered Nov 15 '22 05:11

Akshay Hegde