0

I have a bash script that converts txt image with multi-lune to a single parameter which should be a single line because I need to pass it later as an argument to a different script.

the script removes the new lines "\n" and put space instead-but What I need is to leave the "\n" but still create a single line parameter.

original file (That is always changing and can have more/less lines), images.txt:

feeder-srv
 newTag: develop_eaf7128
input-registry-srv
 newTag: master_e16b788
shard-mongo-srv
 newTag: master_2d2262e
shard-es-srv
 newTag: master_66ac45e

Command to convert to one line parameter:

images_one_line=$(tr '\n' ' ' < images.txt)

parameter output is:

feeder-srv newTag: develop_eaf7128 input-registry-srv newTag: master_e16b788 shard-mongo-srv newTag: master_2d2262e shard-es-srv newTag: master_66ac45e

Which I later pass as argument to another script: source wiki-update.sh "${images_one_line}"

parameter output I need:

feeder-srv newTag: develop_eaf7128\n input-registry-srv newTag: master_e16b788\n shard-mongo-srv newTag: master_2d2262e\n shard-es-srv newTag: master_66ac45e\n

I need the "\n" because I then using curl to perform a REST-API request to edit a page and I need to keep a certain HTML format:

enter image description here

Shahar Hamuzim Rajuan
  • 5,610
  • 9
  • 53
  • 91
  • The literal characters backslash and n? – Shawn Jun 07 '21 at 08:12
  • 1
    Or do just want to remove every other newline? – James Brown Jun 07 '21 at 08:14
  • 2
    *"I need the "\n" because I then using curl to perform a REST-API request to edit a page and I need to keep a certain HTML format:"* - no you don't. You're trying to solve the *completely* wrong problem. – Tomalak Jun 07 '21 at 08:14
  • replace the new line with a special character instead of a space like '|', in the second script convert it back before you feed it to curl – petrch Jun 07 '21 at 08:16
  • `parameter output is:` Variable assignment does not output anything. Please post the _exact code_ you are using to get the output. `parameter output I need:` You want to replace newlines (one character) by two characters \ and `n`? Are you sure you are not asking XY question? `the script removes the new lines "\n" and put space instead-but` Please post the script sources. – KamilCuk Jun 07 '21 at 08:16
  • `images_one_line=$(perl -0777 -pe 's/\n/\\n/g' images.txt)` – Shawn Jun 07 '21 at 08:17
  • **1)** Why are you trying to cram the file contents on the command line, why does `wiki-update.sh` not accept a filename instead? **2)** `curl` accepts a filename for upload **3)** `curl` does not care about newlines in uploaded files. **4)** replacing newlines with a literal `\n` will not have the effect you want. **5)** HTML does not care about newlines unless you specifically use `
    ` or `
    ` or CSS `white-space: pre;`. So whatever you're trying to do here, it almost certainly is the wrong thing.
    – Tomalak Jun 07 '21 at 08:18

1 Answers1

0

Check my awk-based approach below:

$ echo 'feeder-srv
newTag: develop_eaf7128
input-registry-srv
newTag: master_e16b788
shard-mongo-srv
newTag: master_2d2262e
shard-es-srv
newTag: master_66ac45e' | awk 'BEGIN{c=1;}
                               {
                                   if(c % 2 == 0){
                                       printf("%s\\n ", $0)
                                   }
                                   else{
                                       printf("%s ", $0)
                                   }
                                   c++;
                                }'

feeder-srv  newTag: develop_eaf7128\n input-registry-srv  newTag: master_e16b788\n shard-mongo-srv  newTag: master_2d2262e\n shard-es-srv  newTag: master_66ac45e\n
funk
  • 2,221
  • 1
  • 24
  • 23