1

I have searched a little bit and I found the answer in this link

but when I try to execute this command sed -i '5ianything' file1.txt it won't work and an error message shown :

sed: 1: "file1.txt": invalid command code f

also the same error message appears when I use the other command sed -i '5i\anything' file1.txt

I don't know why it won't work for me did I miss something? is there any other way to do this?

edit

I've tried the solution provided in the comments and i run this command, but unfortunately even this won't work

sed -i.bu '5i\anything' file1.txt
    sed: 1: "5i\anything": extra characters after \ at the end of i command  

and this is my file1.txt :

1. alloha I want some cheese
2.
3. this is just a test and you know it
4. 
5. this line will change sooooo sooooooon
6. 
7. daaaamn, it changed I mean that line above me daaaaamn

edit

after I searched a little bit in man page of OSX of sed I found this :

-i extension
             Edit files in-place, saving backups with the specified extension.  If a zero-length extension is given, no backup will be saved.  It is not recommended
             to give a zero-length extension when in-place editing files, as you risk corruption or partial content in situations where disk space is exhausted, etc.
Community
  • 1
  • 1
Holy semicolon
  • 868
  • 1
  • 11
  • 27
  • 2
    Are you using an up-to-date version of gnu sed? What does `sed -V` say? – match Jan 12 '20 at 20:59
  • @match actually even sed -V doesn't work in my terminal and I got error message "sed: illegal option -- V" I am working in Mac – Holy semicolon Jan 12 '20 at 21:03
  • Is [this](https://stackoverflow.com/a/7573438/10957435) what you're looking for? –  Jan 12 '20 at 21:07
  • @Chipster no even that won't work for me – Holy semicolon Jan 12 '20 at 21:10
  • I've run this command to know the version of gnu 'gcc -dumpversion | cut -f1 -d.' and it printed '4' – Holy semicolon Jan 12 '20 at 21:12
  • Sounds like it [could be a Pearl thing](https://askubuntu.com/q/938389), but it also seems like [it could be a mac thing](https://stackoverflow.com/q/4247068/10957435), but the Mac one doesn't seem to be your issue, so I think I'm out of suggestions beyond that. –  Jan 12 '20 at 21:15
  • 1
    You need a new line after the \ – w08r Jan 12 '20 at 21:19
  • @wobr yeah your solution worked for me but I need to added a backup or an empty string like shown in the below answer – Holy semicolon Jan 12 '20 at 21:38

1 Answers1

2

For BSD/Mac sed provide an argument for in-place replacement (-i option):

sed -i .bak '5i\
anything' file1.txt

Or to replace original file without creating a backup pass an empty string to -i (you should first test this without -i and verify it works as expected):

sed -i '' '5i\
anything' file1.txt
builder-7000
  • 7,131
  • 3
  • 19
  • 43
  • 1
    unfortunately nothing worked for me, I've tried both of them and the same error occurs : `sed: 1: "5ianything": command i expects \ followed by text` – Holy semicolon Jan 12 '20 at 21:24
  • Yeah now after you added a new line it worked perfectly, can you suggest for me a document that explains why this happening in mac. – Holy semicolon Jan 12 '20 at 21:35
  • 1
    GNU sed is documented in https://www.gnu.org/software/sed/manual/sed.html. A manual page for BSD sed can be found in https://man.openbsd.org/sed (which is less comprehensive than GNUs docs) – builder-7000 Jan 12 '20 at 21:48