Wildcards in SED


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Wildcards in SED
# 1  
Old 09-27-2006
Wildcards in SED

Hi Folks

Quick one I can't seem to figure out sed wildcards..

I need to replace a string such as "From here.....to here".

I would think the command would look like:
sed 's/From here*to here/new text/g' or
sed 's/From here\*to here/new text/g'

But it's not working for me.

Thanks in advance Smilie Smilie
# 2  
Old 09-27-2006
Code:
echo 'aksjfhd from here fooo asdlfkjaslkfjd to here aslfjdlaksjdf' | sed 's/from here.*to here/OVER THERE/'

# 3  
Old 09-27-2006
* in regular expression will match any number (or none) of the single character that immediately precedes it

Code:
sed 's/From here*to here/new text/g'

This regular expression From here*to here will match
From herto here
From hereto here
From hereeto here
...

\ Usually, turn off the special meaning of the following character

Code:
sed 's/From here\*to here/new text/g'

This regular expression From here\*to here will match
From here*to here

try this

Code:
sed 's/From here.*to here/new text/g'

# 4  
Old 09-27-2006
You might want to use the '-r' option to tell sed to use extended regular expressions. Basic sed regexes are quite limited.

Second, regular expressions work differently in sed than they do in a shell. * doesn't mean anything by itself, it's a modifier for something else. First you tell it what expression you want to match, then optionally, how many of them you want to match. An expression can be a single letter, a set of letters, or something in brackets.
  • A by itself just matches the letter A, like you'd expect.
  • [ABC] by itself just matches the letter A, B, or C.
  • A* tells it to match 0 or more A characters.
  • [ABC]* tells it to match 0 or more characters among A, B, C.
  • [A-Z]* tells it to match 0 or more characters among A, B, C, ..., Z.
  • [^A]* tells it to match 0 or more characters that aren't A.
  • (ABC)* tells it to match 0 or more repititions of "ABC".

* is not the only modifier:
  • A+ tells it to match 1 or more A characters.
  • A? tells it to match 0 or 1 A characters.
  • A{4} tells it to match precisely 4 A characters.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 09-27-2006
The -r option is not available on all Unix flavour.
AIX sed doesn't support this option.

Jean-Pierre.
# 6  
Old 09-27-2006
Alternative , without regular expression

Code:
#!/usr/bin/python
string = "some text in front From here in the middle to here at the end"
fromindex = string.index("From here")
toindex = string.index("to here")
tobeReplace = string[ fromindex : toindex + len("to here") ]
string.replace( tobeReplace , "new text")

Output:
Code:
'some text in front new test at the end'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using Wildcards in scp

to scp using windcards you use the following : scp 'hostname:/home/username/diff_201110*' . Enjoy ! (0 Replies)
Discussion started by: phpsnook
0 Replies

2. UNIX for Advanced & Expert Users

Wildcards

These 2 websites do a GREAT job of explaining different types of wildcards. I learned about the categories of characters which I never knew about at all. GNU/Linux Command-Line Tools Guide - Wildcards GREP (1 Reply)
Discussion started by: cokedude
1 Replies

3. UNIX for Dummies Questions & Answers

SED and wildcards

I am using this code to locate and modify one particular ID in a file containing thousands of entries sed 's/^>OldID/>NewID/g' Infile > Outfile How can I modify the code so I can rename all old IDs to a new unique ID? I tried this sed 's/^>*/>NewID/g' Infile > Outfile but it did not... (10 Replies)
Discussion started by: Xterra
10 Replies

4. UNIX for Dummies Questions & Answers

wildcards in path

Is there some rule about using wildcards in path? Say I want to create a file, but one of the directories in the path is called 1433d.default and on different machines it will be called <some other string>.default touch ~/Library/Application/*.default/myfile In theory I thought that... (5 Replies)
Discussion started by: glev2005
5 Replies

5. Shell Programming and Scripting

SED with Wildcards and Special Characters

Hi All, Need you guys' help to achieve the following: I have some strings and i wish to threw off the end part that's in the file path. From: /directoryname1/subdirectoryname1/abc.txt /directoryname2/subdirectoryname2/defggf.txt To: /directoryname1/subdirectoryname1/... (7 Replies)
Discussion started by: Radeon
7 Replies

6. Shell Programming and Scripting

Use wildcards in a script

Hello I have this script: #!/bin/ksh INPUTFILE=$1 TEMPFILE=$INPUTFILE.$$ OUTPUTFILE=$INPUTFILE.new # nr of arguments has to be 1 if then echo "\nUsage: $0 inputfile\n" return 1 fi # inputfile must exist and be readable if then (13 Replies)
Discussion started by: emferrari
13 Replies

7. UNIX for Dummies Questions & Answers

wildcards NOT

Hi All Please excuse another straightforward question. When creating a tar archive from a directory I am attempting to use wildcards to eliminate certain filetypes (otherwise the archive gets too large). So I am looking for something along these lines. tar -cf archive.tar * <minus all *.rst... (5 Replies)
Discussion started by: C3000
5 Replies

8. UNIX for Dummies Questions & Answers

ls with wildcards

ok, I'm trying to write a script file that lists files with specific elements in the name into a txt file, it looks like this ls s*.dat > file_names.txt can't figure out whats wrong with that line, any ideas? thanks in advance (10 Replies)
Discussion started by: benu302000
10 Replies

9. UNIX for Dummies Questions & Answers

wildcards

when writing a shell script (bourne) and using a unix command like 'ls' is there anything special you need to do to use a wildcard (like *)? (3 Replies)
Discussion started by: benu302000
3 Replies

10. UNIX for Dummies Questions & Answers

Wildcards in VI

I'm trying to delete lines from a large text file using VI. Every line that I am wanting to delete start with 'S' - all others do not. (A list of users) I've tried using * but doesn't seem to like it...any ideas... Doesn't have to be VI - but I'm better with VI than sed/awk. (8 Replies)
Discussion started by: peter.herlihy
8 Replies
Login or Register to Ask a Question