sed non-greedy pattern matching with wildcard


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sed non-greedy pattern matching with wildcard
# 1  
Old 01-20-2012
sed non-greedy pattern matching with wildcard

Code:
Toby> cat sample1
 
This is some arbitrary text before var1, This IS SOME DIFFERENT ARBITRARY TEXT before var2
 
Toby> sed -e 's/^This .* before //' -e 's/This .* before //' sample1
 
var2

I need to convert the above text in sample1 so that the output becomes

var1, var2

by stripping away two different sets of arbitrary characters that occur before var1 and var2.

My attempt above apparently fails because the use of the wildcard and * will greedily match all the way to the last occurence in the line instead of the first occurence.

Can anyone please give me the sed command that will work for this?

Thanks

Last edited by radoulov; 01-20-2012 at 12:38 PM.. Reason: Code tags, please!
# 2  
Old 01-20-2012
Code:
 sed 's/.*\(var1\).*\(var2\)/\1, \2/g' sample1

Or a similar one:
Code:
 grep -oE 'var.{1}' sample1

# 3  
Old 01-20-2012
Thanks for the reply. But your solution does not do what I need. My problem is more general. In my problem the var1 and var2 are not literal strings but are themselves arbitrary and need to have the text before them stripped away. Here is a clearer description of what I need

Toby> cat sample1
This is some arbitrary text before var1, This IS SOME DIFFERENT ARBITRARY TEXT before var2
This is another line with first part before item1, This is the second part before item2

Toby> sed 's/.*\(var1\).*\(var2\)/\1, \2/g' sample1
var1, var2

This is another line with first part before item1, This is the second part before item2

The output I need from sed would then be

var1, var2
item1, item2
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace String matching wildcard pattern

Hi, I know how to replace a string with another in a file. But, i wish to replace the below string pattern EncryptedPassword="{gafgfa}]\asffafsf312a" i.e EncryptedPassword="<any random string>" To EncryptedPassword="" i.e remove the random password to a empty string. Can you... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. Programming

Wildcard Pattern Matching In C

I've been having problems lately trying to do pattern matching in C while implementing wildcards. Take for instance the following code: #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <dirent.h> #include <string.h> ... (14 Replies)
Discussion started by: Azrael
14 Replies

3. Shell Programming and Scripting

Non-greedy pattern matching in shell script

Hi all, Is Perl included by default in Ubuntu? I'm trying to write a program using as few languages as possible, and since I'm using a few Perl one-liners to do non-greedy matching, it's considered another language, and this is a bad thing. Basically, I'm using a Perl one-liner to grab XML... (3 Replies)
Discussion started by: Zel2008
3 Replies

4. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

5. Shell Programming and Scripting

Help with sed pattern matching

Hi My log file is Testtmp2 cat Testtmp2 12:12:38 12:14:29 12:17:34 12:19:08 12:20:10 12:21:35 12:22:20 12:22:26 12:22:34 12:22:38 12:28:14 12:31:35 12:32:50 12:33:04 (3 Replies)
Discussion started by: rahkumar
3 Replies

6. Shell Programming and Scripting

SED pattern matching help

Hello All, I have the following lines in a file <address location="test" ConnectionName="test" /> I want to replace the above lines by <address location="test123" /> I am usind SED and not able to remove the new line characters between the two lines. Can anyone please help... (4 Replies)
Discussion started by: ramk
4 Replies

7. Shell Programming and Scripting

non-greedy sed

I found a sed example that works for me, however, now i would like to understand what it is doing: $ echo "<b>foo </b >bar" | sed 's/<*>//g' foo bar the part that i don't understand is the * my guess is that ^> means the first instance of > stop matching but why * and how is it able to... (2 Replies)
Discussion started by: adambot
2 Replies

8. Shell Programming and Scripting

sed pattern matching

Unfortunately this chap has been banned for some reason and I was looking forward to the resolution of his question: - https://www.unix.com/shell-programming-scripting/123118-append-position-28-33-a.html He was asking if you can use sed to match a pattern you want to replace within a... (6 Replies)
Discussion started by: steadyonabix
6 Replies

9. Shell Programming and Scripting

sed - matching pattern one but not pattern two

All, I have the following file: -------------------------------------- # # /etc/pam.d/common-password - password-related modules common to all services # # This file is included from other service-specific PAM config files, # and should contain a list of modules that define the services... (2 Replies)
Discussion started by: RobertBerrie
2 Replies

10. Shell Programming and Scripting

Pattern matching sed

MSG="THERE WERE XX RECORDS IN ERROR TABLE,AAAA, WHEN LOADING THE BBBB TABLE WITH EXTRACT FROM CCCC INTO TABLES FOR , DATABASE DDDD." echo "$MSG" > /tmp/mplanmsg.$$.out I wan to replace XX with the content in $recordXX cat /tmp/mplanmsg.$$.out|sed 's/XX/\$recordXX/g'| sed... (3 Replies)
Discussion started by: leemjesse
3 Replies
Login or Register to Ask a Question