sed - multi-line regex syntax eval puzzle


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed - multi-line regex syntax eval puzzle
# 1  
Old 09-08-2010
sed - multi-line regex syntax eval puzzle

sed novice bashing away at this....
I am trying to build a sed script that will find the instances of "cn" that have more than one "DirXML" value on them.... see sample below:

I am not having any luck with any variation that tries to find "DirXML.*\nDirXML.*". Isn't there a way to get sed to look at more than one line at a time for a regex value --> sed '/regex/M, /xyz/,/do something' ???
sed '$!N; ....
I guess I just don't know what syntax to use to build the multi-line regex.

Ideally I want the output to be just the cn's that have more than one "Dir" on the liners after them, and just show the cn.... just doesn't seem like it should be so hard.


source file:
cn: kyu
DirXML: cn=ADtoIDV,o=CHWIDV#1#d7663c550d5b9
DirXML: cn=ADtoIDV,o=CHWIDV#1#d7363c551da34
cn: jzaragoza
DirXML: cn=ADtoIDV,o=CHWIDV#1#e22dce6c74771
cn: cburgin001
DirXML: cn=ADtoIDV,o=CHWIDV#1#2d275ff94f3cf
cn: rgallerdo
DirXML: cn=ADtoIDV,o=CHWIDV#1#222b511426575
DirXML: cn=ADtoIDV,o=CHWIDV#1#245b51142fd32
cn: cjacobsen
DirXML: cn=ADtoIDV,o=CHWIDV#1#8fad4f11906
# 2  
Old 09-08-2010
Code:
awk 'BEGIN{RS="cn:";FS="\n"} NF>3 {print RS,$1} ' infile

cn:  kyu
cn:  rgallerdo


Last edited by rdcwayx; 09-09-2010 at 02:22 AM..
This User Gave Thanks to rdcwayx For This Post:
# 3  
Old 09-09-2010
Quote:
Originally Posted by gosleddog
...
Ideally I want the output to be just the cn's that have more than one "Dir" on the liners after them, and just show the cn....
Code:
$ 
$ # slightly changed data file to check for boundary conditions
$ cat f4
cn: kyu
DirXML: cn=ADtoIDV,o=CHWIDV#1#d7663c550d5b9
DirXML: cn=ADtoIDV,o=CHWIDV#1#d7363c551da34
cn: jzaragoza
DirXML: cn=ADtoIDV,o=CHWIDV#1#e22dce6c74771
cn: cburgin001
DirXML: cn=ADtoIDV,o=CHWIDV#1#2d275ff94f3cf
cn: rgallerdo
DirXML: cn=ADtoIDV,o=CHWIDV#1#222b511426575
DirXML: cn=ADtoIDV,o=CHWIDV#1#245b51142fd32
DirXML: cn=ADtoIDV,o=CHWIDV#1#245b51142fd32
DirXML: cn=ADtoIDV,o=CHWIDV#1#245b51142fd32
cn: cjacobsen
DirXML: cn=ADtoIDV,o=CHWIDV#1#8fad4f11906
$ 
$ 
$ perl -lne 'if (/^cn/){$x=$_; $in=1; $i=0} elsif($in and /^DirXML/){$i++} if($i>1){print $x; $in=0; $i=0}' f4
cn: kyu
cn: rgallerdo
$ 
$ 

tyler_durden

Last edited by durden_tyler; 09-09-2010 at 02:41 AM..
This User Gave Thanks to durden_tyler For This Post:
# 4  
Old 09-09-2010
I didn't even think to use awk - I use awk quite often to extract columns -- never dawned in me to use it to pull rows - never used RS before, but I will now... - Thanks!
Thanks for the perl example as well - will definitely be putting this to use.
[ the first rule of perl club is you do not talk about perl club....]
# 5  
Old 09-09-2010
Quote:
Originally Posted by gosleddog
...[ the first rule of perl club is you do not talk about perl club....]
The second rule is: you DO NOT talk about Perl Club. And the eighth and final rule: if this is your first time here, you have to answer a question. Smilie

