How do I replace the word hello with goodbye in every file in this directory, and also recursively?
find . -type f -exec sed -i 's/hello/goodbye/g' {} +
Find and replace with sed in directory and sub directories
Your find
should look like that to avoid sending directory names to sed
:
find ./ -type f -exec sed -i -e 's/apple/orange/g' {} \;
How to replace string in files recursively via sed or awk?
If you need to find and replace text in files - sed
seems to be the best command line solution.
Search for a string in the text file and replace:
sed -i 's/PATTERN/REPLACEMENT/' file.name
Or, if you need to process multiple occurencies of PATTERN
in file, add g
key
sed -i 's/PATTERN/REPLACEMENT/g' file.name
For multiple files processing - redirect list of files to sed:
echo "${filesList}" | xargs sed -i ...
How to replace multiple substrings of a string?
Here is a short example that should do the trick with regular expressions:
import re
rep = {"condition1": "", "condition2": "text"} # define desired replacements here
# use these three lines to do the replacement
rep = dict((re.escape(k), v) for k, v in rep.iteritems())
#Python 3 renamed dict.iteritems to dict.items so use rep.items() for latest versions
pattern = re.compile("|".join(rep.keys()))
text = pattern.sub(lambda m: rep[re.escape(m.group(0))], text)
For example:
>>> pattern.sub(lambda m: rep[re.escape(m.group(0))], "(condition1) and --condition2--")
'() and --text--'
Search and replace in a console
http://www.grymoire.com/Unix/Sed.html
It's cranky and difficult, but it's one way to do it.
Here's an example:
sed -i 's/ugly/beautiful/g' /home/bruno/old-friends/sue.txt
This replaces ugly with beautiful in sue.txt.
Recursive search and replace in text files on Mac and Linux
OS X uses a mix of BSD and GNU tools, so best always check the documentation (although I had it that less
didn't even conform to the OS X manpage):
https://web.archive.org/web/20170808213955/https://developer.apple.com/legacy/library/documentation/Darwin/
Reference/ManPages/man1/sed.1.html
sed takes the argument after -i
as the extension for backups. Provide an empty string (-i ''
) for no backups.
The following should do:
find . -type f -name '*.txt' -exec sed -i '' s/this/that/g {} +
The -type f
is just good practice; sed will complain if you give it a directory or so.
-exec
is preferred over xargs
; you needn't bother with -print0
or anything.
The {} +
at the end means that find
will append all results as arguments to one instance of the called command, instead of re-running it for each result. (One exception is when the maximal number of command-line arguments allowed by the OS is breached; in that case find
will run more than one instance.)
If you get an error like "invalid byte sequence," it might help to force the standard locale by adding LC_ALL=C
at the start of the command, like so:
LC_ALL=C find . -type f -name '*.txt' -exec sed -i '' s/this/that/g {} +
How to unstage large number of files without deleting the content
git reset
If all you want is to undo an overzealous "git add" run:
git reset
Your changes will be unstaged and ready for you to re-add as you please.
DO NOT RUN git reset --hard
.
It will not only unstage your added files, but will revert any changes you made in your working directory. If you created any new files in working directory, it will not delete them though.
Insert a line of text in the end of a function
With GNU sed:
sed '/^hello()/,/^}/s/^}/ insert new line of text "good bye" here\n&/' file
If you want to edit your file "in place" use sed's option -i
.
Related Topics
Make Diff to Ignore Symbolic Link
Use Stdin from Within R Studio
Qfiledialog: How to Filter Only Executables (Under Linux)
Replace Key:Value from One File in Another File in Shellscript
Extract Unique Block of Lines from a File Using Shell Script
Tail a Log into an Excerpt in Real Time
PHPmyadmin Mcrypt Extension Is Missing
Shell Script to Shutdown/Restart Linux System
How to Grep for Presence of Specific Hex Bytes in Files
Symbol Lookup Error Undefined Symbol, But All Symbols Seem to Be Present
Can Not Add New User in Docker Container with Mounted /Etc/Passwd and /Etc/Shadow
Communication Between Linked Docker Containers
"Relocation R_X86_64_32S Against '.Bss' Can Not Be Used When Making a Shared Object"
How to Reset Tty After Exec-Ed Program Crashes
Awk Command to Create Sha2 of Individual Column and Paste into New File
Where Do Char Device Appear After Cdev_Add() Registers Successfully with Major on 117.
Is There a Linker Flag to Force It to Load All Shared Libraries at Start Time