Remove absolute path from file with bash
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have file "list.txt" containing absolute paths to other files
/home/lin/bash/aaa
/home/lin/bash/song.mp3
/home/lin/bash/doc.html
/home/lin/bash/directory
I want to assign path to variable
path="/home/lin/bash/song.mp3"
and then remove whole line with that path. I've tried
sed -i '$path' list.txt
and many other command with grep, echo but nothing works.
bash
add a comment |Â
up vote
0
down vote
favorite
I have file "list.txt" containing absolute paths to other files
/home/lin/bash/aaa
/home/lin/bash/song.mp3
/home/lin/bash/doc.html
/home/lin/bash/directory
I want to assign path to variable
path="/home/lin/bash/song.mp3"
and then remove whole line with that path. I've tried
sed -i '$path' list.txt
and many other command with grep, echo but nothing works.
bash
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have file "list.txt" containing absolute paths to other files
/home/lin/bash/aaa
/home/lin/bash/song.mp3
/home/lin/bash/doc.html
/home/lin/bash/directory
I want to assign path to variable
path="/home/lin/bash/song.mp3"
and then remove whole line with that path. I've tried
sed -i '$path' list.txt
and many other command with grep, echo but nothing works.
bash
I have file "list.txt" containing absolute paths to other files
/home/lin/bash/aaa
/home/lin/bash/song.mp3
/home/lin/bash/doc.html
/home/lin/bash/directory
I want to assign path to variable
path="/home/lin/bash/song.mp3"
and then remove whole line with that path. I've tried
sed -i '$path' list.txt
and many other command with grep, echo but nothing works.
bash
asked Jan 22 at 15:04
Mike Naplet
62
62
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
0
down vote
accepted
There are two problems with your solution. First since you're using single quotes for '$path'
this expression is treated literally, and not like the variable $path
. In order to solve this use double quotes "$path"
. But then you will face the second problem: you want to use slash /
as a special sed
-symbol and at the same time this symbol is present in paths, which will confuse sed
. Therefore you have to use some other symbol instead of slash, for example, use comma
$ sed -i "s,$path,," list.txt
$ cat list.txt
/home/lin/bash/aaa
/home/lin/bash/doc.html
/home/lin/bash/directory
There is another beautiful approach, suggested by Tim Kennedy (see explanation of how it works in the discussion below), which does not leave the blank line
$ sed -i ",$path,d" list.txt
$ cat list.txt
/home/lin/bash/aaa
/home/lin/bash/doc.html
/home/lin/bash/directory
1
sed -i "#$tmppath#d" list.txt
will do the same without leaving behind that empty line.
â Tim Kennedy
Jan 22 at 15:49
@Tim, beautiful solution, but I do not understand how it works)) p.s. if you do not mind, I will cite you in my answer.
â John Smith
Jan 22 at 16:08
2
Your solution,"s,$tmppath,,"
is a sed substitution command. Using a pattern like/pattern/d
tells sed to delete the line the pattern is in. Except, in this case, as you rightly pointed out, the delimiter needs to change because the pattern is full of/
characters. I should have used commas in my example to make it more similar to yours, so it would be:",$tmppath,d"
. In a nut shell, the delimiters identify the pattern to find, and thed
is the delete command. And the leading backslash tells sed to ignore the,
as a comma and use it as a delimiter.
â Tim Kennedy
Jan 22 at 21:34
Thank you all for answers. Solution suggested by Tim Kennedy looks the easiest for me.
â Mike Naplet
Jan 23 at 17:36
@MikeNaplet good reason to upvote his comments)
â John Smith
Jan 23 at 20:22
add a comment |Â
up vote
2
down vote
Try grep
approach once again - it'll work:
grep -xv "$path" list.txt > tmp_$$ && mv tmp_$$ list.txt
The final list.txt
contents:
/home/lin/bash/aaa
/home/lin/bash/doc.html
/home/lin/bash/directory
add a comment |Â
up vote
0
down vote
You could do this with GNU sed
and double qoutes to expand the variable.
sed -i.bak ":^$path$:d" list.txt
- Here we used
-i
to have in-place replace/deletion of matched patters as in$path
, the.bak
is taking a backup of originallist.txt
file with tolist.txt.bak
- We used different separator to prevent failing
sed
with slashes/
in$path
will contain. - We used start of line
^
and end of line$
anchors to match the path in whole line not partially if matched. We escaped opening first delimiter as it's mandatory when delimiter is other than slash
/
. inman sed
documented.cregexpc
Match lines matching the regular expression regexp.
The c may be any character.actually this is telling
sed
that next character is our delimiter.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
There are two problems with your solution. First since you're using single quotes for '$path'
this expression is treated literally, and not like the variable $path
. In order to solve this use double quotes "$path"
. But then you will face the second problem: you want to use slash /
as a special sed
-symbol and at the same time this symbol is present in paths, which will confuse sed
. Therefore you have to use some other symbol instead of slash, for example, use comma
$ sed -i "s,$path,," list.txt
$ cat list.txt
/home/lin/bash/aaa
/home/lin/bash/doc.html
/home/lin/bash/directory
There is another beautiful approach, suggested by Tim Kennedy (see explanation of how it works in the discussion below), which does not leave the blank line
$ sed -i ",$path,d" list.txt
$ cat list.txt
/home/lin/bash/aaa
/home/lin/bash/doc.html
/home/lin/bash/directory
1
sed -i "#$tmppath#d" list.txt
will do the same without leaving behind that empty line.
â Tim Kennedy
Jan 22 at 15:49
@Tim, beautiful solution, but I do not understand how it works)) p.s. if you do not mind, I will cite you in my answer.
â John Smith
Jan 22 at 16:08
2
Your solution,"s,$tmppath,,"
is a sed substitution command. Using a pattern like/pattern/d
tells sed to delete the line the pattern is in. Except, in this case, as you rightly pointed out, the delimiter needs to change because the pattern is full of/
characters. I should have used commas in my example to make it more similar to yours, so it would be:",$tmppath,d"
. In a nut shell, the delimiters identify the pattern to find, and thed
is the delete command. And the leading backslash tells sed to ignore the,
as a comma and use it as a delimiter.
â Tim Kennedy
Jan 22 at 21:34
Thank you all for answers. Solution suggested by Tim Kennedy looks the easiest for me.
â Mike Naplet
Jan 23 at 17:36
@MikeNaplet good reason to upvote his comments)
â John Smith
Jan 23 at 20:22
add a comment |Â
up vote
0
down vote
accepted
There are two problems with your solution. First since you're using single quotes for '$path'
this expression is treated literally, and not like the variable $path
. In order to solve this use double quotes "$path"
. But then you will face the second problem: you want to use slash /
as a special sed
-symbol and at the same time this symbol is present in paths, which will confuse sed
. Therefore you have to use some other symbol instead of slash, for example, use comma
$ sed -i "s,$path,," list.txt
$ cat list.txt
/home/lin/bash/aaa
/home/lin/bash/doc.html
/home/lin/bash/directory
There is another beautiful approach, suggested by Tim Kennedy (see explanation of how it works in the discussion below), which does not leave the blank line
$ sed -i ",$path,d" list.txt
$ cat list.txt
/home/lin/bash/aaa
/home/lin/bash/doc.html
/home/lin/bash/directory
1
sed -i "#$tmppath#d" list.txt
will do the same without leaving behind that empty line.
â Tim Kennedy
Jan 22 at 15:49
@Tim, beautiful solution, but I do not understand how it works)) p.s. if you do not mind, I will cite you in my answer.
â John Smith
Jan 22 at 16:08
2
Your solution,"s,$tmppath,,"
is a sed substitution command. Using a pattern like/pattern/d
tells sed to delete the line the pattern is in. Except, in this case, as you rightly pointed out, the delimiter needs to change because the pattern is full of/
characters. I should have used commas in my example to make it more similar to yours, so it would be:",$tmppath,d"
. In a nut shell, the delimiters identify the pattern to find, and thed
is the delete command. And the leading backslash tells sed to ignore the,
as a comma and use it as a delimiter.
â Tim Kennedy
Jan 22 at 21:34
Thank you all for answers. Solution suggested by Tim Kennedy looks the easiest for me.
â Mike Naplet
Jan 23 at 17:36
@MikeNaplet good reason to upvote his comments)
â John Smith
Jan 23 at 20:22
add a comment |Â
up vote
0
down vote
accepted
up vote
0
down vote
accepted
There are two problems with your solution. First since you're using single quotes for '$path'
this expression is treated literally, and not like the variable $path
. In order to solve this use double quotes "$path"
. But then you will face the second problem: you want to use slash /
as a special sed
-symbol and at the same time this symbol is present in paths, which will confuse sed
. Therefore you have to use some other symbol instead of slash, for example, use comma
$ sed -i "s,$path,," list.txt
$ cat list.txt
/home/lin/bash/aaa
/home/lin/bash/doc.html
/home/lin/bash/directory
There is another beautiful approach, suggested by Tim Kennedy (see explanation of how it works in the discussion below), which does not leave the blank line
$ sed -i ",$path,d" list.txt
$ cat list.txt
/home/lin/bash/aaa
/home/lin/bash/doc.html
/home/lin/bash/directory
There are two problems with your solution. First since you're using single quotes for '$path'
this expression is treated literally, and not like the variable $path
. In order to solve this use double quotes "$path"
. But then you will face the second problem: you want to use slash /
as a special sed
-symbol and at the same time this symbol is present in paths, which will confuse sed
. Therefore you have to use some other symbol instead of slash, for example, use comma
$ sed -i "s,$path,," list.txt
$ cat list.txt
/home/lin/bash/aaa
/home/lin/bash/doc.html
/home/lin/bash/directory
There is another beautiful approach, suggested by Tim Kennedy (see explanation of how it works in the discussion below), which does not leave the blank line
$ sed -i ",$path,d" list.txt
$ cat list.txt
/home/lin/bash/aaa
/home/lin/bash/doc.html
/home/lin/bash/directory
edited Jan 23 at 7:44
answered Jan 22 at 15:23
John Smith
95857
95857
1
sed -i "#$tmppath#d" list.txt
will do the same without leaving behind that empty line.
â Tim Kennedy
Jan 22 at 15:49
@Tim, beautiful solution, but I do not understand how it works)) p.s. if you do not mind, I will cite you in my answer.
â John Smith
Jan 22 at 16:08
2
Your solution,"s,$tmppath,,"
is a sed substitution command. Using a pattern like/pattern/d
tells sed to delete the line the pattern is in. Except, in this case, as you rightly pointed out, the delimiter needs to change because the pattern is full of/
characters. I should have used commas in my example to make it more similar to yours, so it would be:",$tmppath,d"
. In a nut shell, the delimiters identify the pattern to find, and thed
is the delete command. And the leading backslash tells sed to ignore the,
as a comma and use it as a delimiter.
â Tim Kennedy
Jan 22 at 21:34
Thank you all for answers. Solution suggested by Tim Kennedy looks the easiest for me.
â Mike Naplet
Jan 23 at 17:36
@MikeNaplet good reason to upvote his comments)
â John Smith
Jan 23 at 20:22
add a comment |Â
1
sed -i "#$tmppath#d" list.txt
will do the same without leaving behind that empty line.
â Tim Kennedy
Jan 22 at 15:49
@Tim, beautiful solution, but I do not understand how it works)) p.s. if you do not mind, I will cite you in my answer.
â John Smith
Jan 22 at 16:08
2
Your solution,"s,$tmppath,,"
is a sed substitution command. Using a pattern like/pattern/d
tells sed to delete the line the pattern is in. Except, in this case, as you rightly pointed out, the delimiter needs to change because the pattern is full of/
characters. I should have used commas in my example to make it more similar to yours, so it would be:",$tmppath,d"
. In a nut shell, the delimiters identify the pattern to find, and thed
is the delete command. And the leading backslash tells sed to ignore the,
as a comma and use it as a delimiter.
â Tim Kennedy
Jan 22 at 21:34
Thank you all for answers. Solution suggested by Tim Kennedy looks the easiest for me.
â Mike Naplet
Jan 23 at 17:36
@MikeNaplet good reason to upvote his comments)
â John Smith
Jan 23 at 20:22
1
1
sed -i "#$tmppath#d" list.txt
will do the same without leaving behind that empty line.â Tim Kennedy
Jan 22 at 15:49
sed -i "#$tmppath#d" list.txt
will do the same without leaving behind that empty line.â Tim Kennedy
Jan 22 at 15:49
@Tim, beautiful solution, but I do not understand how it works)) p.s. if you do not mind, I will cite you in my answer.
â John Smith
Jan 22 at 16:08
@Tim, beautiful solution, but I do not understand how it works)) p.s. if you do not mind, I will cite you in my answer.
â John Smith
Jan 22 at 16:08
2
2
Your solution,
"s,$tmppath,,"
is a sed substitution command. Using a pattern like /pattern/d
tells sed to delete the line the pattern is in. Except, in this case, as you rightly pointed out, the delimiter needs to change because the pattern is full of /
characters. I should have used commas in my example to make it more similar to yours, so it would be: ",$tmppath,d"
. In a nut shell, the delimiters identify the pattern to find, and the d
is the delete command. And the leading backslash tells sed to ignore the ,
as a comma and use it as a delimiter.â Tim Kennedy
Jan 22 at 21:34
Your solution,
"s,$tmppath,,"
is a sed substitution command. Using a pattern like /pattern/d
tells sed to delete the line the pattern is in. Except, in this case, as you rightly pointed out, the delimiter needs to change because the pattern is full of /
characters. I should have used commas in my example to make it more similar to yours, so it would be: ",$tmppath,d"
. In a nut shell, the delimiters identify the pattern to find, and the d
is the delete command. And the leading backslash tells sed to ignore the ,
as a comma and use it as a delimiter.â Tim Kennedy
Jan 22 at 21:34
Thank you all for answers. Solution suggested by Tim Kennedy looks the easiest for me.
â Mike Naplet
Jan 23 at 17:36
Thank you all for answers. Solution suggested by Tim Kennedy looks the easiest for me.
â Mike Naplet
Jan 23 at 17:36
@MikeNaplet good reason to upvote his comments)
â John Smith
Jan 23 at 20:22
@MikeNaplet good reason to upvote his comments)
â John Smith
Jan 23 at 20:22
add a comment |Â
up vote
2
down vote
Try grep
approach once again - it'll work:
grep -xv "$path" list.txt > tmp_$$ && mv tmp_$$ list.txt
The final list.txt
contents:
/home/lin/bash/aaa
/home/lin/bash/doc.html
/home/lin/bash/directory
add a comment |Â
up vote
2
down vote
Try grep
approach once again - it'll work:
grep -xv "$path" list.txt > tmp_$$ && mv tmp_$$ list.txt
The final list.txt
contents:
/home/lin/bash/aaa
/home/lin/bash/doc.html
/home/lin/bash/directory
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Try grep
approach once again - it'll work:
grep -xv "$path" list.txt > tmp_$$ && mv tmp_$$ list.txt
The final list.txt
contents:
/home/lin/bash/aaa
/home/lin/bash/doc.html
/home/lin/bash/directory
Try grep
approach once again - it'll work:
grep -xv "$path" list.txt > tmp_$$ && mv tmp_$$ list.txt
The final list.txt
contents:
/home/lin/bash/aaa
/home/lin/bash/doc.html
/home/lin/bash/directory
answered Jan 22 at 15:13
RomanPerekhrest
22.4k12144
22.4k12144
add a comment |Â
add a comment |Â
up vote
0
down vote
You could do this with GNU sed
and double qoutes to expand the variable.
sed -i.bak ":^$path$:d" list.txt
- Here we used
-i
to have in-place replace/deletion of matched patters as in$path
, the.bak
is taking a backup of originallist.txt
file with tolist.txt.bak
- We used different separator to prevent failing
sed
with slashes/
in$path
will contain. - We used start of line
^
and end of line$
anchors to match the path in whole line not partially if matched. We escaped opening first delimiter as it's mandatory when delimiter is other than slash
/
. inman sed
documented.cregexpc
Match lines matching the regular expression regexp.
The c may be any character.actually this is telling
sed
that next character is our delimiter.
add a comment |Â
up vote
0
down vote
You could do this with GNU sed
and double qoutes to expand the variable.
sed -i.bak ":^$path$:d" list.txt
- Here we used
-i
to have in-place replace/deletion of matched patters as in$path
, the.bak
is taking a backup of originallist.txt
file with tolist.txt.bak
- We used different separator to prevent failing
sed
with slashes/
in$path
will contain. - We used start of line
^
and end of line$
anchors to match the path in whole line not partially if matched. We escaped opening first delimiter as it's mandatory when delimiter is other than slash
/
. inman sed
documented.cregexpc
Match lines matching the regular expression regexp.
The c may be any character.actually this is telling
sed
that next character is our delimiter.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You could do this with GNU sed
and double qoutes to expand the variable.
sed -i.bak ":^$path$:d" list.txt
- Here we used
-i
to have in-place replace/deletion of matched patters as in$path
, the.bak
is taking a backup of originallist.txt
file with tolist.txt.bak
- We used different separator to prevent failing
sed
with slashes/
in$path
will contain. - We used start of line
^
and end of line$
anchors to match the path in whole line not partially if matched. We escaped opening first delimiter as it's mandatory when delimiter is other than slash
/
. inman sed
documented.cregexpc
Match lines matching the regular expression regexp.
The c may be any character.actually this is telling
sed
that next character is our delimiter.
You could do this with GNU sed
and double qoutes to expand the variable.
sed -i.bak ":^$path$:d" list.txt
- Here we used
-i
to have in-place replace/deletion of matched patters as in$path
, the.bak
is taking a backup of originallist.txt
file with tolist.txt.bak
- We used different separator to prevent failing
sed
with slashes/
in$path
will contain. - We used start of line
^
and end of line$
anchors to match the path in whole line not partially if matched. We escaped opening first delimiter as it's mandatory when delimiter is other than slash
/
. inman sed
documented.cregexpc
Match lines matching the regular expression regexp.
The c may be any character.actually this is telling
sed
that next character is our delimiter.
edited Jan 22 at 16:21
answered Jan 22 at 15:16
ñÃÂsýù÷
15.2k92462
15.2k92462
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f418882%2fremove-absolute-path-from-file-with-bash%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password