I am new to Unix scripting and I am trying to write a script which should be able to append new line character and print the values inside a variable into new line,instead of same line.
Since data is dynamic and it may contain many more string rather only one,I have to add a loop and some conditions inside it to save the values in variable in new line.
Strings to be stored in variable in new line format:
"CA 1938" "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX" "CA 1937" "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX" "CA 1934" "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
My thinking was to write some regex comparison and use that to run the insertion of new line in this varaiable.Below is the code with which I want to achieve that operation.
var="CA 1938 XXXXXXXXXXXXXXXXXXXXXXXXXXXXX CA 1937 XXXXXXXXXXXXXXXXXXXXXXXXXXXXX CA 1934 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
for i in $var; do
#echo $i
p="$p"$'\n'"$i"
done
echo "$p"
Expected output is
CA 1938 XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
CA 1937 XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
CA 1934 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Actual output is
CA
1938
XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
CA
1937
XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
CA
1934
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
So above actual output is coming because $i is all the string values which are separated by whitespace. Could you please help me in getting what I am looking for. Actually this CA(number of the task) and XX(task description).
varusing backticks then never use it. In addition,pandiare never defined. I also doubt justdo ... donewas intentional without a loop.