above are four separate commands. i want to combine the grep in each command into one awk statement. instead of using two egreps, i figure awk can do it better.
desired code would be:
sed -n "2,10p" lfile | awk....
sed -n "2,10p" lfile | awk....
sed -n "2,10p" lfile | awk....
sed -n "2,10p" lfile | awk.....
see what i did there. basically, will awk reliably understand and process the ".*" and "|" like egrep would?
and by the way, when i run the commands that's suppose to give a count, if nothing matches, it just returns a blank. anyway to make it return a 0 instead when nothing is found that matches the specified strings?
I do not really see what this all brings, I think I would prefer the two greps to the awk solutions, because they are easier to understand and thus to maintain. One could go even further and put everything in one awk:
awk '
NR==2,NR==10 {
if (/error/) {
if(/memory/) {
m=m RS $0
i++
}
else {
o=o RS $0
j++
}
}
}
END{
print i m RS j o
}
' file
This probably would make it more efficient, but one could ask if that is required and the down side is that it is also less simple...
the thing is, i have a very huge script that uses quite a lot of egreps like those in my first post. however, while the script runs beautifully, i notice there are just too many commands being called in it. commands that, if given thought can be combined into one by a more advanced user. which is why i created this thread.
bipinajith's one-liner solution not only helps to combine the egreps in my post, it also got rid of the sed. so in essence, his awk did what i (not an advanced user) was trying to do with 3 commands (sed, egrep, egrep).
i'm making the modifcations to my script as we speak. and i'll see if the awk solution helps to decrease the script's overall processing time.
btw, can you please combine your awk suggestion into a one-liner? i think its easier to read for a novice if anywhere is in one line. if you can, please explain the code for me.