LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-21-2014, 07:31 AM   #16
CoreOxide
LQ Newbie
 
Registered: Aug 2014
Posts: 15

Original Poster
Rep: Reputation: Disabled

I did some testing today with an actual log file, had some free time.

Quote:
Originally Posted by grail View Post
Code:
sed -n '/^DistributedPOManager/,/^DistributedPOManager/{/^ControlAction/p}' file
Does not return anything, for some reason...


Quote:
Originally Posted by danielbmartin View Post
Try this ...
Code:
result=$(grep '^DistributedPOManager\|^ControlAction' $InFile \
|cut -c1       \
|paste -sd"\0" \
|grep "DCD"    \
|wc -l)
echo "result=" $result
Daniel B. Martin

This works, but I had to trim the date and time at the beginning of each line.

Thanks!
 
Old 08-21-2014, 09:30 AM   #17
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
The sed assumes all the data is at the start of the line and spelled exactly as you see it, including case, otherwise it will not display anything.

So if your original file has information different to the examples, you either need to provide another example or tweak what has been supplied to match
 
Old 08-22-2014, 11:22 AM   #18
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
Quote:
Originally Posted by CoreOxide View Post
This works, but I had to trim the date and time at the beginning of each line.
Thanks!
As I wrote several times avoid writing such chains, like grep|cut|paste|grep|wc, usually it can be implemented using a single grep/sed/awk/perl/whatever.
Especially the last two steps:
... | grep "DCD" | wc -l is equal to ... | grep -c "DCD"
If you still need to trim some data just go ahead and rework it ... (or at least give us some more sample - as it was already mentioned).

From the other hand I would rather go with something like a state-machine:
Code:
awk ' BEGIN { state = 0; count = 0 }
/first line regex/ { if state == 0 state=1 else error }
/second line regex/ { if state == 1 state=2 else error }
/third regex/ { if state == 2 state=3 else error }
state == 3 { count++; state=0 }
END { print count }
'
(just an idea, not tested and also can be refined ....)
 
2 members found this post helpful.
Old 08-26-2014, 06:30 AM   #19
CoreOxide
LQ Newbie
 
Registered: Aug 2014
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
The sed assumes all the data is at the start of the line and spelled exactly as you see it, including case, otherwise it will not display anything.

So if your original file has information different to the examples, you either need to provide another example or tweak what has been supplied to match
Yeah, my logs have date and time print in the beginning of each line, so I had to trim it. I still need to do some testing on actual logs, but I think it will work


Quote:
Originally Posted by pan64 View Post
As I wrote several times avoid writing such chains, like grep|cut|paste|grep|wc, usually it can be implemented using a single grep/sed/awk/perl/whatever.
Especially the last two steps:
... | grep "DCD" | wc -l is equal to ... | grep -c "DCD"
If you still need to trim some data just go ahead and rework it ... (or at least give us some more sample - as it was already mentioned).

From the other hand I would rather go with something like a state-machine:
Code:
awk ' BEGIN { state = 0; count = 0 }
/first line regex/ { if state == 0 state=1 else error }
/second line regex/ { if state == 1 state=2 else error }
/third regex/ { if state == 2 state=3 else error }
state == 3 { count++; state=0 }
END { print count }
'
(just an idea, not tested and also can be refined ....)
Where in the awk command do I supply the input file?
 
Old 08-26-2014, 06:50 AM   #20
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,870
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
like with any other filter:
Code:
awk 'program' inputfile >outputfile
 
  


Reply

Tags
file, line, order, text



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
C++ text file line by line/each line to string/array guru11 Programming 5 12-29-2011 09:34 AM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:22 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