Sponsored Content
Top Forums Shell Programming and Scripting sed - multi-line regex syntax eval puzzle Post 302452276 by ygemici on Thursday 9th of September 2010 07:55:20 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 04:11 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy