How to use sed to change references to directories? [duplicate] - bash

This question already has answers here:
sed search and replace strings containing / [duplicate]
(2 answers)
Closed 8 years ago.
So I know how to use sed -i 's/string/new-string/g' *.* but I'm trying to change all references to a directory structure and I don't know the proper format for the command.
Example: I want to change 'myfolder/mysubfolder' to 'myfolder'.
So if there's a string in a file that says 'myfolder/mysubfolder/file.txt' I want it to say 'myfolder/file.txt'.

You are on right track, just need to escape the /
sed -i 's/myfolder\/mysubfolder/myfolder/g' *.*

sed -i 's#myfolder/mysubfolder#myfolder#g' *.*
I used a delimiter other than / to avoid having to escape the / in the string.

Related

Replace a string with special characters using sed [duplicate]

This question already has answers here:
Is it possible to escape regex metacharacters reliably with sed
(4 answers)
Closed 7 months ago.
I have the below string
sec.val.hos.patn=.*app\.com$|localhost$|127\.0\.0\.1$
I want replace .*app\.com$|localhost$|127\.0\.0\.1$ with * so that final string looks like below
sec.val.hos.patn=*
I am trying to solve this problem using below sed command on Mac OS
sed -i ' ' 's~\.\*app\\\.com\$\|localhost\$\|127\\\.0\\\.0\\\.1\$~\*~g' file.txt
but unable to get the desired replacement. Can someone please help me to get this working.
I don't know if the pattern is actually more complex than you sample, but seems this should do the trick:
sed 's/\(^.*=\).*$/\1*/' <<< "sec.val.hos.patn=.*app\.com$|localhost$|127\.0\.0\.1$"
Here we capture everything from the start to =, then we replace the whole thing with capture group 1 plus *.

How to use sed to replace string that contains slash? [duplicate]

This question already has answers here:
How to insert strings containing slashes with sed? [duplicate]
(11 answers)
Closed 4 years ago.
I wanto replace # SigningTable refile:/etc/opendkim/SigningTable to SigningTable refile:/etc/opendkim/SigningTable.
Which means just remove #.
I use sed -i 's/# SigningTable refile:/etc/opendkim/SigningTable/ SigningTable refile:/etc/opendkim/SigningTable/g' /etc/opendkim.conf,but doesn't work.
I think it's because /,how to use sed to replace string with /?
You can change the delimiter:
sed -i 's!TEXT!REPLACE!' file
You can use this, if SigningTable appears once in the file:
sed '/SigningTable/{s/^# *//}' in
or be more specific:
sed '\#refile:/etc/opendkim/SigningTable#{s/^#//}' in

Find variable word and replace it in file using Bash [duplicate]

This question already has answers here:
Replace a string in shell script using a variable
(12 answers)
Closed 5 years ago.
I have a file and from this file I am trying to find a word and replace it with another word using Bash. I am using sed to do this and please note that the word that I am looking for is an output from a command. So I am trying to find a word, which is the output of a command, and replace it with another word and override the previous word.
This is my code:
File=file.txt
File2=file2.txt
min=$(cat $File2 | grep word);
sed -i 's/$min/max/g' $File
It's not producing any error, but I am unable to find the word in order to replace it. When I manually type the word rather than using the variable "$min" it works just fine. So when I do this, it works:
sed -i 's/min/max/g' $File
but when I do this, it doesn't:
sed -i 's/$min/max/g' $File
I am thinking maybe sed doesn't accept variables as a search string. Any idea how I can achieve this?
thank you.
Use double quotes for the sed expression, this should work:
sed -i "s/$min/max/g" $File

How to delete first column from the file in Unix [duplicate]

This question already has answers here:
Delete a column from a delimited file in linux
(7 answers)
Closed 8 years ago.
I have a file like the one below -
|A|B|C|D
|1|2|3|4
I want the result -
A|B|C|D
1|2|3|4
I have tried using cut but I'm not getting the desired output. Please suggest how the first column can be removed?
Using sed, delete the leading pipe symbol:
sed 's/^|//' file
There's an outside chance that on some versions of sed you'd need to escape the pipe. You might be able to use the over-write mode too (though not all versions of sed support that):
sed -i .bak 's/^\|//' file

Can I search/replace in multiple .txt files quickly from Terminal? [duplicate]

This question already has answers here:
Recursive search and replace in text files on Mac and Linux
(14 answers)
Closed 7 years ago.
I have about 100 .txt files that contain plain text. Somehow, some of the data has been corrupted and needs to be found/replaced.
I need to search for the characters'--' and replace it with a long dash: '—'.
Is there a way to do this quickly with a command in terminal?
The names of the .txt files in my directory are numbered sequentially: 1.txt, 2.txt, etc.
Thanks!
GNU sed:
sed -i 's/--/—/g' *.txt
OSX BSD sed:
You need to specify a backup file extension. To create a backup file with the extension: .txt.bak:
sed -i '.bak' 's/--/—/g' *.txt
To completely replace the files, specify an empty extension:
sed -i '' 's/--/—/g' *.txt
sed -i 's/--/–/g' *.txt ought to work. The -i flag to sed makes it act on the files in-place, the s stands for substitute, and the g makes it replace multiple occurrences of the pattern on the same line. Look up sed's documentation for more information.
EDIT: This works on GNU/Linux; it turns out that the syntax is slightly different on OSX (see comments and accepted answer).

Resources