Fr0ntSight
Fr0ntSight

Reputation: 2826

Delete specific rows based on specific word in column

I have very large tab-separated files, and I need delete all rows where the word "TelePacific" appears in a specific column. In this case all the rows where TelePacifc occurs in the 4th column. Here is an example input file:

7/18/13 10:06   0:00:09 TelePacific random person DEREK         9256408665  random company
7/18/13 10:07   0:00:21 TelePacific random person DEREK         9256408665  random company
7/18/13 10:10   0:19:21 TelePacific random person DEREK         9256408665  random company
7/18/13 10:39   0:01:07 random person       107  
7/18/13 11:02   0:01:41 random person Gilbert       107 TelePacific
7/18/13 12:17   0:00:42 random person Gilbert       107 TelePacific
7/18/13 13:35   0:00:41 random person Gilbert       107 TelePacific
7/18/13 13:44   0:12:30 TelePacific ADKNOWLEDGE     8169311771  random company
7/18/13 14:46   0:19:48 TelePacific TOLL FREE CALL  8772933939  random company
7/15/13 10:09   0:01:27 random person Esquivel      272 TelePacific
7/15/13 10:16   0:00:55 random person Esquivel      272 TelePacific
7/15/13 10:59   0:00:51 random person Esquivel      272 TelePacific
7/15/13 11:01   0:01:09 random person Esquivel      272 TelePacific

Upvotes: 2

Views: 7951

Answers (5)

Chris Seymour
Chris Seymour

Reputation: 85775

This should do the trick:

$ sed '/TelePacific/d' file

If you are happy with the output use the -i option to store the changes back to the file.

$ sed -i '/TelePacific/d' file

EDIT:

To only return results for TelePacific in the fourth column:

$ awk '$4=="TelePacific"' file

Or the inverse:

$ awk '$4!="TelePacific"' file

Upvotes: 1

Jeff Bowman
Jeff Bowman

Reputation: 95614

fgrep -v will do this.

fgrep is equivalent to grep -F and prevents grep from interpreting special characters in your pattern as regex control characters. The -v parameter causes fgrep to output all lines that don't match the pattern, in contrast to outputting the lines that do (which is the default).

fgrep -v TelePacific inputfile.tsv > outputfile.tsv

As anubhava noted above, you may choose grep -v "\bTelePacific\b" instead to ensure that you don't accidentally match "TelePacificFoo" or "FooTelePacific".

Upvotes: 1

ahilsend
ahilsend

Reputation: 931

Try this:

grep -v TelePacific in-file > out-file

The -v option inverts the search, so grep prints all lines that don't match the search pattern.

This won't work if in-file and out-file are the same. To achive that you have to use a temp file like this:

grep -v TelePacific in-file > in-file.tmp && mv in-file.tmp in-file

Upvotes: 0

anubhava
anubhava

Reputation: 784958

Using grep -v:

grep -v "\bTelePacific\b" file > output && mv output file

Or using awk:

awk '$4 != "TelePacific"' file > output && mv output file

Upvotes: 5

ds_
ds_

Reputation: 1

here is a solution with sed

#!/bin/bash

sed '/TelePacific/d' your_file.txt > file_without_telepacific.txt

Upvotes: 0

Related Questions