sed regex find and replace in file (macOS)

sed replace
sed replace line in file
sed replace part of string
sed regex replace
sed replace character in string
macos sed replace string in file
sed replace string in multiple files
sed replace string in file with variable

I am having some trouble using sed to replace lines in files. What i want is to switch out all occurrences of #include <path/to/my/file.h> with #include "file.h" using regex.

when I test it with echo it gives me the output I'm looking for:

echo "#include <path/to/my/file.h>" | sed -E 's/^(#include.*[\/<])([^\/\<]*\.h)(>)$/#include \"\2\"/g'

But when I run it with a file containing #includes, it changes nothing. I feel like I have tried everything I can find online and nothing makes any difference

Here is the line I use when I try changing lines in a file:

sed -E -i '' 's/^(#include.*[\/<])([^\/\<]*\.h)(>)$/#include \"\2\"/g' /path/to/my/file.h

edit: Fixed by removing ^ and $ at the begining and end of the regex statement

It seems the line-beginning or line-ending anchors in OPs regex were not matching ... possibly because the source file did not follow the MacOS standard on line endings. MacOS (prior to OS X) uses \r while Windows uses \r\n and linux and OS X uses only \n. So, once OP removed the line-beginning or line-ending anchors in the regex, their expression matched and the replacement happened.

OPs regex without line begin/end anchors:

sed -E 's/(#include.*[\/<])([^\/\<]*\.h)(>)/#include \"\2\"/g'

The risk is that if there are other lines with #include <path/to/some/file.h> in them, those lines will also be replaced when they shouldn't. That's not too likely though.

One way the OP could tell if that might be the problem is to send some samples of text with and without the problem lines to a hexdump or octal dump program. We know that the output of echo '#include <path/to/my/file.h>' does not have any weird newlines in it (because sed with the line ending anchors worked on it), and we can examine what it does have for line endings with:

echo '#include <path/to/my/file.h>' | od -cx

And we can compare those line endings to the ones in:

cat afi.h | od -cx | head -30

using Sed to find and replace a string inside a file, I have been trying to replace '{{date}}' with a current date stamp in OSX command line. I have been using the following: sed -i -e 's/{{date}}/`date`/g' mhp.xml. Quite often when working with text files you’ll need to find and replace strings of text in one or more files. sed is a stream editor. It can perform basic text manipulation on files and input streams such as pipelines. With sed you can search, find and replace, insert, and delete words and lines. It supports basic and extended regular

Based on: In-place edits with sed on OS X

Note that MacOS does not support '-i' with NO suffix (a.k.a 'edit in place'). Per referenced question, try using a temporary suffix (.orig in this example)

sed -E -i '.orig' 's/^(#include.*[\/<])([^\/\<]*\.h)(>)$/#include \"\2\"/g' /path/to/my/file.h
rm -f /path/to/my/file.h.orig

How to Use sed to Find and Replace String in Files, With sed you can search, find and replace, insert, and delete strings and lines. It supports basic and extended regular expressions that allow you to macOS uses the BSD version and most Linux distributions come with GNU� Sed also known as “stream editor” is used to filters and transforms text in Linux-based operating systems. It is a very powerful utility and mainly used to find & replace the string but it can also able to perform some other tasks including, insertion, deletion, search, etc. sed also supports that makes it a more powerful test manipulation tool.

Try this simple sed replacement:

echo "#include <path/to/my/file.h>" |sed -r 's|<[^>]+file.h>|"file.h"|'
output
#include "file.h"
explanation:

sed -r use sed with extended RegExp

's|source-pattern|target-pattern| substitue command use | as separator (instead of normal /)

<[^>]+file.h> RegExp matching <[any char not >](more than once)file.h>

"file.h" Target RegExp match.

Sed - Find and replace with sed (Example), txt.bkp with the old version of the file. In some more complicated cases you can use regular expression to replace data. Get this in MAC OS� sed -e ':a' -e 'N' -e '$!ba' -E -f sed.cmds Input.txt > Output.txt When I run that sed command with my sed.cmds file, it successfully finds the newline characters in the sed input stream with the pattern, and then I replace the newline character with a blank space in the replacement pattern. Using the search pattern in the replacement pattern

How to use sed to find and replace text in files in Linux , Find and replace text within a file using sed command Use Stream EDitor (sed) as follows: sed -i 's/old-text/new-text/g' input. txt. The s is the substitute command of sed for find and replace. Fine tuning 2: regular expressions. Both grep and sed support regular expressions, so you can search with grep given a specific pattern and then replace the text with sed given another one. Take a look at the grep manual and the sed manual for more information. Sources. StackOverflow - How to replace a string in multiple files in linux command line

Terminal 101: Find and Replace Using Sed, This command will open sed, which will in turn print the text file to the screen, replacing the found word occurrences with the word you wish to� Find and replace text using regular expressions. When you want to search and replace specific patterns of text, use regular expressions. They can help you in pattern matching, parsing, filtering of results, and so on. Once you learn the regex syntax, you can use it for almost any language. Press Ctrl+R to open the search and replace pane.

Getting Started with Terminal: What is sed and how does it work , It uses powerful pattern matching to change text within files without To match word boundaries, you'll want to use regular expressions in your pattern. Getting Started with Terminal: Must-Know macOS Terminal Commands. I am having some trouble using sed to replace lines in files. What i want is to switch out all occurrences of #include <path/to/my/file.h> with #include "file.h" using regex.

Comments
  • What is the error message?
  • Replace ‘’ with ''.
  • I think you should remove ''
  • Can you share your source files (the #include) so that we can reproduce ?
  • If your newlines are tripping you up, you can try to remove the ^ at the beginning and the $ at the end of your sed substitution left-hand-side: sed -E 's/(#include.*[\/<])([^\/\<]*\.h)(>)/#include \"\2\"/g' It should not cause extra lines to be affected, unless you have data strings with #include in them.
  • The -i option worked for OP when they did a different substitution: sed -E -i '' 's/Dmi/substituteWord/g' /path/to/my/file.h The problem is that his original substitution is not matching.