Need Help with a sed command involving Regex


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Help with a sed command involving Regex
# 8  
Old 11-29-2013
In awk (as sed algo):
Code:
awk '/0-|-0/{print X"\n"$0;exit};/1234/{X=$0}' file

Regards.

---------- Post updated 29-11-13 at 09:23 AM ---------- Previous update was 28-11-13 at 09:13 PM ----------

Quote:
Originally Posted by disedorgue
Hi,
I don't all understand, but try:
Code:
sed -n '/0-/{x;p;x;p;q;};/-0/{x;p;x;p;q;};/1234/x' file

Regards.
-n : only lines explicitly selected for output are written.
here, we treat three pattern: /0-/,/-0/ and /1234/.
For the patterns /0-/ and /-0/, we do code block x;p;x;p;q where:
x : Exchange the contents of the pattern and hold space
p : Write the pattern space to standard output.
q : Quit
For the pattern /1234/ we exchange the contents of the pattern and hold space.
When sed begin a new cycle, the pattern space is empty and the new line will insert in. The hold space is inchanged.
The idea is to catch the pattern /1234/ in hold space and write to output the hold space before the pattern space when the pattern /0-/ or /-0/ are detected and to quit.

Regards.
# 9  
Old 11-29-2013
Thanks disedorgue...very well explained....appreciate the clarity of your expression
# 10  
Old 11-29-2013
If the pattern is always 0-...-0 you can shorten this to
Code:
sed -n -e '/0-.*-0/{x;p;x;p;q;}' -e '/1234/h' file

I have chosen two -e "lines" instead of a ";" separator, so it works with all sed versions.
# 11  
Old 11-30-2013
Thanks Made In Germany,
I will give a it a try to interpret it.Please comment on any mistakes on my part.
1. Look for regex 1234, if true, then pass this to hold buffer.
2. Look for pattern 0-.*-0, if true then exchange, hold buffer goes to pattern space and print pattern space. This prints line containing 1234
3.Then again exchange and print .This prints line containing 0-.*-0 .
4.Then quit. This ensures that ONLY the first instance of the occurrence gets printed.
Rgds
SShinde

---------- Post updated at 04:14 AM ---------- Previous update was at 04:07 AM ----------

Just another thought,
Supposing I were looking to print the FIRST THREE instances of the pattern.
How would the sed command look like?
Rgds
SShinde
# 12  
Old 11-30-2013
You got it, well explained!
--
sed is limited - no variables that can be used as counters.
If you have a "do it n-times" requirement then it's time for awk or perl.
BTW sed has a n-times modifier for its s command, and its buffers have multi-line capability, so there might be even an artistic solution...

Last edited by MadeInGermany; 11-30-2013 at 06:25 AM..
# 13  
Old 11-30-2013
Artistic solution as here (in red, all pattern find (five) , but print only FIRST FOUR print - see red value in sed command -)
But here, this syntax is not for all sed version:
Code:
$ cat co.yy
rwerwerwe rere
fdfefefe fsdfds
rerere ffff trtrt
aaaa 1234 asadsdsd
Xaaa 1234 asadsdsd
Aaaa 1234 asadsdsd
hfjfjfjsjfsf shfshfsjhshs
dssssdsdsdd
dsgdshgsdgdsdgssgdsgdsdhsgdshgshdshdg
akjjkjkj         0---+---0
gfhdfgdfhfgdhhfgdhfgd
sgdhsgdhsdgsdhgsdgsdhhdg
b1234hjhhh ghgsggdgdg
Yaaa 1234 asadsdsd
dddhhdjdhdh
Baaa 1234 asadsdsd
bdagdhdgahadgadg  0---0---+
dagdhdgahadgadg  00+
rwerwerwe rere
fdfefefe fsdfds
rerere ffff trtrt
caaaa 1234 asadsdsd
hfjfjfjsjfsf shfshfsjhshs
dssssdsdsdd
dsgdshgsdgdsdgssgdsgdsdhsgdshgshdshdg
cjjkjkj         0---+---0
gfhdfgdfhfgdhhfgdhfgd
sgdhsgdhsdgsdhgsdgsdhhdg
d1234hjhhh ghgsggdgdg
dddhhdjdhdh
dagdhdgahadgadg  0---0---+
dagdhdgahadgadg  00+
d1234hjhhh ghgsggdgdg
dagdhdgahadgadg  0---0---+

