Grep: Saving rows containing a specific row in a specific column

I am trying to highlight rows that have a specific value in a specific column and store that in the output. I am trying to do this using grep. Is it possible?

My data looks like this:

apple   5   abcdefd  ewdsf
peach   5   ewtdsfe  wtesdf
melon   1   ewtedf   wersdf
orange  3   qqqwetr  hredfg

      

I want to highlight the lines that have a value of 5 on its second column and save it in a new output file.

apple   5   abcdefd  ewdsf
peach   5   ewtdsfe  wtesdf

      

I would be grateful for your help!

+3


source to share


5 answers


It may be possible with the help grep

, but definitely a suitable tool for performing this operation awk

. You can filter every row with 5 in the second column with

awk '$2 == 5'

      

Description

awk

separates it into entries in records (usually a row) and fields (usually a column) and performs actions on records that meet certain conditions. Here

awk '$2 == 5'

      

is the short form for

awk '$2 == 5 {print($0)}'

      

which translates to



For each record, if the second field ($2) is 5, print the full record ($0).

      

Options

If you need to dynamically select the key value used to filter your values, use the option -v

awk

:

awk -v "key=5" '$2 == key {print($0)}'

      

The field separator is a regular expression that determines what text separates the columns, and can be changed using a field -F

. For example, if your data was in a base CSV file, the filter would be

awk -F", *" '$2 == 5'

      

Visit the awk tag wiki for some helpful information to get you started awk

.

+6


source


To print when the second field is 5

, use:awk '$2==5' file



+4


source


Try:

grep '^[^\s]\+\s5.*$' file.txt

      

pattern looks for the beginning of a string, followed by more than one nonspatial character, followed by a space, and then 5, followed by any number of characters, followed by an eol.

0


source


You can get the following command.

$ cat data.txt
apple   5   abcdefd  ewdsf
peach   5   ewtdsfe  wtesdf
melon   1   ewtedf   wersdf
orange  3   qqqwetr  hredfg
grape   55  kkkkkkk  aaaaaa

$ grep -E '[^ ]+ +5 .*' data.txt > output.txt

$ cat output.txt
apple   5   abcdefd  ewdsf
peach   5   ewtdsfe  wtesdf

      

You can only get the answer with the command grep

. But I highly recommend that you use the command awk

.

0


source


A simple way to do it:

grep '5' MyDataFile

      

Result:

apple   5   abcdefd  ewdsf
peach   5   ewtdsfe  wtesdf

      

To write this to a new file:

grep '5' MyDataFile > newfile

      

Note. which will find 5 in MyDataFile. To limit the second column, a short script suits your needs. If you want to restrict it to only the second column then a quick script like below will be executed. Usage script number datafile

::

#!/bin/bash

while read -r fruit num stuff || [ -n "$stuff" ]; do
    [ "$num" -eq "$1" ] && printf "%s  %s  %s\n" "$fruit" "$num" "$stuff"
done <"$2"

      

output:

$ ./fruit.sh 5 dat/mydata.dat

apple  5  abcdefd  ewdsf
peach  5  ewtdsfe  wtesdf

      

-2


source







All Articles