News for hackers. Stuff that matters.
Re:Awk in 20 Minutes by baldfat on Thursday January 15, 2015 @12:33PM (#8893910)
Awk & SED I have a hard time think about one without the other. I think they should be married.
Re:Awk in 20 Minutes by cbd1984 on Thursday January 15, 2015 @02:33PM (#8894800)
If you view the Unix shell as a language unto itself, awk and sed are both higher-level functions in that language, married via the use of pipelines (concatenative programming without a stack).

http://en.wikipedia.org/wiki/Concatenative_programming_langu...

Re:Awk in 20 Minutes by AdmiralAsshat on Thursday January 15, 2015 @01:32PM (#8894375)
Awk & Sed are labeled as the following in my mind:

- Awk: That thing I use when I need to grep for something over more than one line and/or do some basic transformations.

- Sed: That thing with the painful syntax for doing ridiculously complicated regex substitutions.

With that said, I find sed much more difficult to use than awk and generally try to avoid it if I can. I'm even prone to just opening the file in vim and executing the replace command through that rather than using sed.

Re:Awk in 20 Minutes by a3n on Thursday January 15, 2015 @01:58PM (#8894562)
I would use sed for transformations (simple or complex) where I only care about one transformation (even though you can do multiple): Does the current line match this pattern? Change it to this other thing and move on the next line.

I would use awk when there are a handful or more of potential transformations: Does the current line match any of these multiple patterns? Do the action that's defined for each of the patterns, then move on to the next line.

If I need to do multiple transformations, and still want to use sed, I find it easiest to create a chain of single sed transformations, piped together. Somewhere in that area a shift to awk (or python) becomes justified.

Re:Awk in 20 Minutes by simpleigh on Thursday January 15, 2015 @02:45PM (#8894889)
I found sed great for messing around with SVN repository dumps. Delete a few lines here to remove the commit that added a directory... change a few paths to pretend the files in that directory had always been somewhere else... add a few lines somewhere else.

Sed scripts are a quick way to automate simple edits to large files.

Re:Awk in 20 Minutes by seanp2k2 on Friday January 16, 2015 @04:37AM (#8897950)
Does noting that you don't have to use / as the separator help with the syntactical pain? I personally always use |, since I'm unlikely to be using that for a pattern. Example:

grep foo somefile | sed 's|/path/to/some/file|/new/path|g'

Lot nicer than all those picket fences \/\/\/

Re:Awk in 20 Minutes by coliveira on Thursday January 15, 2015 @01:08PM (#8894189)
I use awk often, but rarely use sed, so I think they don't need to be always together.
Re:Awk in 20 Minutes by dfkf on Thursday January 15, 2015 @06:46PM (#8896250)
Re:Awk in 20 Minutes by jnazario on Thursday January 15, 2015 @05:59PM (#8896003)
at least in the awks built into osx and debian have sub and gsub (and gawk adds gensub).

       sub(r, t, s)
              substitutes  t  for the first occurrence of the regular expression r in the string s.  If s is
              not given, $0 is used.

       gsub   same as sub except that all occurrences of the regular expression are replaced; sub  and  gsub
              return the number of replacements.