# bash / put a text file content in a variable

**URL:** https://dietpi.com/forum/t/bash-put-a-text-file-content-in-a-variable/1300
**Category:** Troubleshooting
**Created:** [2 June 2017 12:17 UTC](https://dietpi.com/forum/t/bash-put-a-text-file-content-in-a-variable/1300 "2017-06-02T12:17:35Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![pakikje](https://dietpi.com/forum/letter_avatar_proxy/v4/letter/p/45deac/32.png) [@pakikje](https://dietpi.com/forum/u/pakikje)
#### Post date: [2 June 2017 12:17 UTC](https://dietpi.com/forum/t/bash-put-a-text-file-content-in-a-variable/1300/1 "2017-06-02T12:17:35Z")

</div>

Hello guys,

I have a txt-file that has a (changing) number as content in it. ex: “12345”

I need to put this (changing) number in a variable. This number is filtered out of a html-page (with a sed-command).

Who can give me a tip?  
  
  
  
(this is the script)  
  
  
#!/bin/bash

wget nos.nl/uitzending/nos-journaal  
grep ‘data-id=.\*"’ nos-journaal \> dataid.txt #get the data-id number  
sed -i ‘s/data-id="//g’ dataid.txt #clean up the number, remove quotes and spaces and junk  
sed -i ‘s/"//g’ dataid.txt #  
sed -i ‘s/ //g’ dataid.txt #

#put the content of dataid.txt in a variable!  
#replace year/month/day with variables!  
#wget [https://download.omroep.nl/nos/content/mp4/web03/2017/06/02/$dataid/mp4\_web03.mp4](https://download.omroep.nl/nos/content/mp4/web03/2017/06/02/%24dataid/mp4_web03.mp4)

At the end i want to wget a mp4-file. I want to put the “number” in a variable ($dataid) and in the download path.

---

<div class="post-metadata">

### Author: ![Fourdee](https://dietpi.com/forum/user_avatar/dietpi.com/fourdee/32/11_2.png) [@Fourdee](https://dietpi.com/forum/u/Fourdee)
#### Post date: [3 June 2017 14:01 UTC](https://dietpi.com/forum/t/bash-put-a-text-file-content-in-a-variable/1300/2 "2017-06-03T14:01:27Z")

</div>

> [@pakikje](#):
>
> #!/bin/bash
> 
> wget nos.nl/uitzending/nos-journaal  
> grep ‘data-id=.\*"’ nos-journaal \> dataid.txt #get the data-id number  
> sed -i ‘s/data-id="//g’ dataid.txt #clean up the number, remove quotes and spaces and junk  
> sed -i ‘s/"//g’ dataid.txt #  
> sed -i ‘s/ //g’ dataid.txt #
> 
> At the end i want to wget a mp4-file. I want to put the “number” in a variable ($dataid) and in the download path.

This will read dataid.txt to a variable MY\_VAR:

```plaintext
#!/bin/bash

wget nos.nl/uitzending/nos-journaal
grep 'data-id=.*\"' nos-journaal > dataid.txt #get the data-id number
sed -i 's/data-id=\"//g' dataid.txt #clean up the number, remove quotes and spaces and junk
sed -i 's/\"//g' dataid.txt
sed -i 's/ //g' dataid.txt

MY_VAR=$(cat dataid.txt) # Load into variable

echo $MY_VAR # Print variable to screen

```

---

<div class="post-metadata">

### Author: ![pakikje](https://dietpi.com/forum/letter_avatar_proxy/v4/letter/p/45deac/32.png) [@pakikje](https://dietpi.com/forum/u/pakikje)
#### Post date: [3 June 2017 19:53 UTC](https://dietpi.com/forum/t/bash-put-a-text-file-content-in-a-variable/1300/3 "2017-06-03T19:53:01Z")

</div>

THNX!! Works…

My script:

#!/bin/bash  
rm _txt  
rm nos_  
wget nos.nl/uitzending/nos-journaal  
grep ‘data-id=.\*"’ nos-journaal \> dataid.txt  
sed -i ‘s/data-id="//g’ dataid.txt  
sed -i ‘s/"//g’ dataid.txt  
sed -i ‘s/ //g’ dataid.txt  
MY\_VAR=$(cat dataid.txt) # Load into variable  
MY\_YEAR=$(date +’%Y’)  
MY\_MONTH=$(date +’%m’)  
MY\_DAY=$(date +’%d’)  
clear  
wget [https://download.omroep.nl/nos/content/mp4/web01/$MY\_YEAR/$MY\_MONTH/$MY\_DAY/$MY\_$](https://download.omroep.nl/nos/content/mp4/web01/%24MY_YEAR/%24MY_MONTH/%24MY_DAY/%24MY_%24)  
wget [https://download.omroep.nl/nos/content/mp4/web02/$MY\_YEAR/$MY\_MONTH/$MY\_DAY/$MY\_$](https://download.omroep.nl/nos/content/mp4/web02/%24MY_YEAR/%24MY_MONTH/%24MY_DAY/%24MY_%24)  
wget [https://download.omroep.nl/nos/content/mp4/web03/$MY\_YEAR/$MY\_MONTH/$MY\_DAY/$MY\_$](https://download.omroep.nl/nos/content/mp4/web03/%24MY_YEAR/%24MY_MONTH/%24MY_DAY/%24MY_%24)  
mv mp4\_web01.mp4 nos.mp4  
mv mp4\_web02.mp4 nos.mp4  
mv mp4\_web03.mp4 nos.mp4  
rm nos-journaal  
rm \*txt  
mv nos.mp4 /mnt/dietpi\_userdata/Video/nos.avi  
service minidlna restart

ps: i have to add a logic in the script that will check id web03 is present (and then wget it) or web02 or web01 (these are the same video’s but then in decreasing sharpness-order)
