0

As the question implies I am intending to store the input through a bash script in raw format using a single variable.

Basically when the input prompt occurs, I would be pasting some bunch of lines and then writing that stored variable to a text file. After a bit of search I did came across readarray but it is not storing the input/paste in its original paste state

Just for the purpose of simplicity lets say the input/paste I am intending to store in the variable is as follows:

1
 2
  3
   4
    5

Expected script

#!/bin/bash
#the part i need help with ( Store the multiple line input/paste in raw format in a variable called let say test ) 
....
echo "$test" > test.txt
cat test.txt

This should print that 1..5 exactly in the format I have pasted during input prompt in console / also shown above

Edit - It might be possible someone is curious what is my actual use case, so I am giving an example what my paste actually is in real use case. I used 1..5 example in question only for simplicity.

https://cdn.jwplayer.com/videos/XXXizsW4-32313922.mp4
  out=Lecture 01- AS 1 Theory.mp4
https://cdn.jwplayer.com/videos/XXX6XFPB-32313922.mp4
  out=Lecture 02- AS 1 Question.mp4
https://cdn.jwplayer.com/videos/XXXIeQNM-32313922.mp4
  out=Lecture 03- AS 2 Theory.mp4

I would be pasting text in such format only

https://link
  out=Some-Name.mp4 ( 2 spaces before word out )

Although there are 300,400 such pairs of lines that I would be pasting (if that matters).

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sachin
  • 1,217
  • 2
  • 11
  • 31
  • define: "readarray but it doesn't suit my needs" – Luuk Aug 08 '20 at 15:52
  • It is not storing my paste in variable in a raw format – Sachin Aug 08 '20 at 15:54
  • What do you mean by "raw format"? Can you provide an example of an input and the expected output, in an unambiguous form (perhaps a hex dump or a `$'C-style string\n'` if you need to include control codes or other nonprintable characters)? – tripleee Aug 08 '20 at 16:05
  • By raw format , i mean it should store exactly what i paste during the input prompt . It should preserve all indentation and spaces – Sachin Aug 08 '20 at 16:06
  • @tripleee there you go , i have also added my real use case scenario in question. I hope now everything is clear – Sachin Aug 08 '20 at 16:18
  • *raw* is really a poor word choice here, I thought you wanted to keep NUL bytes too, which is impossible. – oguz ismail Aug 08 '20 at 16:22
  • You are probably writing the file incorrectly. I am unable to repro any corruption; demo at https://ideone.com/RmERB6 – tripleee Aug 08 '20 at 16:25
  • i apologize to everyone for my incorrect terminology that might have caused any confusion . @tripleee , i saw your solution and realized my mistake thanks for correction . If you can answer that , it would give viewers an alternative answer ( we have got one good answers using while loop , other 2 are invalid ) – Sachin Aug 08 '20 at 16:53

5 Answers5

6

If this isn't all you need then post a new question:

$ foo=$(< file)
$ printf '%s\n' "$foo"
1
 2
  3
   4
    5
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
4

Given:

$ cat test.txt
 1
  2
   3
    4
     5

Several ways:

First, you can use process substitution to read the file contents into a single variable:

$ txt="$(cat test.txt)"
$ echo "$txt"
 1
  2
   3
    4
     5

Or, you can loop over the contents of the file line-by-line and preserve the formatting:

$ while IFS= read -r line || [[ -n $line ]]; do printf "'%s'\n" "$line"; done <test.txt
' 1'
'  2'
'   3'
'    4'
'     5'

(Remove the ' in printf "'%s'\n" to remove those from the output)


Edit

Your edit makes your question less clear, but it seems that you are dealing with a large number (300,400 you state) of line pairs.

If so, your best bet is to use awk to deal with line pairs:

$ echo "https://cdn.jwplayer.com/videos/XXXizsW4-32313922.mp4
    out=Lecture 01- AS 1 Theory.mp4
https://cdn.jwplayer.com/videos/XXX6XFPB-32313922.mp4
    out=Lecture 02- AS 1 Question.mp4
https://cdn.jwplayer.com/videos/XXXIeQNM-32313922.mp4
    out=Lecture 03- AS 2 Theory.mp4"  | awk '!(NR%2){sub(/mp4/,"MP4 EVEN LINE")}1' 
https://cdn.jwplayer.com/videos/XXXizsW4-32313922.mp4
    out=Lecture 01- AS 1 Theory.MP4 EVEN LINE
https://cdn.jwplayer.com/videos/XXX6XFPB-32313922.mp4
    out=Lecture 02- AS 1 Question.MP4 EVEN LINE
https://cdn.jwplayer.com/videos/XXXIeQNM-32313922.mp4
    out=Lecture 03- AS 2 Theory.MP4 EVEN LINE

(Remove the ! from !(NR%2) to deal with the odd pairs.)

dawg
  • 98,345
  • 23
  • 131
  • 206
2

The problem is apparently with how you write the variable. If you use an array, you need to quote it correctly when you write it.

readarray -d $'\n' test
printf '%s\n' "${test[@]}"

Perhaps see also When to wrap quotes around a shell variable?

tripleee
  • 175,061
  • 34
  • 275
  • 318
1

Something along these lines:

#!/bin/bash

IFS= read -r -d $'\004' test
echo "$test" > /tmp/test.txt

Press CTRL-D after you've pasted the input and check the file /tmp/test.txt.

M. Nejat Aydin
  • 9,597
  • 1
  • 7
  • 17
0
$ cat 12345
1
 2
  3
   4
    5
$ cat 12.sh
#!/bin/bash

readarray -d '\n' lines

echo "$lines" > 12345.tmp
echo "$lines"
$ cat 12345 | ./12.sh
1
 2
  3
   4
    5

$ cat 12345.tmp
1
 2
  3
   4
    5

$
Luuk
  • 12,245
  • 5
  • 22
  • 33
  • 1
    This is not correct; `$lines` is not the array, just its first element. – tripleee Aug 08 '20 at 16:28
  • I fail to see the difference between my post, and your (@tripleee). Both read the data in the same way, so in your case the data is also (only) in the first element? – Luuk Aug 08 '20 at 17:24
  • Ok, but i do not have a '$' in `readarray -d '\n' lines`. Demo: https://ideone.com/0ODbmb – Luuk Aug 09 '20 at 08:16
  • Yeah, that's a separate bug, as pointed out by Ed Morton. Demo: https://ideone.com/TGDqvg – tripleee Aug 09 '20 at 08:34
  • On 'normal' input () the complete file gets read into one variable (the first element of an array). The separate bug is a completely different story. Finally, yes maybe i should have typed `echo ${lines[@]}` for the output, but since i only read 1 element .... – Luuk Aug 09 '20 at 09:00
  • 1
    Then why do you use an array at all? `read -r -d 'no such separator no sirree' lines` would be less confusing. (And still you need to quote the variable you `echo`.) Demo: https://ideone.com/Zo2fsZ – tripleee Aug 09 '20 at 09:22
  • See first comment on the question, which is a reaction on the question. Unfortunately the text in the question is changed since i posted that reaction. – Luuk Aug 09 '20 at 09:26
  • 2
    Still "I want an array but none of the useful features of an array" is a puzzling interpretation. – tripleee Aug 09 '20 at 09:31