I have a file and the idea is to grep the content following a pattern ("create mode") and to store this content in a bash variable in order to use it then in a curl statement. I have the commands working but the problem is that when the output is assigned to the variable, the new lines were replaced by white spaces.
Example of file
...
example1
example2
create mode 100644 repo/linux-image-5.4.0-89-generic_5.4.0-89.100_amd64.deb
create mode 100644 repo/linux-modules-5.4.0-89-generic_5.4.0-89.100_amd64.deb
create mode 100644 repo/python3.8-minimal_3.8.10-0ubuntu1~20.04.1_amd64.deb
create mode 100644 repo/python3.8_3.8.10-0ubuntu1~20.04.1_amd64.deb
example3
example4
...
I need to grep only the ubuntu packages and store them into a bash variable var. The idea is to keep the format (With the \n)
I tried using grep, sed and modifiying the output with printf and assigning the output to a variable.
Example using grep and printf
test_variable=$( cat file.txt | grep "create mode" )
var=$( printf '%s\n' "${test_variable//' create mode 100644 repo/'/}" )
echo $var
repo/linux-image-5.4.0-89-generic_5.4.0-89.100_amd64.deb repo/linux-modules-5.4.0-89-generic_5.4.0-89.100_amd64.deb repo/python3.8-minimal_3.8.10-0ubuntu1~20.04.1_amd64.deb repo/python3.8_3.8.10-0ubuntu1~20.04.1_amd64.deb
When I used the same printf command but without assigning the output to a variable, the result was:
linux-image-5.4.0-89-generic_5.4.0-89.100_amd64.deb
linux-modules-5.4.0-89-generic_5.4.0-89.100_amd64.deb
python3.8-minimal_3.8.10-0ubuntu1~20.04.1_amd64.deb
python3.8_3.8.10-0ubuntu1~20.04.1_amd64.deb
I tried using tr and others for troubleshooting this.
Other example using sed
sed -e '/create mode/!d' file.txt
create mode 100644 repo/linux-image-5.4.0-89-generic_5.4.0-89.100_amd64.deb
create mode 100644 repo/linux-modules-5.4.0-89-generic_5.4.0-89.100_amd64.deb
create mode 100644 repo/python3.8-minimal_3.8.10-0ubuntu1~20.04.1_amd64.deb
create mode 100644 repo/python3.8_3.8.10-0ubuntu1~20.04.1_amd64.deb
That is ok. But when i tried to assign this output to a variable, the newlines were replaced by a white space.
var=$( sed -e '/create mode/!d' file.txt )
echo $var
create mode 100644 repo/linux-image-5.4.0-89-generic_5.4.0-89.100_amd64.deb create mode 100644 repo/linux-modules-5.4.0-89-generic_5.4.0-89.100_amd64.deb create mode 100644 repo/python3.8-minimal_3.8.10-0ubuntu1~20.04.1_amd64.deb create mode 100644 repo/python3.8_3.8.10-0ubuntu1~20.04.1_amd64.deb
Could you help me with this? Thanks