Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sed regular expression 1

Status
Not open for further replies.

ranadhir

Programmer
Nov 4, 2003
54
IN
We have a file with entries like
good/bad/ugly.
We are able to retrieve the last bit(ugly) with
sed -e 's/.*\///g' testfilelist.txt
But we wish to achieve the same by creating 'data regions'.
We are trying

sed -e 's/(.*/)([^/]\+)$/\2/g' testfilelist.txt
But we are getting
sed: -e expression #1, char 12: Unknown option to `s'.
How do we print out the first ,second and third entries in the line above by using this approach?
 
Hi

Oops, you have to escape the slashes ( / ) too. To have more clear expression, use other character as delimiter. I prefer comma ( , ) :
Code:
sed -e 's,\(.*/\)\([^/]\+\)$,\2,g' /input/file

Feherke.
 
tried this
sed -e 's/\(.*/)\([^/]\+)$\/\2/g' testfilelist.txt
But that does not improve matters.
Am using GNU sed version 4.0.7
Is this a proper attempt at partitioning into data regions?
 
awk -F/ '{print $2}' testfilelist.txt

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Solved!!
The final form looks like quite scary - but it works
sed -e 's/\(.*\/\)\([^\/]\+\)$/\2/g' testfilelist.txt
Awk is definitely much more simpler;but this was just an attempt to understand the data region concept ,to use in perl
 
to use in perl
In perl, why not simply use the split function ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
What i meant is the following construct in perl:
next unless($line=~m/([^\/]+)\/([^\/]+)\/([^\/]+)$/);
This too works fine - the only difference that i see is to not use the escape characters.
 
Hi

Feherke said:
Usually you have to escape the parethesis ( () ) in sed.
ranadhir said:
the only difference that i see is to not use the escape characters.
That is a difference between the regular expression syntax of the two programs.

You could try txt2regex, a tough regular expression editor which can display multiple syntaxes.

Some [tt]sed[/tt] versions, like GNU [tt]sed[/tt] has a -r switch to use the typical syntax :
man sed said:
-r, --regexp-extended
use extended regular expressions in the script.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top