-1

I need to replace a line in a file. The new line includes path, spaces, comma and quotes.

Replace this line: "download-dir"

Into this one: ___"download-dir": "/basedirectory/downloads", (underline replacing space)

I'm running sed -i through bash script and I used "$(dirname $(readlink -f $0))" to get the base directory (basedirectory/downloads).

All I was able to do was sed -i '/"download-dir"/c\'"$(dirname $(readlink -f $0))"/downloads ~/arq, which would change the line partially to what I want.

However, I couldn't add ___"download-dir":_ (underline replacing spaces) before the path and a coma after it.

Suggestions, please?

Tanktalus
  • 21,664
  • 5
  • 41
  • 68
André Luís
  • 141
  • 2
  • 7
  • Please add sample input and your desired output for that sample input to your question. – Cyrus Jan 11 '19 at 18:41
  • There (2nd and 3rd lines). – André Luís Jan 11 '19 at 18:47
  • 2
    If your content is JSON, `sed` is the wrong tool. Use a JSON-aware mechanism for editing, such as `jq`. If we had a complete enough example of the file to be syntactically correct, we could actually test our answers. – Charles Duffy Jan 11 '19 at 18:48
  • BTW, if you use the `{}` button to format while-line or multi-line code segments, whitespace will be preserved so you don't need to do hackery like this messing around with underscores. – Charles Duffy Jan 11 '19 at 18:49
  • (and if your content *would* be valid JSON if the original read something like `"download-dir": "",` instead of `"download-dir"` alone, is there a *reason* the input/template is in the format it is?) – Charles Duffy Jan 11 '19 at 18:51
  • ...note that our markdown engine here *is not* GitHub-flavored, so triple-backticks don't have the special meaning you might be used to them having elsewhere. – Charles Duffy Jan 11 '19 at 18:52
  • @CharlesDuffy As for your 3rd response, you're right. All I wanted was to replace X in ```"download-dir": "X",``` with a variable, and my lack of knowledge can't figure out the best way to do so. All I was trying to do is to use a command I already used in the past to do the job. Apparently, it's the hardest way to achieve my goal. As for the first response, that sound like alien coding for me. As for the last one, I'm sorry; I was just trying to make the text better to read. – André Luís Jan 11 '19 at 18:57

2 Answers2

0

Solution: command or variable must be between '.

Line before: "download-dir": "path",

Line after: "download-dir": "/basedir/downloads",

Code: sed -i '/"download-dir"/c\ "download-dir": "'"$(dirname $(readlink -f $0))"'/downloads",' file

The spaces after c\ was on purpose.

Thanks.

André Luís
  • 141
  • 2
  • 7
0

Using jq to generate content in a manner that's safe for all possible filenames, including ones containing characters that can't be used in JSON literals without quoting:

newDir=$(realpath "${BASH_SOURCE%/*}")   # $0 is unreliable; see BashFAQ #28

jq --arg newDir "$newDir"      \
   '.["download-dir"]=$newDir' \
   <in.json >out.json

See BashFAQ #28 re: why $0 should not be used to identify your script's location, the bash-hackers' wiki page on parameter expansion to understand the ${varname%/*} syntax used in place of dirname, and the mention of JSON parsing in BashWeaknesses.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441