Prev: Passover Seder plates !!!
Next: Firefox P.O.S.
From: Nuno J. Silva on 11 Mar 2010 15:02 Torsten Mueller <dev-null(a)shared-files.de> writes: > J G Miller <miller(a)yoyo.ORG> schrieb: > >> > Thanks! (You never end learning sed ...) Thanks! Yet another sed feature I didn't know about. I think sometimes I end up replacing \n to something else (using tr) to include \n on the expression. This will surely make some of those command chains simpler. >> >> What annoys me most though, is people who resort to awk to change >> things on one line. awk is for manipulating columns of data, not >> just changing or selecting a single field or two on one line ;) > > Never used awk seriously. But indeed over the years I surprised a lot > of people (especially from the Windows world ...) by demonstrating the > features of sed upon a 1GB logfile. This ends up always in unbelieving > eyes and a mutual whisper of "witchcraft" and so on. I really wonder > that sed is still almost unknown even in the Unix world. Younger > people have usually never heard of this. I recall reading an instruction list to rename a project (on windows), and the name had to be changed on a file. Of course, the instructions, written for windows user, and (probably) by a windows user, asked people to open the file in an editor and change some lines. Fortunately sed was available. -- Nuno J. Silva gopher://sdf-eu.org/1/users/njsg
From: Chris F.A. Johnson on 11 Mar 2010 20:28 On 2010-03-11, Chris Davies wrote: > J G Miller <miller(a)yoyo.org> wrote: >> What annoys me most though, is people who resort to awk >> to change things on one line. awk is for manipulating columns >> of data, not just changing or selecting a single field or two on >> one line ;) > > IMO awk instructions are often easier to read when processing data > separated by whitespace. For example, I can write this: > > virsh list | > awk '$3 == "running" {print $2} > > or this: > virsh list | > sed -n "/^[^{tab} ]*[{tab} ]\([^{tab} ]*\)[{tab} ]running.*/\1/p' > > or an equivalent in Perl, C, etc. > > One is definitely clearer (to me) than the other. On the other hand, > the solution showing how sed can process a line /after/ a pattern match > is definitely clearer than what I'd imagine writing in awk. awk ' /pattern/ && (n == 0) { ++n; print; next } (n == 1) { #do whatever } { print } ' "$file" -- Chris F.A. Johnson, <http://cfajohnson.com> Author: Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress) Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
From: Chris Davies on 12 Mar 2010 06:21
J G Miller <miller(a)yoyo.org> wrote: > virsh list is generating columns of data? yes? > That is exactly the good use for which awk was designed. Perhaps virsh was a bad example, but I prefer to choose something that folks "out there" could potentially try. > It is using awk on a single line of data that I was complaining about. "Use" as in "search & edit", or "use" as in "search and print"? I think the difference is best described by the two tools' names - one is designed as a line-based pattern editor, the other is a more of a scanning/reporting tool. Both can be used for parts of either job, but not necessarily as easily. > The point is awk is best used for extracting bits of information from > columns of data eg telephone numbers from /etc/passwd, rather than > sed which is best used to edit a field in situ. Mostly agreed. > As always, it depends on what you are trying to achieve and the > type of data will determine which is the best tool to use, and > the tool will be different for different types of jobs. Definitely agreed. And for those with only hammers, everything's a nail. Fortunately we have a plethora of tools from which to choose an appropriate one :-) Chris |