Skip to main content
3 of 5
added 39 characters in body

Sed command to replace file path in txt file

Good day all!

I am trying to change the path of directories in a text file using sed cmd.

The servers I have tested the commands are running Rocky Linux 8.5 (Green Obsidian).

Here's the sed command I am using:

sed -i 's|1344P1052_ssl02 /gs:/prod1|sd://prod1-backup|g' example.txt

sed -i 's|1344P1052_ssl09 /gs:/prod1|sd://prod1-backup|g' example.txt

There is no error in the output, but the directory path in the file remains the same.

It should change [example below]

Before:

1344P1052_ssl02 /gs:/prod1/some_notes_here
1344P1052_ssl09 /gs:/prod1/some_notes_here

sed -i 's|1344P1052_ssl02 /gs:/prod1|sd://prod1-backup|g' example.txt
sed -i 's|1344P1052_ssl09 /gs:/prod1|sd://prod1-backup|g' example.txt

After:

sd://prod1-backup/some_notes_here
sd://prod1-backup/some_notes_here

I think it does not work because of the space, semi-colons, etc but I googled in this group's answer and read that using |

should work for special chars, backslash and space. Now I am not too sure, so I am just doing it manually with vim line by line.

Appreciate any help really...