Grep -v value in 2nd column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep -v value in 2nd column
# 1  
Old 06-06-2014
Grep -v value in 2nd column

Trying to do a grep -v on a value in the 2nd column of text. So if the word apple appears in a line in the 2nd column, it would not show up when the file was cat. Seems like a simple enough operation but I just can't figure it out. Any help would be appreciated. Thanks in advance.
Code:
Are apples good for you. Are pears better for you. Is beer an alternative.

The output would be
Code:
Are pears better for you.  Is beer an alternative

# 2  
Old 06-06-2014
Quote:
Originally Posted by jimmyf
Trying to do a grep -v on a value in the 2nd column of text. So if the word apple appears in a line in the 2nd column, it would not show up when the file was cat. Seems like a simple enough operation but I just can't figure it out. Any help would be appreciated. Thanks in advance.
Code:
Are apples good for you. Are pears better for you. Is beer an alternative.

The output would be
Code:
Are pears better for you.  Is beer an alternative

It is best if you post a portion of the real text you are working on, instead of a fake imitation. Since your request is inconsistent[*] with the output shown, it is most likely that if someone gives you a solution base on it, you'll come back saying that it doesn't work on your actual text.


* inconsistency:
Quote:
So if the word apple appears in a line in the 2nd column, it would not show up
Quote:
Are pears better for you. Is beer an alternative
What it appears to not show up is a human delineated sentence instead of just the 2nd column
# 3  
Old 06-06-2014
apples pears beer are all in the 2nd column.
# 4  
Old 06-06-2014
Code:
awk '$2 !~ "apple"' RS="." ORS="." file
 Are pears better for you. Is beer an alternative.

# 5  
Old 06-07-2014
What Aia is trying to tell you is:

Quote:
Originally Posted by jimmyf
Code:
Are apples good for you. Are pears better for you. Is beer an alternative.

Code:
Are pears better for you.  Is beer an alternative

You posted a single line of input. In this single line the second "word" (as per the IBM definition of "word", which is a sequence of nonblank characters delimited by blanks) is "apples". As "grep" works on lines (and lines alone!), the whole line would be either printed or not printed, depending on the regexp you feed it. If you create a regexp matching the second word "apple" (this would be perfectly possible) and you use the "-v" switch to suppress every line matched and print every line not matched, then the output of the single-line input would be empty.

What you want - as indicated by your sample output - is not to work on lines but on sentences. Precisely, you want to remove every sentence which has "apple" as the second word from the input. "Sentence" here means "a sequence of words, followed by a closing punctuation mark (full stop, question mark, exclamation mark, but not colon, comma or semicolon).

Is that so? If yes, we could provide an awk- or sed-script doing this. "grep" will never be able to do this at all.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep values from column 2 in reference of column 1

Gents Is it possible to update the code to get the desired output files from the input list. I called variable to the first column. I need to consider the first column as key to grep the values in the second column according to the desired request. input list (attached ) output1 ... (12 Replies)
Discussion started by: jiam912
12 Replies

2. Linux

Print the 1st column and the value in 2nd or 3rd column if that is different from the values in 1st

I have file that looks like this, DIP-17571N|refseq:NP_651151 DIP-17460N|refseq:NP_511165|uniprotkb:P45890 DIP-17571N|refseq:NP_651151 DIP-19241N|refseq:NP_524261 DIP-19241N|refseq:NP_524261 DIP-17151N|refseq:NP_524316|uniprotkb:O16797 DIP-19588N|refseq:NP_731165 ... (2 Replies)
Discussion started by: Syeda Sumayya
2 Replies

3. Shell Programming and Scripting

How to awk or grep the last column in file when date on column contains spaces?

Hi have a large spreadsheet which has 4 columns APM00111803814 server_2 96085 Corp IT Desktop and Apps APM00111803814 server_2 96085 Corp IT Desktop and Apps APM00111803814 server_2 96034 Storage Mgmt Team APM00111803814 server_2 96152 GWP... (6 Replies)
Discussion started by: kieranfoley
6 Replies

4. Shell Programming and Scripting

Transpose from 2nd column till the last column

Hi I have 5 columns like this a b c d e f g h i j k l m n o From 2nd column till the 5th column of every record, I would like to transpose them as rows, so my output file contains only one row a b c d e f g h i j (9 Replies)
Discussion started by: jacobs.smith
9 Replies

5. Shell Programming and Scripting

Calculate 2nd Column Based on 1st Column

Dear All, I have input file like this. input.txt CE2_12-15 3950.00 589221.0 9849709.0 768.0 CE2_12_2012 CE2_12-15 3949.00 589199.0 9849721.0 768.0 CE2_12_2012 CE2_12-15 3948.00 589178.0 9849734.0 768.0 CE2_12_2012 CE2_12-52 1157.00 ... (3 Replies)
Discussion started by: attila
3 Replies

6. Shell Programming and Scripting

Grep/Awk on 1st 2 Letters in 2nd Column of File

Hi everyone. I need to change a script (ksh) so that it will grep on the 1st 2 letters in the second column of a 5 column file such as this one: 192.168.1.1 CAXY0_123 10ABFL000001 # Comment 192.168.1.2 CAYZ0_123 10ABTX000002 # Comment 192.168.2.1 FLXY0_123 11ABCA000001 ... (4 Replies)
Discussion started by: TheNovice
4 Replies

7. Shell Programming and Scripting

1st column,2nd column on first line 3rd,4th on second line ect...

I need to take one column of data and put it into the following format: 1st line,2nd line 3rd line,4th line 5th line,6th line ... Thanks! (6 Replies)
Discussion started by: batcho
6 Replies

8. Shell Programming and Scripting

comparing column of two different files and print the column from in order of 2nd file

Hi friends, My file is like: Second file is : I need to print the rows present in file one, but in order present in second file....I used while read gh;do awk ' $1=="' $gh'" {print >> FILENAME"output"} ' cat listoffirstfile done < secondfile but the output I am... (14 Replies)
Discussion started by: CAch
14 Replies

9. Shell Programming and Scripting

grep data on 2nd line and 3rd column

How do I grep/check the on-hand value on the second line of show_prod script below? In this case it's a "3". So if it's > 0, then run_this, otherwise, quit. > ./show_prod Product Status Onhand Price shoe OK 3 1.1 (6 Replies)
Discussion started by: joker_789us
6 Replies

10. Shell Programming and Scripting

Parse 1 column and add 2nd column

I'm racking my brain on this one! :( I have a list like this: Paul 20 Paul 25 Paul 30 Frank 10 Julie 15 Julie 13 etc, etc... I've been trying to figure out a way to have the output display the name in the first column ONCE and add the numbers in the second column and display that... (2 Replies)
Discussion started by: sdlennon
2 Replies
Login or Register to Ask a Question