Why does this SED example work?


Trivia Category: Science: Gadgets
Difficulty: Medium
The Western Electric Model 500 telephone uses tone dialing to dial phone numbers.
True or False?

 
Thread Tools Search this Thread
# 1  
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  
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  
Post withdrawn...Smilie

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

Last edited by methyl; 07-05-2012 at 07:32 PM..
# 5  
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  
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  
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?
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

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

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

8. UNIX for Dummies Questions & Answers

sed command not work with variables?

I am trying to write a simple script which will take a variable with sed to take a line out of a text and display it #!/bin/sh exec 3<list while read list<&3 do echo $list sed -n '$list p'<list2 done this does not work, yet when I replace the $list variable from the sed command and... (1 Reply)
Discussion started by: MaestroRage
1 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

Featured Tech Videos