Grepping exact pattern and deleting the rows


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grepping exact pattern and deleting the rows
# 1  
Old 05-02-2016
Grepping exact pattern and deleting the rows

Hi All,

I have the Input as below:

Code:
21011513    6030    5570    1710       0       0    5140       0    3430    3430       0
21049513  152340  138260  101210       0       0  134440       0   33880   33880       0
31003514   16100   13280    7580    3250    1530   15650    8090    8100    8100       0
31513514   28720   23310   11840    6140    2330   27180   15960   15340   15340       0
16161515    6380    5370    3750     820     530    5950    1790    2200    2200
45141518    7580    6430      40       0     610    6730    3510    6690    6690       0
39019519  152060  130190   92940       0       0  132790       0   39850   39850       0

I want to grep the lines containing 513 and 515 in the positions 6 to 9. and i want to delete those rows. So my output should look like below.

Code:
31003514   16100   13280    7580    3250    1530   15650    8090    8100    8100       0
31513514   28720   23310   11840    6140    2330   27180   15960   15340   15340       0
45141518    7580    6430      40       0     610    6730    3510    6690    6690       0
39019519  152060  130190   92940       0       0  132790       0   39850   39850       0

I have tried the below code:


nawk -F, '$1 !~ /513|515/' sample > op2

With this code i am getting the output as below:

Code:
31003514   16100   13280    7580    3250    1530   15650    8090    8100    8100       0
45141518    7580    6430      40       0     610    6730    3510    6690    6690       0
39019519  152060  130190   92940       0       0  132790       0   39850   39850       0

Since the Row 4 in my Input contains '513' in the positions 3 to 5, that row also getting ignored.

Is there any better way to achieve the desired output ?

Thanks in Advance.

Regards,
am24
# 2  
Old 05-02-2016
Hello am24,

Could you please try following and let me know if this helps.
Code:
awk '($1 !~ /513$/ && $1 !~ /515$/)'  Input_file

Output will be as follows.
Code:
31003514   16100   13280    7580    3250    1530   15650    8090    8100    8100       0
31513514   28720   23310   11840    6140    2330   27180   15960   15340   15340       0
45141518    7580    6430      40       0     610    6730    3510    6690    6690       0
39019519  152060  130190   92940       0       0  132790       0   39850   39850       0

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 05-02-2016
Why use -F, when there is no comma as field separator?

Code:
$ awk '$1 !~ /51[35]$/' infile
31003514   16100   13280    7580    3250    1530   15650    8090    8100    8100       0
31513514   28720   23310   11840    6140    2330   27180   15960   15340   15340       0
45141518    7580    6430      40       0     610    6730    3510    6690    6690       0
39019519  152060  130190   92940       0       0  132790       0   39850   39850       0

This User Gave Thanks to zaxxon For This Post:
# 4  
Old 05-02-2016
We are talking of positions 6 - 8, aren't we? Try
Code:
grep -v '^.....51[35] ' file

# 5  
Old 05-02-2016
Hi Ravinder/Zaxxon

Thank you so much for the quick response. Both of your codes worked fine. I have got the expected output. Thanks Zaxxon for reminding me about the Field Separator.

Just to understand, the '$' in the code indicates the end of the column right ? Please correct me if i am wrong.

What if i want to ignore any row that contains '513' in the mid position of the column ?

Say like below:
31513514 28720 23310 11840 6140 2330 27180 15960 15340 15340 0

in the above 513 is in positions 3 to 5. How can i ignore this particular row?

Regards,
am24
# 6  
Old 05-02-2016
Hello am24,

Could you please try following and let me know if this helps you.
Code:
awk '(substr($1,3,3) != 51[35])'  Input_file

Thanks,
R. Singh
# 7  
Old 05-02-2016
Thanks Rudi.

I have tried the code given by you. It worked fine. Also i got the logic. The ^ symbol is for the exact match and each '.' is referring to the position starting from 1.

Am I correct ?

If i am correct then, the solution for teh second question i asked will be
grep -v '^..513... ' sample > op6

Thanks again :-)

Regards,
am24

---------- Post updated at 07:15 AM ---------- Previous update was at 07:10 AM ----------

Hi Ravinder,

Thanks. I have tried the code. But getting an error message as below.

Code:
nawk '(substr($1,3,3) != 51[35])'  sample > op8
awk: syntax error near line 1
awk: bailing out near line 1

Regards,
am24
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To search exact pattern

Hi, I need to search the exact pattern in a file file.txt AUS.txt|AUS.chk NZ.txt|NZ.ch I am getting the result as AUS.txt|AUS.chk with below code but i need only AUS.txt to be printed grep AUS.txt file.txt CODE tags also for data files (8 Replies)
Discussion started by: rohit_shinez
8 Replies

2. Shell Programming and Scripting

Printing next two lines from a file after grepping a specific pattern

Hi I have a file like # vi require.txt 1,BANK,Read blocks that cycle. yellow Read blocks. 2,ACCOUNT,Finished Red Finished . 3,LOAN, pipe white pipe 4,PROFIT,Resolve. black Resolve Am using like cat require.txt | grep -w ACCOUNTThe output I get is (8 Replies)
Discussion started by: Priya Amaresh
8 Replies

3. Shell Programming and Scripting

To find the exact pattern

My Input : Hi editor this is the exact pattern which we looking for the previous patternmatch My code: awk '/pattern/ { print a } { a = $0 }' Current output : exact previous (3 Replies)
Discussion started by: Roozo
3 Replies

4. UNIX for Dummies Questions & Answers

Deleting a pattern in UNIX without deleting the entire line

Hi I have a file: r58778.3|SOURCES={KEY=f665931a...,fw,221-705}|ERRORS={16_1:T,30_1:T,56_1:C,57_1:T,59_1:A,101_1:A,115:-,158_1:C,186_1:A,204:-,271_1:T,305:-,350_1:C,368_1:G,442_1:C,472_1:G,477_1:A}|SOURCE_1="Contig_1092402550638"(f665931a359e36cea0976db191ff60ff09cc816e) I want to retain... (15 Replies)
Discussion started by: Alyaa
15 Replies

5. UNIX for Advanced & Expert Users

merge two tab delimited file with exact same number of rows in unix/linux

Hi I have two tab delimited file with different number of columns but same number of rows. I need to combine these two files in such a way that row 1 in file 2 comes adjacent to row 1 in file 1. For example: The content of file1: field1 field2 field3 a1 a2 a3 b1 b2 b3... (2 Replies)
Discussion started by: mary271
2 Replies

6. Shell Programming and Scripting

how to finding the exact pattern

I have a output like below: A1 B2 C1 D3 A12 B4 A14 I am trying to find A1 by using grep grep -i "A1" But I got (4 Replies)
Discussion started by: anupdas
4 Replies

7. Shell Programming and Scripting

Deleting specific rows in large files having rows greater than 100000

Hi Guys, I need help in modifying a large text file containing more than 1-2 lakh rows of data using unix commands. I am quite new to the unix language the text file contains data in a pipe delimited format sdfsdfs sdfsdfsd START_ROW sdfsd|sdfsdfsd|sdfsdfasdf|sdfsadf|sdfasdf... (9 Replies)
Discussion started by: manish2009
9 Replies

8. Shell Programming and Scripting

Searching a pattern in file and deleting th ewhole line containing the pattern

Hi All, Please can someone assist in the script I have made that searches a pattern in a file and delete the whole line containing the pattern. #!bin/sh # The pattern that user want to add to the files echo "Enter the pattern of the redirect" read value # check if the user has... (1 Reply)
Discussion started by: Shazin
1 Replies

9. Shell Programming and Scripting

Grepping for Exact Strings

ok, apparently this is a very difficult question to answer based on my searches on google that came up fruitless. what i want to do is grep through a file for words that match a specified string. but the thing is, i keep getting all words in the file that have the string in them. say for... (27 Replies)
Discussion started by: SkySmart
27 Replies

10. Shell Programming and Scripting

Grepping 1 line above and below pattern

I have a pattern:: xldn3176bap>arj SOCRATES_MAIN_LNX | grep " FA " 10/04/2007 21:01 10/04/2007 21:01 FA 1776752/1 1 I want the line above this line and the line below it too. Can anyone tell me how it can be done? - iAm4Free (4 Replies)
Discussion started by: iAm4Free
4 Replies
Login or Register to Ask a Question