LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-23-2017, 06:02 AM   #1
AnirudhSingh
LQ Newbie
 
Registered: Jul 2017
Posts: 1

Rep: Reputation: Disabled
print consecutive lines having same value after matching the string


Hi All,

I have a file with below mentioned data

Sat Jul 22 03:04:00 2017 None
Sat Jul 22 03:05:00 2017 None
Sat Jul 22 03:06:00 2017 None
Sat Jul 22 03:07:00 2017 None
Sat Jul 22 03:08:00 2017 100.000000
Sat Jul 22 03:09:00 2017 100.000000
Sat Jul 22 03:10:00 2017 100.000000
Sat Jul 22 03:11:00 2017 100.000000
Sat Jul 22 03:12:00 2017 100.000000
Sat Jul 22 03:13:00 2017 100.000000
Sat Jul 22 03:14:00 2017 100.000000
I need to match and print value "None" if it occurs consecutively 5 time in a file
 
Old 07-23-2017, 06:16 AM   #2
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,072

Rep: Reputation: 2444Reputation: 2444Reputation: 2444Reputation: 2444Reputation: 2444Reputation: 2444Reputation: 2444Reputation: 2444Reputation: 2444Reputation: 2444Reputation: 2444
Thank you for sharing that information. Did you have some question or problem related to this information?
 
Old 07-23-2017, 08:46 AM   #3
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,215

Rep: Reputation: 2679Reputation: 2679Reputation: 2679Reputation: 2679Reputation: 2679Reputation: 2679Reputation: 2679Reputation: 2679Reputation: 2679Reputation: 2679Reputation: 2679
Code:
 n=0; while read _ _ _ _ _ a; do [[ $a == "None" ]] && ((n=n+1)) || n=0; ((n==5)) && echo "$a"; done < data.txt
 
1 members found this post helpful.
Old 07-23-2017, 10:38 AM   #4
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,900
Blog Entries: 43

Rep: Reputation: 3138Reputation: 3138Reputation: 3138Reputation: 3138Reputation: 3138Reputation: 3138Reputation: 3138Reputation: 3138Reputation: 3138Reputation: 3138Reputation: 3138
Member response

Hi,

Welcome to LQ!

I suggest that you first read; http://www.linuxquestions.org/questi...#faq_lqwelcome so you will have an idea as to how to create a informational request for a post/thread.

So you have shown us your need! What is your deed?
We will aid you when you help yourself to a solution. Provide us with what you have attempted and then maybe someone will be able to assist.

Hope this helps.
Have fun & enjoy!
 
Old 07-23-2017, 03:12 PM   #5
!!!
Member
 
Registered: Jan 2017
Location: Fremont, CA, USA
Distribution: Trying any&ALL on old/minimal
Posts: 997

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
Hi AnirudhSingh. I see you came back 36 minutes before #4 welcomed you to LQ (so do I; I like puzzles!) and gifted you guidance on how to use LQ. Was the bash solution you saw in #3 what you wanted? Or are you using another language, like perl? An idea I had, was for you to write, in language-independent 'pseudo-code' (basically English), how a human would do it, to show your thoughts on an algorithm.

A minor detail: after your program reports a 5th "None", *IF* the subsequent=following input line also had None, did you want your program to:
a) have already exited
b) keep reporting=printing "None", until an input line doesn't end with that, or
c) wait for another group of 5 ? Thus, *IF* your input data contained exactly=only 10 consecutive matches='hits', would you want your program to print=report "None" 1, 6, or 2 times? "Bug"?

What new members of this nice community ideally do, in the Intro Forum, is first share a little about their distro, PC, and Linux experience/goals/interest. Are you taking a class? Do you enjoy researching thngs on the web (as I do!!!) ?

Anyway, I hope to hear back from you. Study well

Last edited by !!!; 07-23-2017 at 03:58 PM. Reason: Added links to Intro Forum, Wiki, DW.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Stripping all but lines matching string from text files d.vanheeckeren Linux - General 5 04-29-2013 08:00 AM
Shell script to find a string and print x lines before and y lines after the string igorza Linux - Newbie 6 04-18-2013 04:31 PM
print pattern matching lines until immediate occurence of a character keerthika Linux - Newbie 7 04-11-2012 05:58 AM
Sed command to print matching lines and 2 lines above.. DX398 Programming 12 10-01-2008 08:25 AM
regular expresions: java: matching lines with out string zymos Programming 1 05-28-2005 09:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration