1

I have the following code:

cat file | grep example | sed 's/http/https/' >> report.txt

Output of that command:

https://example.com/page/index.html

This will add a link, with HTTPS, to the file. I would like, however, for that same output to also replace "URL" in a second file.

Content of the second file:

TEXT TEXT
TEXT

URL

TEXT TEXT

Desired change in second file:

TEXT TEXT
TEXT

https://example.com/page/index.html

TEXT TEXT

Is this possible?

P.S.: I can't repeat the command again, as it is actually not a simple cat but an upload process that starts the command.

  • 1
    Yes, it's possible... please expand on that replacement; btw, you don't need cat and grep. – don_crissti Apr 7 '17 at 13:58
  • I have a second file in json that contains the text URL, that I would like to replace by what comes out of the code I posted. Makes sense? :/ – t988GF Apr 7 '17 at 14:09
  • replace ` >> report.txt ` by tee -a report.txt second_file.txt ? – Archemar Apr 7 '17 at 14:32
  • note that tee is intended to write/append multiple file at once, not to edit. – Archemar Apr 7 '17 at 14:33
  • The post was improperly formatted. Changed the post to better reflect what I have and what I'm aiming for. Sorry for the inconvenience. – t988GF Apr 7 '17 at 16:27
1

For the record, you don't need cat and grep as sed alone can do it:

sed '/URL/!d;s/http/&s/'

Anyway, assuming you run a chain of commands that produce a single line of output you can do:

... | tee -a report.txt | sed 's|[\&/]|\\&|g;s|.*|s/URL/&/g|' | sed -f- -i infile.json

This appends the output to report.txt but also pipes it to sed which turns it into a sed command which is then piped to another sed which will edit the .json file in-place.

  • Worked exactly as I was hoping! Thank you very much. – t988GF Apr 7 '17 at 16:43
0

This is the short answer to a short question:

cat file | grep URL | sed 's/http/https/' | tee -a report.txt | while read $LINE; do
    sed --in-place -e "s·URL·$LINE·" thejsonfile.json
done

Make sure to replace all three · in the sed script by some character not contained in $LINE, or leave it as · (middle dot).

EDIT: I've just learned from a different question, that piping into a while read loop is considered bad practice, because of performance and other reasons. It'll get your job done, thou, so I leave this here.

| improve this answer | |
  • thejsonfile.json is getting the URL removed, and nothing added. I also made a mistake regarding the output, as the output is: This is the public address: example.com/page/index.html I've edited my question. Can this be the reason your solution isn't working? – t988GF Apr 7 '17 at 16:14
0
sed -n '/example/s/http/&s/p' file | tee -a report.txt |\
sed -e 'G;s/$/./;G;s/$/wq/;h;s|.*|/URL/c|;G' | ed -s - json_file

We build an ed code based on the first output and use that to edit the json file.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.