1

I'm trying to insert encoded, encrypted content at certain line of code in header file (.h) in Xcode project which is written in Objective-C, from bash script. Also, I work in mac (OS X). Code in script that I have now is this:

sed -n '' "10i
$jsonConfigFileContent
" Sources/ConfigurationInteractor.h

And when I execute the script from terminal I get next error:

sed: 10i
#define jsonConfigFileContent @"CwtTuNRnWcO2HJfytXDYMnoeRQnZOGbEW6/f6uYS1TM2uUSvvEQQXwUH7D6iU5HQNoKuYvfW4vp4YUqsQ9oFXga2Dz/SQsALNouAi9hDNfr+AaE9i7RW6hUYZuu742xage9l9m8+4bWudiJiCKBVH+2gXN8qhoopa/46en9XYPr78warXotiGxBzrkCP2kBic1lGlAabQy4LEMMRklOShsyxZqb6sw4Pl5PZlp9G9vF1u1+IOPkmfG82JgcvJTsXFcs+yyedZERhkGMKL+crKewMlnQLj0k49KaptKYBLRtbq98y/yXAqN9kWc8eZhKw9Ov+sVz2kHcjOhVGgUY8eY6wMqo9jndDWx9bmxiu6gVvtU1i1ycZedBJWxAfq3XFBzB+kII5+ufVfIO93yNqNAByjnzNTkCxJ2ko/P8fYeppNmmn/Y6aBi9qQ9u1j1clt37ylYogOnOB9ph+qCoT640Z+0/HponVG1JFSiY3j1EvxA0lT4qh8SlLm5BpIL5tUfJ913k7hrdEVl1pA30MvDw2LUDB/hNG1B//dVuqb+LN4XFS7Ckac53xko4z1txrADTT2cOJKwZlOHWCMKtGJC9n6xmZAzOBqqQiPCTqxQw2FSxyVcJqGe6lstWfzXDUZYtzofvsdj6+Gdg5dz9YYESr9mAIpD5wuMTi9WKf4yeV735I9JtzOhXDGljLpDB8F0riUAVatzq7YQ8LGNfsNIg9eMD8uSnBRDUCEwCB5IdAfXlLY2w6h/Hm2TUOYgrP/pEzwmBP1na/jh/CbDrpJsDX0TA4LufglkUvAMW1X+waN5epyDibj99GIZxofTjPVn82diQTgjq0nqPt8o3cAGm9AKFD+8aPeK4w1GM6tSSlWDQe8byHFRy2P6M4tzfraZgYnz7S5XRlp8900fvTFDG/bPe2CP/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PNkaAywzgM/5yKckrYAJuUetTpPuzNQi1TBAxcpfJZZwbf5U/KY4Ab/yaQKKjJ9FN5ub2e5VF8T/uVXf0LzEDrJaOTQPcK+A3+fbvEBXobOM3odlZTezqQoZ/hpZQtflTzZj8eCRN+WAIQMqU7XvBGH4mC/gDhEcph5g2MpKVYlxLSj3FW70LzHbg6HEzCEX/9+dJp+XBfExVT5Z9JKnLcWMbPAGKwhJlAG3AB1s4Euvgq+vq0GZmWiJfi0C20d+OTWqDGkWLEQ2k1yhpbA7mHchFvkvDJHccuUifNVHEVsVhAKI+ZlASZmxi+H0M5rpM6NSgJiYPTnIt1oT+78GLDdO/xrck+ic8FIjAuTR8vZZ/FxllFIl6FUu8sv5zjVRd40f/b/UgtyTocRkZ6DPPGYXuv5/rJgGHiGA59JjZ78R1FpWkPObkU/gX5kp5nWIyaprsVVgnzp5ultcPGlTp0JvpihoT08x051FC+7JRqrp5NPJpAkrky45spLD0h8w5GXkQ6VQALMKZjaUzc6D7Iq6Fek="

Error: File name too long

If the jsonConfigFileContent is smaller (has less characters) the insertion works fine. But when this string is big I get this error. How to insert large string at certain line of code in header (h.) file and how to fix this error? Thanks for your answers.

EDIT: Another code that I was using in this script is this:

sed -i -e "10i\ 
$(echo $jsonConfigFileContent)
" Sources/VWConfigurationInteractor.h

but I would get this error:

sed: 3: "10i\ 
#define jsonConfi ...": invalid command code T

Also works fine with smaller strings, but when string is large it doesn't work. Besides invalid command code T I get the same error but with different letters.

MattCodes
  • 489
  • 8
  • 21
  • what does the single quotes do in your command or perhaps a typo?.. can you also show the smaller string which worked? the error doesn't seem to suggest lengthy string to be an issue.. could be just some special characters in your variable wrecking havoc – Sundeep Sep 28 '16 at 08:28
  • you can also try if this works: `(head -n9 file ; echo "$var" ; sed '1,9d' file) > outfile` – Sundeep Sep 28 '16 at 08:33
  • The `''` suffix if for use with `-i` option. eg `sed -i''`. Try to remove it. – SLePort Sep 28 '16 at 08:41
  • @Sundeep - Single quotes are there because of some other suggestion. Any other smaller string would succeed. A string with few lines. I'll edit the question. – MattCodes Sep 28 '16 at 08:42
  • @Kenavoz - I've tried to remove `''` and it wont work. – MattCodes Sep 28 '16 at 08:43
  • And you should escape the double quotes in the variable value. You can do it with Bash parameter expansion : `${jsonConfigFileContent//\"/\\\"}` – SLePort Sep 28 '16 at 08:48
  • For large string, you'd better use /r comand of sed to insert file to certain position, see [here](http://www.grymoire.com/Unix/Sed.html#uh-37) for examples. – gzh Sep 28 '16 at 08:49
  • @Sundeep, Kenavoz - I've edited my question. – MattCodes Sep 28 '16 at 08:49
  • @Kenavoz - when use `${jsonConfigFileContent//\"/\\\"}` I get this error: `sed: 3: "10i\ #define jsonConfi ...": extra characters at the end of l command` – MattCodes Sep 28 '16 at 08:53
  • @gzh - Thanks for link, I'll check it out. – MattCodes Sep 28 '16 at 08:56
  • I tried `seq 12 | sed "10i$a"` and it works just fine.. `$a` contains the huge string from your first error log.. but tested on GNU sed 4.2.2, so not sure if sed on your OSX differs in subtle ways... – Sundeep Sep 28 '16 at 09:01
  • @Sundeep - No, it wont work. I got this error: `invalid command code B` – MattCodes Sep 28 '16 at 09:06
  • this doesn't work too? `(head -n9 file ; echo "$var" ; sed '1,9d' file) > outfile` and let us know if `r` command reading from file works out.. that should be more reliable.. – Sundeep Sep 28 '16 at 09:08
  • @Sundeep - this works, but it replaces all content from file with this string. It should be inserted at line 10 in that same file. Rest of the content in that file should be the same, intact. – MattCodes Sep 28 '16 at 09:15
  • it does insert at line 10 and leaves everything else same.. that is the intention with stringing together 3 different commands.. I have no idea why any of our suggestion continues to fail.. and I cannot reproduce the error you get because am not on OSX.. hope someone else will be able to help you... – Sundeep Sep 28 '16 at 09:18
  • one last suggestion: install GNU sed on OSX: http://stackoverflow.com/questions/30003570/how-to-use-gnu-sed-on-mac-os-x – Sundeep Sep 28 '16 at 09:20
  • @Sundeep - thanks a lot for your help, I appreciate it. This is a progress also, at least it finally inserted LARGE string in the file. – MattCodes Sep 28 '16 at 09:23

2 Answers2

0

If you could use awk.

Set the value in a variable (with single quotes):

a='#define jsonConfigFileContent @"CwtTuNRnWcO2HJfytXDYMnoeRQnZOGbEW6/f6uYS1TM2uUSvvEQQXwUH7D6iU5HQNoKuYvfW4vp4YUqsQ9oFXga2Dz/SQsALNouAi9hDNfr+AaE9i7RW6hUYZuu742xage9l9m8+4bWudiJiCKBVH+2gXN8qhoopa/46en9XYPr78warXotiGxBzrkCP2kBic1lGlAabQy4LEMMRklOShsyxZqb6sw4Pl5PZlp9G9vF1u1+IOPkmfG82JgcvJTsXFcs+yyedZERhkGMKL+crKewMlnQLj0k49KaptKYBLRtbq98y/yXAqN9kWc8eZhKw9Ov+sVz2kHcjOhVGgUY8eY6wMqo9jndDWx9bmxiu6gVvtU1i1ycZedBJWxAfq3XFBzB+kII5+ufVfIO93yNqNAByjnzNTkCxJ2ko/P8fYeppNmmn/Y6aBi9qQ9u1j1clt37ylYogOnOB9ph+qCoT640Z+0/HponVG1JFSiY3j1EvxA0lT4qh8SlLm5BpIL5tUfJ913k7hrdEVl1pA30MvDw2LUDB/hNG1B//dVuqb+LN4XFS7Ckac53xko4z1txrADTT2cOJKwZlOHWCMKtGJC9n6xmZAzOBqqQiPCTqxQw2FSxyVcJqGe6lstWfzXDUZYtzofvsdj6+Gdg5dz9YYESr9mAIpD5wuMTi9WKf4yeV735I9JtzOhXDGljLpDB8F0riUAVatzq7YQ8LGNfsNIg9eMD8uSnBRDUCEwCB5IdAfXlLY2w6h/Hm2TUOYgrP/pEzwmBP1na/jh/CbDrpJsDX0TA4LufglkUvAMW1X+waN5epyDibj99GIZxofTjPVn82diQTgjq0nqPt8o3cAGm9AKFD+8aPeK4w1GM6tSSlWDQe8byHFRy2P6M4tzfraZgYnz7S5XRlp8900fvTFDG/bPe2CP/WSB+vB+bK7juBJKrg4NhALsvqb61LFADKK2MLTpCNexz5cyDdUBCf1TJOx0+TwKx+/1eyBa72Ge0Q2/3XeufumrytRq93F1yoym9Ac5pYnu45NVaQXXZbrv7iq4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oZ/hpZQtflTzZj8eCRN+WAIQMqU7XvBGH4mC/gDhEcph5g2MpKVYlxLSj3FW70LzHbg6HEzCEX/9+dJp+XBfExVT5Z9JKnLcWMbPAGKwhJlAG3AB1s4Euvgq+vq0GZmWiJfi0C20d+OTWqDGkWLEQ2k1yhpbA7mHchFvkvDJHccuUifNVHEVsVhAKI+ZlASZmxi+H0M5rpM6NSgJiYPTnIt1oT+78GLDdO/xrck+ic8FIjAuTR8vZZ/FxllFIl6FUu8sv5zjVRd40f/b/UgtyTocRkZ6DPPGYXuv5/rJgGHiGA59JjZ78R1FpWkPObkU/gX5kp5nWIyaprsVVgnzp5ultcPGlTp0JvpihoT08x051FC+7JRqrp5NPJpAkrky45spLD0h8w5GXkQ6VQALMKZjaUzc6D7Iq6Fek="'

And then execute:

$ awk -v a="$a" 'NR==3{printf("%s\n",a)};1' Sources/ConfigurationInteractor.h
0

I've managed to solve this issue. In the header file I had to add pattern variables to the certain line of code. And than in bash script I added:

sed -i "" "s|#define jsonConfigFileContent.*|$(echo $define$jsonConfigFileContent$lastMark)|g" Sources/ConfigurationInteractor.h

And it finally worked.

MattCodes
  • 489
  • 8
  • 21