Reputation: 11
I’m having some issues trying to edit a file with SED using a variable that has forward slashes in it.
The file I’m trying to edit “checksum” has the following contents…
758f9775093bf885dbc963cba1aec732,/bin/bash
1b45027142521f2a1c3d95891770eb47,/etc/grub.conf
2a34dc288f7994cc7b1b338605ef8775,/etc/hosts
03f670845fe199955f9a0babecbda9a0,/etc/networks
I want to search for the string “/bin/bash” using a variable (file_path=“/bin/bash”) and when I find that string, remove everything up to the “,” and replace with the work “VALID”.
Here’s my sed syntax and attempts below, however it fails when I try to use the variable. Any suggestions?
Success without variable
# sed '\/bin\/bash/ s/^.*,/VALID,/g' checksum
VALID,/bin/bash
1b45027142521f2a1c3d95891770eb47,/etc/grub.conf
2a34dc288f7994cc7b1b338605ef8775,/etc/hosts
03f670845fe199955f9a0babecbda9a0,/etc/networks
Failure with variable
# file_path="/bin/bash"
# echo $file_path
/bin/bash
# sed '/$file_path/ s/^.*,/VALID,/g' checksum
758f9775093bf885dbc963cba1aec732,/bin/bash
1b45027142521f2a1c3d95891770eb47,/etc/grub.conf
2a34dc288f7994cc7b1b338605ef8775,/etc/hosts
03f670845fe199955f9a0babecbda9a0,/etc/networks
I’ve tried wrapping the variable in quotes, {} and still had the same failures:
# sed '/"$file_path"/ s/^.*,/VALID,/g' checksum
758f9775093bf885dbc963cba1aec732,/bin/bash
1b45027142521f2a1c3d95891770eb47,/etc/grub.conf
2a34dc288f7994cc7b1b338605ef8775,/etc/hosts
03f670845fe199955f9a0babecbda9a0,/etc/networks
# sed '/{$file_path}/ s/^.*,/VALID,/g' checksum
758f9775093bf885dbc963cba1aec732,/bin/bash
1b45027142521f2a1c3d95891770eb47,/etc/grub.conf
2a34dc288f7994cc7b1b338605ef8775,/etc/hosts
03f670845fe199955f9a0babecbda9a0,/etc/networks
I’ve also used “|” as the escape character.
# sed '|{$file_path}| s|^.*,|VALID,|g' checksum
sed: -e expression #1, char 1: unknown command: `|'
Any suggestions on how to accomplish this?
# sed --version
GNU sed version 4.2.1
Upvotes: 0
Views: 129
Reputation: 204164
Just use awk for a simple life:
awk -v fp="$file_path" 'BEGIN{FS=OFS=","} $2==fp{$1="Valid"} 1' file
That will work no matter what the contents of file_path
, including /
or an RE metacharacters or shell globbing characters and it won't produce a false match if it's contents partially match any part of line in the file.
Upvotes: 1
Reputation: 89584
To complete @ooga's good answer. It can be interesting to keep single quotes to avoid ambiguities that may occur with special characters that bash can interpret. To do that, you can simply close the sed block with a single quote to reopen it later:
sed '\~'$file_path'~s~^[^,]*~VALID~' checksum
Upvotes: 1
Reputation: 15501
The single quotes inhibit variable substitution. Try this:
sed "\:$file_path: s/^.*,/VALID,/" checksum
Note that I've changed the pattern delimiter to a colon so that the slashes in the variable's value won't end the pattern.
Upvotes: 1