tyler_durden
# 6  
Old 09-09-2010
MySQL

Quote:
Originally Posted by gosleddog
Isn't there a way to get sed to look at more than one line at a time for a regex value --> sed '/regex/M, /xyz/,/do something' ???
Yes , just everytime there is a change in sed Smilie but sometimes use sed can be difficult to others (awk,perl,,,).and and you can use atpresent methods,functions in the others..there are may some difficults especially contain same number or same alpha character line in which method your want (between lines method)
but we write specific sed script for this Smilie

however you said
sed '/regex/M, /xyz/,/do something' --> sed cant know what value would process for this you must give specific values for example

for exa
Code:
# sed -n '/cn: kyu/,/cn: jzaragoza/{;/cn: jzaragoza/!p}' infile | sed ';N;N;s/\(.*\)\nDir.*\n.*/\1/'
cn: kyu

the code if "cn: kyu" and "cn: jzaragoza" between lines if contains two DIR lines (for more lines a maybe can a write specific script -> like for 3 lines N;N;N or we find line number between our values and then set a value for this [x=N;N;N;] and then use "$x" instead of values ...... Smilie)

or can write with hold buffer Smilie
Code:
# cat infile
cn: kyu_OK
DirXML: cn=ADtoIDV,o=CHWIDV#1#d7663c550d5b9
DirXML: cn=ADtoIDV,o=CHWIDV#1#d7363c551da34
cn: jzaragozaNOTOK
DirXML: cn=ADtoIDV,o=CHWIDV#1#e22dce6c74771
cn: cburgin001_OK
DirXML: cn=ADtoIDV,o=CHWIDV#1#2d275ff94f3cf
DirXML: cn=ADtoIDV,o=CHWIDV#1#2d275ff94f3cf
DirXML: cn=ADtoIDV,o=CHWIDV#1#2d275ff94f3cf
cn: rgallerdo_OK
DirXML: cn=ADtoIDV,o=CHWIDV#1#222b511426575
DirXML: cn=ADtoIDV,o=CHWIDV#1#222b511426575
DirXML: cn=ADtoIDV,o=CHWIDV#1#222b511426575
DirXML: cn=ADtoIDV,o=CHWIDV#1#222b511426575
cn: kyu_NOTOK
DirXML: cn=ADtoIDV,o=CHWIDV#1#d7663c550d5b9
cn: cjacobsen_OK
DirXML: cn=ADtoIDV,o=CHWIDV#1#8fad4f11906
DirXML: cn=ADtoIDV,o=CHWIDV#1#8fad4f11906
cn: jzaragoza_NOTOK
DirXML: cn=ADtoIDV,o=CHWIDV#1#e22dce6c74771
cn: rgallerdox_OK
DirXML: cn=ADtoIDV,o=CHWIDV#1#222b511426575
DirXML: cn=ADtoIDV,o=CHWIDV#1#222b511426575
DirXML: cn=ADtoIDV,o=CHWIDV#1#d7663c550d5b9
DirXML: cn=ADtoIDV,o=CHWIDV#1#d7663c550d5b9
DirXML: cn=ADtoIDV,o=CHWIDV#1#d7663c550d5b9

Code:
# sed -ne :a -e '/cn:/{h;n;/Dir/n;{/cn:/ba;/Dir/x;P}}' infile
cn: kyu_OK
cn: cburgin001_OK
cn: rgallerdo_OK
cn: cjacobsen_OK
cn: rgallerdox_OK

Welcome sed club Smilie
*Powerfull Sed*

ygemici
regards
# 7  
Old 09-13-2010
awesome - thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get multi-line sed not to run if regex is found

