Why does this SED example work?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Why does this SED example work?
# 1  
Old 07-05-2012
Why does this SED example work?

Code:
$ x="/home/guru/temp/f1.txt"
$ echo $x | sed 's^.*/^^'

This will give the absolute path f1.txt. I don't understand WHY it works. How is it determining the last "/" character exactly?
# 2  
Old 07-05-2012
After 231 posts, you must realise that there is some variation in unix/Linux/Shell.
Please post what Operating System and version your are running and what Shell you use.

I would advise all other posters to not respond until the O/P provides proper context.
# 3  
Old 07-05-2012
Post withdrawn...Smilie

Last edited by elixir_sinari; 07-05-2012 at 06:25 PM..
# 4  
Old 07-05-2012
BSD Sed, OS X 10.6.8 (Mac). Sorry.

Last edited by methyl; 07-05-2012 at 07:32 PM..
# 5  
Old 07-05-2012
This is going to need a sed expert. Anybody out there?

Personally I would use the unix basename command and be sure how it worked. Others may differ.
# 6  
Old 07-05-2012
It takes advantage of the greedy matching characteristic of sed, which means that it will try to find the longest match possible.. So in this case the longest match to .*/ is /home/guru/temp/, so it will replace that with nothing, since the replacement part contains nothing.

The ^ plays no role, here other than that it is the character that delimits the match part and the replacement part. Normally forward slashes (/) are used for this but it can be almost any character. So instead, one could have used sed 's|.*/||' or s'=.*==', for example.

--
If your scope is a single variable, than a more effective method is to use basename, as methyl suggests or to use variable expansion:
Code:
echo "${x##*/}"

This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 07-05-2012
Greedy matching, perfect. Is this characteristic of all Sed versions? How do you determine this regex behavior of a particular command, such as Sed or any other?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem getting sed to work with variables

Hello, I am processing text files looking for a string and replacing the first occurrence of the string with something else. For the text,id Name 1 methyl-(2-methylpropoxy)-oxoammonium 2 N-amino-N-(methylamino)-2-nitrosoethanamine 3 3-methoxy-3-methyloxazolidin-3-ium... (12 Replies)
Discussion started by: LMHmedchem
12 Replies

2. UNIX for Dummies Questions & Answers

sed command does not work as expected

Why when I use this command do I get "E123"? echo NCE123 | sed -n 's/\(.*\)\(\{1,\}\{1,5\}\)\(.*\)/\2/p' But when I used this command, I get NCE123? echo NCE123 | sed -n 's/\(.*\)\(\{3\}\{1,5\}\)\(.*\)/\2/p' I thought \{1,\} would mean any number of characters and \{1,5\ would mean 1-5... (1 Reply)
Discussion started by: newbie2010
1 Replies

3. Shell Programming and Scripting

sed to work on 2nd field only

I have a requirement to replace "\" with "/" in only the 2nd field of the input file which has 2 fields. The field delimiter is "|" Sample records from input file: 1\23|\tmp\user mn\wer|\home\temp Expected output: 1\23|/tmp/user mn\wer|/home/temp I used sed 's/\\/\//g' ... (2 Replies)
Discussion started by: krishmaths
2 Replies

4. UNIX for Dummies Questions & Answers

sed won't work

Hi All, can anybody tell me what's wrong with this code? # SEARCH replaced by REPLACE #!/bin/bash SEARCH="95$$ 0 t" REPLACE="95$$ 1 t" for I in `ls *000.inp | cut -c-12`; do echo $I sed 's/$SEARCH/$REPLACE/' ${I}-000.inp > ${I}-100.inp done It don't replace the string... (5 Replies)
Discussion started by: f_o_555
5 Replies

5. Shell Programming and Scripting

Alternate work out for sed command

Iam trying to insert a line after #Cluster in the property file shown below #Node=Nodehostname:NodeProfilename Node=testNode:test_profile #Cluster=Cluster_Name:nodeName@ClusterMem1,nodeName@ClusterMem2,.... #DC=DCname:DCnodegrp:DCtemp DC=test_DC:test_NG:test_template i was using sed command... (12 Replies)
Discussion started by: SSSB
12 Replies

6. Shell Programming and Scripting

Sed with sort doesnt work

Sed with sort doesnt work The below code doesnt work: sed -e '/^$/d' -e 's/,/|/g' | sort -t"|" -k1,1 -u file1 when i seperate them it work but i have to create intermediate file which i dont want to: sed -e '/^$/d' -e 's/,/|/g' file1 > file2 sort -t"|" -k1,1 -u file2 Help... (2 Replies)
Discussion started by: pinnacle
2 Replies

7. Shell Programming and Scripting

Use variable in sed don't work.

Hi all. I have a script as below: cutmth=`TZ=CST+2160 date +%b` export cutmth echo $cutmth >> date.log sed -n "/$cutmth/$p" alert_sbdev1.log > alert_summ.log My purpose is to run through the alert_sbdev1.log and find the 1st occurence of 'Jan' and send everything after that line to... (4 Replies)
Discussion started by: ahSher
4 Replies

8. Shell Programming and Scripting

Getting sed to work on part of a line

I been trying to get this right. I have trying to get rid of spaces in between the character < and the character >. Everytime I try, sed gets too greedy and do the whole line. Ex. < T AG 1> Hello, how are you doing? <Tag 2> I am doing fine. I want this: <TAG1> Hello, how are you... (6 Replies)
Discussion started by: quixoticking11
6 Replies

9. Shell Programming and Scripting

sed script. How does it work?

I'm using this command sed -e "s/'/'/g" -e 's/&quot;/"/g' -e 's/&amp;/\&/g' myfile.txt My question is does this command reads file 3 times applying different replacement each time or it reads it only once and do 3 replacements at the same time? My concern is, since I have big files (1 MB or more) that... (2 Replies)
Discussion started by: billy5
2 Replies

10. Shell Programming and Scripting

sed doesn't work

Hello I' m confused a bit. I want to replace string "&amp" with "&" using this command. sed 's/&amp/&/g' and it doesn't work. Nothing happens. On the other side this works: sed 's/&amp/@/g' or sed 's/&amp/^/g' !!! Can somebody help please? Thanks (3 Replies)
Discussion started by: billy5
3 Replies
Login or Register to Ask a Question