The solution to the sed - multiple lines problem

The solution to the sed - multiple lines problem

Post by b.. » Fri, 02 Mar 1990 20:11:00



This is the solution of the "sed - matching multiple lines on input" problem
which I posed a few days ago.  I thought I'd post it to the net to let people
know I've solved it and just how to do it.
Given the following input:

one
two
three

and the following sed editor script:

s/one\ntwo\nthree/one, two, three/g

Why don't it work?  Well here's my hypothesis:
        Sed reads the first line which contains the string "one\n" and
discards the newline.  So the pattern space looks like "one".  Of course the
pattern "one\ntwo\nthree" won't match that.  So the second line gets read
and the same thing happens, then the third line and so nothing ever matches.
The solution was to build up the pattern space by using the "hold buffer".
The following sed script does the job:

/one/!b
N
/two/!b
N
/three/!b
s/\n/, /g

See the sed writeup to understand the commands !, b and N.  Thanks to all who
responded with positive suggestions.

--Bill

 
 
 

The solution to the sed - multiple lines problem

Post by ch.. » Fri, 02 Mar 1990 04:32:00



>The following sed script does the job:
>/one/!b
>N
>/two/!b
>N
>/three/!b
>s/\n/, /g

Alas, this script fails to convert the input

        one
        one
        two
        three
        four

to

        one
        one, two, three
        four

While I think it is possible to handle this case, and its counterpart
`one\ntwo\none\ntwo\nthree', I am not going to work on it.
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)


 
 
 

1. Solution: multiple Apache log files, multiple args to env=, multiple conditions to CustomLog directive

Here is a solution to a problem I had a little while ago.  I wanted to
have Apache keep 3 separate log files: 1 for requests from me, a 2nd
for nimda virus attacks, and a 3rd for everything else.

The key to the solution was to use "!" to unset environment variables
in certain cases.  Joshua Slive pointed this out for me.

I'm posting this here because I can't find a way to use Google to
reply to those old posts of mine.

Here's an example that will log requests from a specific IP address
(presumably the webmaster's) to one file, and log nimda virus attacks
to another file.  A third log file will hold all the remaining
requests.

#========================================================================#
SetEnvIf Remote_Addr "w\.x\.y\.z" localaccess=1
SetEnvIf localaccess 1 dontlog

# Here, !localaccess will keep nimda requests out of the localaccess
log.
SetEnvIf Request_URI "^/scripts" nimda=1 !localaccess
SetEnvIf Request_URI "^/c/winnt" nimda=1 !localaccess
SetEnvIf Request_URI "^/_mem_bin" nimda=1 !localaccess
SetEnvIf Request_URI "^/_vti_bin" nimda=1 !localaccess
SetEnvIf Request_URI "^/MSADC" nimda=1 !localaccess
SetEnvIf Request_URI "^/msadc" nimda=1 !localaccess
SetEnvIf Request_URI "^/d/winnt" nimda=1 !localaccess
SetEnvIf nimda 1 dontlog

CustomLog logs/localaccess.log combined env=localaccess
CustomLog logs/nimda.log combined env=nimda
CustomLog logs/everythingelse.log combined env=!dontlog
#========================================================================#

I've received a couple emails about this problem, so I figure this
will help someone out.

-Anthony
www.nodivisions.com

2. Setting up X-Terminal Clients. Where to configure to which host it should connect and that it should connect to a remote host.

3. Extracting multiple lines OR deleting multiple lines from a file using AWK

4. mouse problems w/xfree86 3.2

5. Multiple-line dial-up solutions needed

6. does proxy arp not work in solaris 2.3 or 2.4?

7. Grep/awk/sed for multiple/groups of lines in a file???

8. Linux Users/Brisbane

9. Using Sed and Shell Variables in Multiple Lines Search and Replace using /c\

10. Fold multiple lines with sed or awk

11. Grep/awk/sed for multiple/groups of lines in a file???

12. Pattern Matching Multiple Lines with SED and AWK