Hello, everyone. Thanks for taking the time to read my post. I have nagios config files for which I'm adding the custom variable _mac_address. I have a sed script that places this variable into an existing file. The problem I'm having is if a line in the file is commented out, I don't want the... (2 Replies)
Discussion started by: JimBass
2 Replies

2. Shell Programming and Scripting

Another sed Syntax Puzzle . . .

Greetings! Have a quick question for the community today; this time looking at a nifty little sed puzzle ;) Consider the following file content to be worked through:What needs to happen is theblock should be removed up to and including the following blank line, leavingI have bits and pieces... (8 Replies)
Discussion started by: LinQ
8 Replies

3. Shell Programming and Scripting

Echo multi-line string via heredoc syntax

$ cat bashtest #!/usr/local/bin/bash echo <<<"EOF" line1 line2 line3 EOF $ ./bashtest ./bashtest: line 3: line1: command not found ./bashtest: line 4: line2: command not found ./bashtest: line 5: line3: command not found ./bashtest: line 6: EOF: command not found What am i doing... (4 Replies)
Discussion started by: urello
4 Replies

4. Shell Programming and Scripting

Multi line regex for search and replace

I have text file like below: a.txt Server=abc Run=1 Time=120.123 Tables=10 Sessions=16 Time=380.123 Version=1.1 Jobs=5 Server=abc Run=2 Time=160.123 Tables=15 Sessions=16 Time=400.258 Version=2.0 (1 Reply)
Discussion started by: sol_nov
1 Replies

5. Shell Programming and Scripting

sed multiple multi line blocks of text containing pattern

Hi, I have a log file which has sessionids in it, each block in the log starts with a date entry, a block may be a single line or multiple lines. I need to sed (or awk) out the lines/blocks with that start with a date and include the session id. The files are large at several Gb. My... (3 Replies)
Discussion started by: andyatit
3 Replies

6. Shell Programming and Scripting

sed to replace a line with multi lines from a var

I am trying to find a line in a file ("Replace_Flag") and replace it with a variable which hold a multi lined file. myVar=`cat myfile` sed -e 's/Replace_Flag/'$myVar'/' /pathto/test.file myfile: cat dog boy girl mouse house test.file: football hockey Replace_Flag baseball ... (4 Replies)
Discussion started by: bblondin
4 Replies

7. Shell Programming and Scripting

Multi-line filtering based on multi-line pattern in a file

I have a file with data records separated by multiple equals signs, as below. ========== RECORD 1 ========== RECORD 2 DATA LINE ========== RECORD 3 ========== RECORD 4 DATA LINE ========== RECORD 5 DATA LINE ========== I need to filter out all data from this file where the... (2 Replies)
Discussion started by: Finja
2 Replies

8. Shell Programming and Scripting

EVAL syntax problems

Hi there As part of a larger script I need to put the output of an ls into a variable which has an incremental number. ie nathan@nathan-Vostro-1700:~$ eval 'proc'$val='`ls -ld /proc/9467`' nathan@nathan-Vostro-1700:~$ echo $proc0 dr-xr-xr-x 8 nathan nathan 0 2012-05-02 09:21... (3 Replies)
Discussion started by: nathan.harris
3 Replies

9. Shell Programming and Scripting

perl regex multi line cut

hello mighty all there's a file with lots of comments.. some of them looks like: =comment blabla blablabla bla =cut i'm trying to cut this out completely with this code: $line=~s/^=.+?=cut//sg; but no luck also tryed to change it abit but still I don't understand how the... (9 Replies)
Discussion started by: tip78
9 Replies

10. Shell Programming and Scripting

sed: delete regex line and next line if blank

Hi, I want to write a sed script which from batiato: batiato/giubbe: pip_b.2.txt pip_b.3.txt pip_b.3mmm.txt bennato: bennato/peterpan: 123.txt consoli: pip_a.12.txt daniele: (2 Replies)
Discussion started by: one71
2 Replies
Login or Register to Ask a Question