Here is a demo script that compares the awk and grep approaches for this single problem:
producing:
Both outputs are the same. The suggestion of rovf is quite correct, as illustrated, you can use grep to match field content. However, it also illustrates why I do not usually do that. Both commands are more than really basic, perhaps intermediate-complex. The grep makes use of a regular expression that is beyond many people's ability to understand it, much less create it. The awk is also not basic, but the form suggests what we are looking for, namely the content of fields 4 and 5. The extra "baggage" is to take care of the quotes. There are probably other ways of doing that, but this one seems straight-forward when one realizes how to quote an escaped quote.
So from a results-oriented POV, the solutions are the same.
However, an additional point is that, if one uses awk as the first step, one might also make the entire solution awk-based. I used standard commands because the problem decomposed easily into steps, until the extra requirements of of field contents arose.
I don't think the issue of speed would come up, but generally grep is somewhat fast than awk in similar situations. I have posted elsewhere some results comparing utilities and languages for matching.
So, rovf is correct, but the issue of transparency is more important, in my opinion.
Thanks to rovf for pointing our that grep and regular expressions are very powerful ... cheers, drl
I prefer awk, because I know it well, so there is little effort to make it precise and efficient.
For extreme robustness (e.g. long input lines) I go with perl. But usually takes longer till it does what I want.
For a "work on fields" task grep is often unprecise, or the readability and extend-ability are worse.
I Have a text file with several thousand lines of text.
Occasionally there will be a "sysAlive" line of text (every so often)
What would be an awk command to print every line of text, and to put in incrementing counter ONLY on the "sysAlive" lines
For example:
>cat file.txt
lineAAA a b c d... (4 Replies)
Hi,
I want to get the only application name from the server.
Ex:
if i give $ ps -ef | grep bw.
It will show all BW process with entire path. It will little confuse to list out the process. Can anyone have syntax to get only the instance name. I need this for be, hawk,ems also.
Please... (2 Replies)
Ok, running tsm on 2008R2, and I have the gnu awk and grep installed...I want to only capture inactive (I) files inside of the backup...I can get this to work on linux, but because windows interprets " ' ` differently, i can't get it to print out right...Here's what I have:
so basically... (8 Replies)
I want to grep some information out of the dmidecode but when I type
dmidecode | grep Memory
I get several instances of the word. Is there a way I can just choose which instance I want to display? (8 Replies)
Hey guys,
I'm having a bit of trouble getting this to work using either sed or grep. It's possible awk might be the ticket I need as well, but my regulat expression skills aren't quite up to the task for doing this.
I'm looking to grep for the string ERROR from the following log up until any... (6 Replies)
Hi all, i am new to unix scripting in ksh or any shell for that matter. I have downloaded a xml file from a website and saved on my local harddrive. inside the xml, the same tag is listed multiple times.
<title>Tonight</title>
<title>Thursday</title>
<title>Friday</title>... (6 Replies)
I need to be able to search for a beginning line header, then use grep or something else to get the very next instance of a particular string, which will ALWAYS be in "Line5". What I have is some data that appears like this:
Line1
Line2
Line3
Line4
Line5
Line6
Line7
Line1
Line2
...... (4 Replies)
How do I use awk to find the NR of first instance of a specific string after eg NR==10?
I need to find the first instance of the word "class" after a specific NR. (2 Replies)
I have an input text that looks like this (comes already sorted):
on Caturday 22 at 10:15, some event
on Caturday 22 at 10:15, some other event
on Caturday 22 at 21:30, even more events
on Funday 23 at 11:00, yet another event
I need to delete all the matching words between the lines, from... (2 Replies)