How can I get the 3 consecutive appearances of a line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I get the 3 consecutive appearances of a line?
# 1  
Old 06-07-2012
How can I get the 3 consecutive appearances of a line?

File 1

aaa
ccc
fff

File 2

aaa
bbb
ddd

File 3

aaa
bbb
eee

File 4

bbb
ggg

Output should be:

aaa
bbb

Thank you.
# 2  
Old 06-07-2012
Your question is not clear and explained. Be clear with you question and use code tag.
# 3  
Old 06-07-2012
Code:
cat file* | sort | uniq -c | head -2

If you dont want you can remove the count
This User Gave Thanks to lorcan For This Post:
# 4  
Old 06-07-2012
Quote:
Originally Posted by lorcan
Code:
cat file* | sort | uniq -c | head -2

If you dont want you can remove the count
Thanks for this but I tried it but still it includes the line even it skips a certain file. That is to say,

File 1 - aaa
File 2 - aaa
File 3 - no "aaa"
File 4 - aaa

The output was:

3 aaa

Wherein it should not output the "aaa" because its appearances are not consecutive. Thanks in advance.
# 5  
Old 06-07-2012
How about this..

Code:
awk '{a[$0]++} END { for (i in a) { if (a[i]==3) print i}}' file*


EDIT : Ignore this solution. Doesn't meet the "consecutive" requirement you provided.
This User Gave Thanks to clx For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Replace consecutive occurrence of string in same line

Hi All, I have a requirement to replace consecutive occurence of same string nedd to be replaced. Below is the input and desired output. Input: --------- 123.5|ABC|.|.|. 234.4|DEF|.|.|.|.|.| Output: --------- 123.5|ABC|||. 234.4|DEF||||| so basically "|.|" need to be replaced with... (9 Replies)
Discussion started by: ureddy
9 Replies

2. Shell Programming and Scripting

Grep 2 consecutive lines and replace the second line in a file

I have a file lake this cat ex1.txt </DISCOUNTS> <B2B_SPECIFICATION elem="0"> <B2B_SPECIFICATION elem="0"> <DESCR>Netti 2 </DESCR> <NUMBER>D02021507505</NUMBER> </B2B_SPECIFICATION> <B2B_SPECIFICATION elem="1"> <DESCR>Puhepaketti</DESCR>... (2 Replies)
Discussion started by: Dhoni
2 Replies

3. Shell Programming and Scripting

Delete a specific line containing non consecutive number?

Dear Specialists, I have following data 1 1 2 2 2 3 3 3 6 4 3 4 5 4 9 6 5 11 7 6 7 and I would like to obtain data like below 1 1 2 2 2 3 4 3 4 7 6 7 (2 Replies)
Discussion started by: Ryan Kim
2 Replies

4. Shell Programming and Scripting

Get the time difference between two consecutive line in UNIX perl script

Hi All :o, I have some log files which contains these informations: 2013-04-24 09:11:34.018 INFO XXXXXXXXXXXX 2013-04-24 09:11:34.029 INFO YYYYYYYYYYYY 2013-04-24 09:11:34.039 INFO ZZZZZZZZZZZZZZZ 2013-04-24 09:12:21.295 INFO TTTTTTTTTTTTTTT 2013-04-24 09:12:21.489 INFO... (3 Replies)
Discussion started by: shariquehabib
3 Replies

5. Shell Programming and Scripting

How to insert line with between two consecutive lines that match special pattern?

I have following pattern in a file: 00:01:38 UTC abcd 00:01:48 UTC 00:01:58 UTC efgh 00:02:08 UTC 00:02:18 UTC and I need to change something like the following 00:01:38 UTC abcd 00:01:48 UTC XXXX 00:01:58 UTC efgh 00:02:08 UTC XXXX (6 Replies)
Discussion started by: jjnight
6 Replies

6. Shell Programming and Scripting

select entry from consecutive line awk

Hi folks, I have a file with four columns that looks like the following (tab separated) 1 1 1 a 2 2 2 b 3 3 3 c 4 4 4 d I would like to create a file with always 4 columns but where the third entry corresponds to the thirst entry of the next line and so on, to get the following 1 1 2 a... (4 Replies)
Discussion started by: klebsiella
4 Replies

7. UNIX for Dummies Questions & Answers

how to print out consecutive two line from a text file

the text file like below's input: aaaaaaaaafdsfsda sdfsadfasdfasfds sdfadsf asdfadf asdfa adfsfsafas sdfsfafads asdfasdfsa I want to print out consecutive two line output: aaaaaaaaafdsfsda (1 Reply)
Discussion started by: vincent_W
1 Replies

8. Shell Programming and Scripting

Merge two non-consecutive lines based on line number or string

This is a variation of an earlier post found here: unixcom/shell-programming-scripting/159821-merge-two-non-consecutive-lines.html User Bartus11 was kind enough to solve that example. Previously, I needed help combining two lines that are non-consecutive in a file. Now I need to do the... (7 Replies)
Discussion started by: munkee
7 Replies

9. Shell Programming and Scripting

QUERY_STRING with multiples appearances of the same string prefix

I have a shell script and it works pretty well to pull out the query_string contents of a single instance of router= But if I have multiple router= strings, it only pulls out the last one. Is there a way to pull out every router= and if there is a multple string, to egrep them together... (1 Reply)
Discussion started by: numele
1 Replies

10. Shell Programming and Scripting

grab next consecutive line or lines

Hello i'm writting a krn shell that will find the letter F and I would like to grab everything from F till 0:00. Is there a sed or awk command that i could use. Thank you. File is looks like this. 11/12 xxx xxxx xxx F xxxx xxxx some info entered here some info entered here xxxx... (1 Reply)
Discussion started by: wisher115
1 Replies
Login or Register to Ask a Question