Berkeley CSUA MOTD:Entry 31106
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2025/07/09 [General] UID:1000 Activity:popular
7/9     

2004/7/1-2 [Computer/SW/Unix] UID:31106 Activity:insanely high
7/1     Using sed, how do I do the following?  I want to replace the
        first line containing a particular pattern with the 2nd line
        containing said pattern.  For example, if "line" is the pattern,
        then I want
                this is the 1st line
                this is the 2nd
                this is the 3rd
                this is the 4th line
        to become
                this is the 4th line
                this is the 2nd
                this is the 3rd
                this is the 1st line  (Actually, I don't care if this line
                                       shows up like this or gets deleted.)
        \_ stupid to do this in sed.  use perl.
          \_ OK, how do I do this in perl?
             \_ while ( $line = <> ){
                        if ( $line !~ /line/ ) {
                                $output[$count++]=$line;
                                next;
                        }
                        if ( ! $firstline ) {
                                $firstline = $line;
                                $firstindex = $count++;
                        } else {
                                $output[$firstindex]=$line;
                                $output[$count++]=$firstline;
                        }
                }
                print foreach @output;
                # (Approximately)  -tom
                \_ You don't need the "foreach"
          \_ Just for fun, how can this be done using sed?
             \_ sed is short for "stream editor".  It's designed to look at
                the input one line at a time.  making it handle line operations
                is unholy, and possibly illegal in most states.
             \_ there is nothing fun about doing this in sed.  -tom
                \_ lots of things can be fun if you're avoiding more important
                   work. here it is:

        sed -e '/line/\!bS;:M;$\!N;s/\n.*line/&/;tE;$\!bM;:E;s/^\([^\n]*\)\(\n.
*\n\)\([^\n]*\)$/\3\2\1/;:S'

                   this swaps the 2N-1'th and the 2N'th lines matching "line"
                   for all possible N. if there is an odd number of such
                   lines, the last one is left in place. note that the
                   3 "!"'s are \-escaped for tcsh; your shell might
                   differ. naturally, s/line/foo/g to change pattern.
                   tested on gnu sed 3.02. inserting a few newlines will
                   make this work on soda's sed as well:
                        sed -e '/line/\!bS\
                        :M\
                        $\!N;s/\n.*line/&/;tE\
                        $\!bM\
                        :E\
                        s/^\([^\n]*\)\(\n.*\n\)\([^\n]*\)$/\3\2\1/;\
                        :S'
                                                         -alexf
                               \_ alexf = leet sed god
                                  \_ s/leet sed god/bored new sed user/ -alexf
                \_ sed rules!!!
                   \-first, i probably wouldnt do this in sed either.
                     it may be doable in sed but your description isnt
                     complete, so i cant suggest a solution. do you just
                     want to *swap* 1st match of regexp with second match
                     of regexp? finally, sed can certainly do non-line
                     based things more easily than say grep. see e.g.
                     the , operator. --psb
                        \_ I want the first line that matches regexp to
                           be replaced with the 2nd line that contains
                           regexp.  The entire line need not match regexp,
                           but only contain it.  But I want the entire
                           first line (that contains a match to regexp) to
                           be essentially overwritten by the next line that
                           contains a match to regexp.  -op
        \_ rpn calculator written in sed:
           http://sed.sourceforge.net/local/scripts/dc.sed.html
        \_ just curious, why do you want to do this? seems kinda weird.
           \_ he's trying to troll ilyas into writing it in ocaml
2025/07/09 [General] UID:1000 Activity:popular
7/9     

You may also be interested in these entries...
2012/9/20-11/7 [Computer/SW/Unix, Finance/Investment] UID:54482 Activity:nil
9/20    How do I change my shell? chsh says "Cannot change ID to root."
        \_ /usr/bin/chsh does not have the SUID permission set. Without
           being set, it does not successfully change a user's shell.
           Typical newbie sys admin (on soda)
           \_ Actually, it does: -rwsr-xr-x 1 root root 37552 Feb 15  2011 /usr/bin/chsh
	...
2012/9/24-11/7 [Computer/SW/Languages, Computer/SW/Unix] UID:54484 Activity:nil
9/24    How come changing my shell using ldapmodify (chsh doesn't work) doesn't
        work either? ldapsearch and getent show the new shell but I still get
        the old shell on login.
        \_ Scratch that, it magically took my new shell now. WTF?
           \_ probably nscd(8)
	...
2012/4/27-6/4 [Computer/SW/Languages/Misc, Computer/SW/Unix] UID:54372 Activity:nil
4/27    I wrote a little shell script to collect iostat data:
        #!/bin/bash
        DATE=`date +%m%d`
        DATADIR=/var/tmp/user
        OUTPUTFILE=$DATADIR/$DATE.out
        while true
	...
2011/10/26-12/6 [Computer/SW/Unix] UID:54202 Activity:nil
10/24  What's an easy way to see if say column 3 of a file matches a list of
       expressions in a file? Basically I want to combine "grep -f <file>"
       to store the patterns and awk's $3 ~ /(AAA|BBB|CCC)/ ... I realize
       I can do this with "egrep -f " and use regexp instead of strings, but
       was wondering if there was some magic way to do this.
       \_ UNIX has no magic. Make a shell script to produce the ask or egrep
	...
2011/5/19-7/13 [Computer/SW/Languages/Misc] UID:54115 Activity:nil
5/19    If script A runs, and calls script B ..... is it possible for me to exit\
        script A based on results of script B and not continue?
        \_ assume any shell
        \_ Yes.
           \_ without passing the result to some stupid temp file?
              \_ It sounds like you want "scriptb || exit", which will run
	...
2009/8/19-9/1 [Computer/SW/Unix] UID:53285 Activity:nil
8/18    Hi again, new freebsd guy here again, in bash I was able to go
        LD_LIBRARY_PATH=/opt/foo/lib ./runmyapp
        I managed to do this in tcsh by using setenv in a shell script
        that setenv's the lib path and then executes $1, just wondering
        if there was a way to do it in 1 line from the cmd line as in bash?
        Thanks, btw %2c or %3c worked.  Freebsd, tcsh and vi forever!
	...
2009/7/22-27 [Computer/SW/Unix] UID:53181 Activity:nil
7/22    Why does everyone's 'mail last read' date say Jul 19th? even for people
        who don't log in (shell is safesorry)?  Just wondering O mighty unix
        gurus.
        \_ Modification time change when it was copied to new soda.
	...
Cache (209 bytes)
sed.sourceforge.net/local/scripts/dc.sed.html
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.