Sponsored Content
Full Discussion: Good sed Book?
Top Forums Shell Programming and Scripting Good sed Book? Post 302731705 by DGPickett on Thursday 15th of November 2012 03:28:22 PM
Old 11-15-2012
So, it is a area where behavior is not trustworthy, and thus I never go there! Files with no line feed right before EOF tend to have that last "line" ignored by sed. Maybe that's POSIX, too. I think EOF, new line and form feed should all be treated as end of line, but it is a bit late, never mind those MAC people with just carriage return and the DOS people with both. Both made sense for teletype: the cariage took more time to return 80 columns than the platen to rise one line, so it was sent on its way first.

Since sed is pretty easy about white space, I put sed on different lines than shell, indented meaningfully, and so have never needed one or more -e options! You, too, are worthy of well formatted code, reducing your errors, potential confusion and that of future maintainers.

I have never used 'G' and 'H' or space exchange but g and h are nice for parsing situations where something is missing, so you want to annotate the original line with an error prefix and write it to a reject log. You h it on the way in, in case of rejections, and upon rejection, g it before annotation on the way out. Similarly, usually I do not use 'D', but 's/.*\n//' so the second line is not released.

The 't' is a great time saver, as the s can both modify and recognize '/../' what had been there with one regex search. Just make sure, especially in a looper, that it gets cleaned out before reuse, as the flag reflects all s since the last t or automatic read.

I have been warming up to the -n and 's/.../.../p' lately, as it fits many situations (frequency not variety), but initially I ignored them as I was interested in the most versatile tactics.

I would note that many sed flavors do not tolerate comments # whatever, which is a shame. Inline documentation can help maintenance. In C, C++, JAVA, shell and SQL I like the switch/case/when/then/else, as each case can be commented neatly!

In data warehouses and similar places with crushingly big data sets, sed's lack of temp files and near-C speed are very well respected. It has a very important role to play in a pipe-oriented shell programming paradigm, where there are no intermediate or temp files, or any temp files are managed by the tool like sort. This results in lower latency and pipeline parallel multiprocessing, as many steps run concurrently.

Using literal '|' and named pipes (/sbin/mknod p p -- one of those p's is a file name), especially the self-managed named pipes '<(...)' and '>(...)' in bash and luckier systems' ksh, you can build a tree of pipelines working one or many inputs to produce one or many outputs. (On unlucky systems, bash makes named pipes somewhere under /var/tmp that accumulate, a bug I reported.) Unfortunately for sed, the self-managed named pipes '<(...)' and '>(...)' are parsed as words in ksh (according to David Korne) and probably bash; they have virtual spaces around them that you cannot erase without passing them through a shell function call or the like. Life is sometimes excessively complicated! In the following example, the first '>(...)' after a 'w' command in $1 does not work, might resolve to, essentially, ' /dev/fd/3 ' (the writable fd number from a pipe() call), so the pipe's file name '/dev/fd/3' is unrecognized sed command line option $2, the next part of the sed script is $3 and the next named pipe, perhaps '/dev/fd/5', is $4:
Code:
$ sed '
  /xyz/w '>( sort -u >file_1 )'
  /abc/w '>( sort -u >file_2 )'
 .... '


Last edited by DGPickett; 11-15-2012 at 05:21 PM..
 

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
NWBPSET(1)							      nwbpset								NWBPSET(1)

NAME
nwbpset - Create a bindery property or set its value SYNOPSIS
nwbpset [ -h ] [ -S server ] [ -U user name ] [ -P password | -n ] [ -C ] DESCRIPTION
nwbpset Reads a property specification from the standard input and creates and sets the corresponding property. The format is determined by the output of 'nwbpvalues -c'. nwbpset will hopefully become an important part of the bindery management suite of ncpfs, together with 'nwbpvalues -c'. See util/nwbpsecurity for an example. As another example, look at the following command line: nwbpvalues -t 1 -o supervisor -p user_defaults -c | sed '2s/.*/ME/'| sed '3s/.*/LOGIN_CONTROL/'| nwbpset With this command, the property user_defaults of the user object 'supervisor' is copied into the property login_control of the user object 'me'. nwbpvalues -t 1 -o me -p login_control -c | sed '9s/.*/ff/'| nwbpset This command disables the user object me. Feel free to contribute other examples! nwbpset looks up the file $HOME/.nwclient to find a file server, a user name and possibly a password. See nwclient(5) for more information. Please note that the access permissions of $HOME/.nwclient MUST be 600 for security reasons. OPTIONS
-h -h is used to print out a short help text. -S server server is the name of the server you want to use. -U user user is the user name to use for login. -P password password is the password to use for login. If neither -n nor -P are given, and the user has no open connection to the server, nwbpset prompts for a password. -n -n should be given if no password is required for the login. -C By default, passwords are converted to uppercase before they are sent to the server, because most servers require this. You can turn off this conversion by -C. AUTHORS
nwbpset was written by Volker Lendecke. See the Changes file of ncpfs for other contributors. nwbpset 8/7/1996 NWBPSET(1)
All times are GMT -4. The time now is 11:45 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy