Reputation: 1719
This is a question about regular expression.
I have the following code:
#!/bin/sh
temp="/home/user/game/log.txt"
echo $temp | sed -e "s#\(.*\)/.*#\1#"
/home/user/game
What is the meaning of this guy --- s#\(.*\)/.*#\1#
?
I search from the web and I know some meaning, for example:
s
- substitute
.
- any word
*
- the word in front of this can repeat 0 to infinite times
But I still don't understand the meaning of this code,although I know the function
Upvotes: 0
Views: 2711
Reputation: 98088
"s#\(.*\)/.*#\1#"
s
is the substitute command, its format is like s[DELIM]regex[DELIM]replace[DELIM]
. and DELIM can be any character delimiting the arguments. In your case, the delimiter is #
. In the regex part you have:
\(.*\)/.*
Here the match between \(
and )
is captured, meaning that you can refer to it in the replace part of the command. Inside the capture you are [greedy] matching anything. But this match has to stop somewhere because after the capture there is a slash /
which should be matched. Since the captured .*
is greedy, sed will match and capture until the last slash. Then, it will match .*
without capturing. This part won't contain any slashes (due to previous greedy match). Thus, the regex will match all input if it contains a slash, but it will also remember the part until the last slash.
the replace part:
\1
replaces the matched pattern with the captured part. All in all, this command matches files within some directory and will remove filename, leaving only the directory name.
Upvotes: 1
Reputation:
s#\(.*\)/.*#\1
s
: substitue command#
: delimeter of the three parts the s
command has\(\)
: a group that can later be referenced.
: any character/
: a literal slash.*
: any number of characters\1
: referecen to the first groupThe s
command has three parts:
s
itself\(.*\)/.*
\1
So this takes everything up the last /
as the first group and prints it by referencing it with \1
.
Upvotes: 1