Making sure that line X is printed before line Y in a text file?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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 ....)
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
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.