Sponsored Content
Full Discussion: Good sed Book?
Top Forums Shell Programming and Scripting Good sed Book? Post 302730357 by DGPickett on Monday 12th of November 2012 04:55:26 PM
Old 11-12-2012
Well, sed is so simple, I taught myself. I find there are two flavors of script, loopers and filters. Loopers read most or all lines but the first using N, so they can see multiple lines in the buffer, and loop using branch features. Filters just do things on each line, as one mainly thinks of sed doing.

Awk has variables, arrays, hash tables, a sense of fields and records divorced from the line feed, which sed lacks, but is complex enough that one might just as well learn PERL, which is more able and orthogonal. Adding to the pifalls of awk is that there was an old awk and new awk, sometimes nawk, so examples may vary in dialect.

Most of sed is also usable in grep, vi, ex, and on the command line of ksh and bash when in 'set -o vi' mode, my favorite. I started on regex line editors with qedx in MULTICs, then used them in GCOS with University of Waterloo FRED the friendly editor, before I arrived in UNIX/vi land.

Regex are extended in several flavors, so what works in sed is more than basic grep but less that egrep/"grep -E" and that is different from awk.

You can make executable pure sed or awk files if the first line is "#!/bin/sed -f". I usually write sed right on the command line, barefoot inside ', like this extra blank (all white space) line remover, a classic looper (\t is a real tab, [f a real form feed):
Code:
sed '
  s/[ \t\f]*$//
  :loop
  $b
  N
  s/[ \t\f]*$//
  s/^\n$//
  t loop
  P
  s/.*\n//
  t loop
 '

The '$b' ensures that N is not run at EOF, losing a line on some early, defective sed implementations. Early proprietary sed was fixed buffer but faster than GNU and later sed's with realloc()-able indirect buffers. I often kept both around for script compatibility, renaming GNU sed 'gsed'. In one app, I turned whole pages into lines using 'tr' to swap line feeds and form feeds, so I could mark up the pages in sed to insert them into the database as big strings. I am not shy about running multiple sed's in a row, as looper sed and filter sed are not friends, and get some multiprocessing in the deal:
Code:
sed '
  s/[ \t\f]*$//
 ' | sed '
  :loop
  $b
  N
  s/^\n$//
  t loop
  P
  s/.*\n//
  t loop
 '

 

10 More Discussions You Might Find Interesting

1. Programming

Good book

I just want to know if someone can tell me if this book "C Programming Language (2nd Edition) by Brian W. Kernighan, Dennis M. Ritchie" is a good book to learn C on unix/linux ??? i'm an old (33 :)) mainframe programmer who wants to learn something else besides cobol and pl/1 ...... (2 Replies)
Discussion started by: pat
2 Replies

2. UNIX for Dummies Questions & Answers

Good Solaris Admin book??

Can anyone recommend a good Solaris 8 or 9 Admin book? (5 Replies)
Discussion started by: here2learn
5 Replies

3. Shell Programming and Scripting

Need a good scripting book

Just a quick request guys As you might have guessed I've just started getting involved in Unix The guys and the boss in the unix team (not with them yet) have given me some projects to do at my request. Some of which involve scripting. The work is paying for me to go on a scripting... (2 Replies)
Discussion started by: w33man
2 Replies

4. AIX

Need a Good AIX Book

I'm an AIX newby:eek: and need to learn fast (I go on a course in a few week's time but I need to know some stuff now:mad:). Can anybody recommend a good AIX book please? Not too basic though - I've been in software for many years (8bit/16bit/32bit, etc, Intel/Microsoft/FORTRAN/68000/anything... (9 Replies)
Discussion started by: Pennant Man
9 Replies

5. AIX

Good book for AIX

Hi guys, From where can i download a good book on AIX other than redbooks from IBM website. I am also looking for the below book. AIX 5L Administration By Randal K. Michael (3 Replies)
Discussion started by: shabu
3 Replies

6. UNIX for Dummies Questions & Answers

Good book on Unix

I'm learning about Unix on my mac through the terminal application. I like it quite a bit. I'm finishing the chapter on Unix from my Mac OS X the missing manual, and it's whetted my appetite. Can anyone recommend a good book on beginning Unix (starting at beginner to intermediate). I'd like... (1 Reply)
Discussion started by: Straitsfan
1 Replies

7. BSD

Good book about the freeBSD architecure

Hi Guys, I need some help in getting a good book that describes the internals of the freeBSD OS, like the architecure, the process and memory management, etc.. I have some book which is named : the design and implementation of the freeBSD operating system, but I feel it's somewhat... (2 Replies)
Discussion started by: marwan
2 Replies

8. Programming

Good book to learn C

I'd like to learn C but I wanted to ask if anyone knows of a good book to start with. I came across some folks who said the best one is 'The C programming language, second edition' but some reviews said that it's not for beginners. I am learning Java and UNIX on my Mac and am familiar with... (6 Replies)
Discussion started by: Straitsfan
6 Replies

9. UNIX for Dummies Questions & Answers

Good book on Unix

Please suggest me few good books on Unix. I have currently purchased Unix Power Tools. (2 Replies)
Discussion started by: prashdeep
2 Replies

10. Shell Programming and Scripting

Looking for good book on awk

I am not sure if I am posting to the right forum but I would like to buy a book which goes into Awk in detail and covers the most advanced Awk programming techniques. Would anybody be able to recommend a good book? I see plenty of books available on Amazon but I am not sure how detailed they are.... (2 Replies)
Discussion started by: kieranfoley
2 Replies
All times are GMT -4. The time now is 01:45 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy