This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Use slashes in sed replace
I need to find the following string /a/b/c and replace it with /r/s/t
It is a string and not a folder
/sam/pam/nancy --> /tim/cook/iphone
I am in the directory and just need to update multiple files having this line.
Use sed to change the files in-place. For example:
sed -i 's|/a/b/c|/r/s/t|g' *.txt
perl -pi -e 's/\/a\/b\/c/\/r\/s\/t/g' file_name
Related
This question already has answers here:
Combining two sed commands
(2 answers)
Closed last month.
I have string as WEB-INF/lib/abc.jar
This has to be removed via usage of sed command and output has to be abc.jar
Input : WEB-INF/lib/abc.jar
Output: abc.jar
I am trying the command separately as
sed 's/WEB-INF//g' | sed 's/lib//g'
However this has to be done in one shot not to be used in different commands .
Please suggest
Using sed
$ sed 's~[^.]*/~~' input_file
abc.jar
This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 4 years ago.
I know that for replacing a string (in a file which has matchstring string), we can use following command
grep -rl matchstring somedir/ | xargs sed -i 's/string1/string2/g'
How can I use/change the command if my string has special characters like "/"?
For example:
string1: "/home/folder1"
string2: "/home/folder1/folder2"
As #jamieguinan mentioned in his command, almost any delimiter character can be used. So, I changed the command as following: grep -rl matchstring somedir/ | xargs sed -i 's,string1,string2,g' Where string1 and string2 are: /home/folder1 and /home/folder1/folder2, respectively.
This question already has answers here:
How do you exclude symlinks in a grep?
(5 answers)
Closed 5 years ago.
How can I ignore to follow symbolic links with grep ?
I tried to use with grep -R but it don't help me
Try grep -r instead. It doesn't follow symbolic links .... according to the manual entry!
Another option would be to use find <dir> -P -type f ... | xargs grep .... (Or leave out the -P because not following symlinks is default behavior for find. If you have whitespace in pathnames, use find ... -path0 and xargs -0.)
This question already has answers here:
Add a header to a tab delimited file
(8 answers)
Closed 6 years ago.
I have pipe separated large file(s) that has more 10,000,000 lines. All of these files are missing header and only the first split has the header. I want insert the header line for the rest of the files.
I tried following:
sed -i '1 i \user_id|name|age|transactions' file.txt
but this didn't work. What is the most efficient way to insert a header line for large file.
Similar question has been asked, unfortunately solution didn't work. It could be due to sed structure of understanding pipe separation might be different.
Try this:
sed '1s/.*/user_id|name|age|transactions\n&/' file.txt
This question already has answers here:
Find and replace in file and overwrite file doesn't work, it empties the file
(12 answers)
Closed 6 years ago.
I am trying to modify a file using sed in a linux server (Ubuntu 16.04).
Here is an example of the code I am running:
sed 's/lineToChange/newString/' example.txt > example.txt
I feel like I should see newString in example.txt after executing this command since the result of the sed command (which prints newString when executed by itself without the redirect) is redirected to overwrite the example.txt content.
Unfortunately the file ends up empty when I do this...
My common sense is telling me that this should be right but clearly there is something I just don't understand here.
If you want to edit a file inline, you should use the -i option:
sed -i 's/lineToChange/newString/' example.txt
This runs sed into a new file and moves that file to example.txt. Whenever you do ">", you essentially empty out example.txt which makes it empty for sed to work on.