2

When I was studying bash, I found it very helpful to go and study the bash scripts already present on a clean install of Linux—/etc/profile, for instance, and anything in /etc/rc.d/init.d/. These are often quite advanced scripts, and by studying them I ensured I learned about many obscure features not covered in most bash tutorials.

I am studying sed now, and although the list of sed features is much shorter (so I know for a fact I have studied all of the features), I still feel it would be very beneficial to study through some sed scripts that are actually used in production, and are not just examples in tutorials.

To that end, I would really like to study any sed scripts that are already present on a well-known Linux distro, such as Ubuntu or CentOS. Trouble is, I have no idea where such scripts might be. I've already tried file /bin/* | grep script | sed 's/:.*//' | xargs grep sed with no results. file /sbin/* | grep script | sed 's/:.*//' | xargs grep -c sed returns some results, and I'm looking through those, but all the ones I have checked so far are just sed one-liners embedded in bash or sh scripts.

Where can I find some actual sed scripts on my Linux machine? Or failing that, is there a good place online to find some sed scripts that are actually used in production? (Reading through sedtris won't help much for my purposes. ;)

Wildcard
  • 36,499
  • what you're asking for typically doesn't exist except, as you've already discovered, as one-liners in sh or bash scripts. see also http://unix.stackexchange.com/questions/2434/is-there-a-basic-tutorial-for-grep-awk-and-sed – cas Oct 24 '15 at 04:02
  • I don't believe people are insane enough to write entire scripts in sed. (Not in a mainstream distro, anyway.) – muru Oct 24 '15 at 04:39
  • ive always found sedtris edifying. if you have GNU sed, you can look at some of the scripts in info sed. you could look at math.sed. i seriously doubt youll find any examples worth following in any scripts distributed by default with linux distributions. you would find some, though, if you looked hard at a BSD Ports tree and its constituent makefiles. – mikeserv Oct 24 '15 at 06:40

3 Answers3

3

Try:

grep -w sed /etc/init.d/*
grep -w sed /etc/grub.d/*
grep -w sed /usr/bin/*

The first yields 25 scripts and the second 47 on my system (debian).

The -w option restricts grep to looking for sed as a whole word. This way, matches to words like used or supposedly are avoided.

John1024
  • 74,655
2

Rather than look in the operating system, look at scripts used for building the programs.

Any autoconf-generated configure script contains sed-scripts, and itself generates a script which contains a sed-script. That substitutes values for names in the template files such as Makefile.in to produce a Makefile. For example (conveniently found with web search) this chunk of a config.status file for distcc illustrates how sed is used:

    while :; do
      case $as_dir in #(
      *\'*) as_qdir=`$as_echo "$as_dir" | sed "s/'/'\\\\\\\\''/g"`;; #'(
      *) as_qdir=$as_dir;;
      esac
      as_dirs="'$as_qdir' $as_dirs"
      as_dir=`$as_dirname -- "$as_dir" ||
$as_expr X"$as_dir" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
     X"$as_dir" : 'X\(//\)[^/]' \| \
     X"$as_dir" : 'X\(//\)$' \| \
     X"$as_dir" : 'X\(/\)' \| . 2>/dev/null ||
$as_echo X"$as_dir" |
    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
        s//\1/
        q
      }
      /^X\(\/\/\)[^/].*/{
        s//\1/
        q
      }
      /^X\(\/\/\)$/{
        s//\1/
        q
      }
      /^X\(\/\).*/{
        s//\1/
        q
      }
      s/.*/./; q'`
      test -d "$as_dir" && break
    done

Aside from autoconf, scripting is useful for other aspects of building (and installing) programs. The manlinks.sed script in ncurses is long enough to be considered as a program.

You can of course use grep to find sed used. However, it is common to use a ".sed" file-suffix for standalone scripts. "locate .sed" finds those.

In a quick check on my machine, there is one extremely long sed script (more than two thousand lines, counting comments):

#!/usr/5bin/sed -nf
#r
#r sokoban.sed - aurlio marinho jargas <aurelio@verde666.org>
#r Changes by Gunnar Ritter, October 2002.
#r
#r Sccsid @(#)sokoban.sed       1.7 (gritter) 10/12/03
#r
#r motivated by reading the amazing Adventure (Colossal Cave) history
#r      <http://www.rickadams.org/adventure/a_history.html>
#r GPL levels took from Mike Sharpe's sokoban.vim <http://vim.sourceforge.net>
#r
#r IMPORTANT
#r this script has terminal control chars, so you must DOWNLOAD this
#r file. just copy/paste or printing it to a file (lynx) will NOT work.
#r
#r THE GAME
#r you know sokoban. everybody knows sokoban.

(Someone made a colorized version of it).

That is one of three scripts used for testing sed in the "heirloom tools":

-rw-r--r--   1 tom           285 Oct  6 2002    makefile 
-rw-r--r--   1 tom          3664 Oct 11 2003    hanoi.sed
-rw-r--r--   1 tom          2338 Oct 11 2003    math.sed
-rw-r--r--   1 tom         42552 Oct 11 2003    sokoban.sed

Likewise, the sed program has an interesting example:

#!/bin/sed -nf
#  dc.sed - an arbitrary precision RPN calculator
#  Created by Greg Ubben <gsu@romulus.ncsc.mil> early 1995, late 1996
#
#  Dedicated to MAC's memory of the IBM 1620 ("CADET") computer.
#  @(#)GSU dc.sed 1.1 06-Mar-1999 [non-explanatory]
#
#  Examples:
#       sqrt(2) to 10 digits:   echo "10k 2vp" | dc.sed
#       20 factorial:           echo "[d1-d1<!*]s! 20l!xp" | dc.sed
#       sin(ln(7)):             echo "s(l(7))" | bc -c /usr/lib/lib.b | dc.sed
#       hex to base 60:         echo "60o16i 6B407.CAFE p" | dc.sed
#       tests most of dc.sed:   echo 16oAk2vp | dc.sed
#
#  To debug or analyze, give the dc Y command as input or add it to
#  embedded dc routines, or add the sed p command to the beginning of
#  the main loop or at various points in the low-level sed routines.
#  If you need to allow [|~] characters in the input, filter this
#  script through "tr '|~' '\36\37'" first (or use dc.pl).

Likewise, there is a page with a colorized version of dc.sed

However, autoconf and build-scripts are where you might find productive use of sed.

Further reading:

Thomas Dickey
  • 76,765
0

They are not on your machine, and they may not be used, but you could do worse than search through this very web site you are using, e.g. search for sed in body of answer with score of 5+, as they correspond to real-world problems answered.

meuh
  • 51,383