Code:
$ sed -n -e '/1234/H' -e '/0-.*-0/H' -e '${x' -e 's/^\n//' -e 's/$/\n/' -e ':loop' -e 's/\([^\n]*1234[^\n]*\n\)\([^\n]*1234[^\n]*\n\)/\2/' -e 't loop' -e 's/\(\([^\n]*1234[^\n]*\n[^\n]*\(0-\)*\(-0\)*[^\n]*\n\)\{4\}\).*/\1/;s/\n$//p' -e '}' co.yy
Aaaa 1234 asadsdsd
akjjkjkj         0---+---0
Baaa 1234 asadsdsd
bdagdhdgahadgadg  0---0---+
caaaa 1234 asadsdsd
cjjkjkj         0---+---0
d1234hjhhh ghgsggdgdg
dagdhdgahadgadg  0---0---+

# 14  
Old 12-02-2013
SmiliePhew!Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regex with sed

hi i would like to say "DATABASENAME=" to "TABLESNAME=" remove "," and press enter myconfig file thanks (1 Reply)
Discussion started by: mnnn
1 Replies

2. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

3. Shell Programming and Scripting

Help with sed substitution / regex

Hi all, please can anyone show me how to use sed and regular expressions to achieve the following. If a line contains a capital A followed by exactly 5 or 6 characters followed by an angled bracket then insert an asterix before the angled bracket. So: XCONFIGA12345<X Becomes: ... (5 Replies)
Discussion started by: Jedimark
5 Replies

4. UNIX for Advanced & Expert Users

Sed regex problem

Hi, I tried to extract the time from `date` with sed. (I know it works with `date +%H:%M:%S` as well) I got three solutions of which just one worked. I thought "+" should repeat the previous expression 1 or more times and {n} should repeat the previous expression n times. $ date Thu... (9 Replies)
Discussion started by: thiuda
9 Replies

5. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

6. Shell Programming and Scripting

Using SED command in a shell script: Unterminated address regex

Hi All, I am trying to use a sed command in a shell script in order to delete some lines in a file and I got the following error message. I don't understand why it is not working 'cause I have tried with simple quotes, then with double-quotes, and it is not working. sed: -e expression #1,... (7 Replies)
Discussion started by: Alpha3363
7 Replies

7. Shell Programming and Scripting

SED command help: Can we pass predefined variables in place of regex

Hi All, I have a doubt. Can we assign a regular expression for pattern searching to a variable in a script and then use that variable in place of a regular expression in sed command.I tried but got some syntax error!!Is it not possible.Because my requirement is that i have a generic script to get... (8 Replies)
Discussion started by: usha rao
8 Replies

8. Shell Programming and Scripting

sed - using regex and | need help

From my understanding when using regex1|regex2 the matching process tries each alternative in turn, from left to right, and the first one that succeeds is used. When im trying to extract the name from those examples: A) name.can.be.different.20.03.2009.boom B)... (2 Replies)
Discussion started by: TehOne
2 Replies

9. Shell Programming and Scripting

Sed and regex help needed

Hi all, I'm writing a script that replaces a value in a file. The file is formatted as follows: So, for this example, I'd like to replace the value for param_two. The value for param_two can be a one, or two-digit number. It replaces the value in file.cfg, and directs the... (9 Replies)
Discussion started by: marknu1
9 Replies

10. UNIX for Dummies Questions & Answers

sed regex

I would like to do this: replace the word "prod" with the word "special" but it may occur through the file naturally without a command, I only want it to happen when it has a specific command in front of it. The command will always look like this &lt;IMG,###,###,##,&gt;prod/directory/IMG/file ... (4 Replies)
Discussion started by: Shakey21
4 Replies
Login or Register to Ask a Question