How to get lines with only one occurence of pattern?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get lines with only one occurence of pattern?
# 1  
Old 12-21-2016
How to get lines with only one occurence of pattern?

My data
Code:
20161220 20:30:01 MODE 1 TEST 1 SOURCE 1 SET 1
20161220 20:30:02 MODE 1 TEST 2 SOURCE 1 SET 1
20161220 20:30:02 MODE 1 TEST 3 SOURCE 1 SET 1
20161220 20:30:02 MODE 1 TEST 1 SOURCE 2 SET 1
20161220 20:30:04 MODE 1 TEST 1 SOURCE 1 SET 1 MODE 1 TEST 2 SOURCE 2 SET 1
20161220 20:30:02 MODE 2 TEST 2 SOURCE 1 SET 1
20161220 20:30:06 MODE 1 TEST 1 SOURCE 1 SET 1 MODE 2 TEST 2 SOURCE 2 SET 1

To get lines with more than one MODE I do this:
Code:
$ sed -n '/MODE.*MODE/p' aa
20161220 20:30:04 MODE 1 TEST 1 SOURCE 1 SET 1 MODE 1 TEST 2 SOURCE 2 SET 1
20161220 20:30:06 MODE 1 TEST 1 SOURCE 1 SET 1 MODE 2 TEST 2 SOURCE 2 SET 1

Now I need to get only lines with single occurrence of MODE. Before I start writing an awk counting these MODE keywords, is there a "proper" way with just a regular expression?
# 2  
Old 12-21-2016
If all lines contain MODE as shown:
Code:
$ sed -n '/MODE.*MODE/!p' aa

This User Gave Thanks to Aia For This Post:
# 3  
Old 12-21-2016
For must contain MODE but not MODE.*MODE

Code:
sed -n '/MODE/!d;/MODE.*MODE/!p' aa


or you could use 2 greps:

Code:
grep "MODE" aa | grep -v "MODE.*MODE"

This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 12-21-2016
Some alternatives with Perl.

Display only lines with one MODE
Code:
perl -ne 'print if (()=/MODE/g) == 1' migurus.file

Display only lines with two MODE
Code:
perl -ne 'print if (()=/MODE/g) == 2' migurus.file

Display any line that do not have only two MODE
Code:
perl -ne 'print if (()=/MODE/g) != 2' migurus.file

Display any line with zero or one MODE
Code:
perl -ne 'print if (()=/MODE/g) < 2' migurus.file


Last edited by Aia; 12-21-2016 at 11:43 PM..
# 5  
Old 12-22-2016
Hi Aia , could you please explain below highlighted part of code.
Code:
perl -ne 'print if (()=/MODE/g) == 1' migurus.file

# 6  
Old 12-22-2016
You cannot do this with a single basic regular expression. You need to either count the number like some solutions do or use multiple regexes.

A variation on Chubler XL's approach with two regexes:
Code:
sed '/MODE.*MODE/d; /MODE/!d' file

Or counting the number of occurrences:
Code:
awk -FMODE 'NF==2' file


Last edited by Scrutinizer; 12-22-2016 at 05:02 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 12-22-2016
Quote:
Originally Posted by looney
Hi Aia , could you please explain below highlighted part of code.
Code:
perl -ne 'print if (()=/MODE/g) == 1' migurus.file

The parenthesis is a list context of the matches from the regex which the evaluation uses back in scalar context.

Written in another way:
Code:
 perl -ne '@match_list = $_ =~ m/MODE/g; print $_ if (scalar @match_list) == 1' migurus.file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract whole word preceding a specific character pattern with first occurence of the pattern

Hello. Here is a file contents : declare -Ax NEW_FORCE_IGNORE_ARRAY=(="§" ="§" ="§" ="§" ="§" .................. ="§"Here is a pattern =I want to extract 'NEW_FORCE_IGNORE_ARRAY' which is the whole word before the first occurrence of pattern '=' Is there a better solution than mine :... (3 Replies)
Discussion started by: jcdole
3 Replies

2. UNIX for Dummies Questions & Answers

Vi delete line with second occurence of pattern

I have a large file and many lines are duplicated. I'm trying to delete lines with every second occurrence of a pattern. Did tried searching similar question but no luck. I can delete all lines matching pattern with :g/pattern/d but don't want to lose data. Sample pattern to delete... (6 Replies)
Discussion started by: homer4all
6 Replies

3. Shell Programming and Scripting

Match pattern and print the line number of occurence using awk

Hi, I have a simple problem but i guess stupid enough to figure it out. i have thousands rows of data. and i need to find match patterns of two columns and print the number of rows. for example: inputfile abd abp 123 abc abc 325 ndc ndc 451 mjk lkj... (3 Replies)
Discussion started by: redse171
3 Replies

4. Shell Programming and Scripting

Help with using awk to print pattern/occurence

Hi, Do anybody know how to use awk to count the pattern at specific column? Input file M2A928K 419 ath-miR159a,gma-miR159a-3p,ptc-miR159a 60 miR235a . . Output file M2A928K 419 ath-miR159a,gma-miR159a-3p,ptc-miR159a 60 miR235a 3 . . I plan to count how many "miR" in column 3... (2 Replies)
Discussion started by: cpp_beginner
2 Replies

5. Linux

Problem editting the first occurence of a pattern in the first uncommented line

Hi I have to replace a pattern found in the first uncommented line in a file. The challenge I'm facing is there are several such similar lines but I have to edit only the first uncommented line. Eg: #this is example #/root/xyz:Old_Pattern /root/xyz:Old_Pattern /root/xyz:Old_Pattern ... (10 Replies)
Discussion started by: Stoner008
10 Replies

6. Shell Programming and Scripting

Printing the continuous occurence of pattern using awk ?

Hello all, I have a input file like this. input file --------------- abc ab001 + ab002 zca acb ab006 + ab007 caz cba ab003 + ab004 zca bac ab004 - ab005 zac bca ab002 - ab003 cza cba ab005 + ab006 acz cba ab005 ... (5 Replies)
Discussion started by: admax
5 Replies

7. Shell Programming and Scripting

How to identify the occurence of a pattern between a unique character?

hi, is it possible to find the number of occurences of a pattern between two paranthesis. for e.g i have a file as below. >>{ >>hi >>GoodMorning >>how are you? >>} >>is it good, >>tell me yes, if it is good In the above file, its clear the occurence of word "Good"... (17 Replies)
Discussion started by: divak
17 Replies

8. Shell Programming and Scripting

Sed and replacing one occurence of pattern

I would like to use sed to replace one occurence of a pattern in a file. When I use the s/// command it replaces all occurences of the pattern in the file. Should I be using something other than sed? Thanks (6 Replies)
Discussion started by: ss9u
6 Replies

9. Shell Programming and Scripting

Perl onliner to search the last line with an occurence of a pattern

Hi I need a perl onliner which seaches a line starting with a pattern(last occurence) and display it. similar to grep 'pattern' filename | tail -1 in UNIX Ex: I want to display the line starting with "cool" and which is a last occurence adadfadafadf adfadadf cool dfadfadfadfara... (4 Replies)
Discussion started by: ammu
4 Replies

10. UNIX for Dummies Questions & Answers

Pattern occurence in a file

How can we find the number of occurence of a specified pattern in a file? This command would be a part of a shell script. (5 Replies)
Discussion started by: videsh77
5 Replies
Login or Register to Ask a Question