I have this text file, which looks like this
Item:
SubItem01
SubItem02
SubItem03
Item2:
SubItem0201
SubItem0202
Item3:
SubItem0301
...etc...
And I need is to get it to look like this:
Item=>SubItem01
Item=>SubItem02
Item=>SubItem03
Item2=>SubItem0201
Item2=>SubItem0202
Item3=>SubItem0301
I am aware of the fact, that I need two for loops to get this. I did some tests, but... well, it didn't end up well.
for(( c=1; c<=lineCount; c++ ))
do
var=`sed -n "${c}p" TMPFILE`
echo "$var"
if [[ "$var" == *:* ]];
then
printf "%s->" $var
else
printf "%s\n"
fi
done
Could anyone please kick me back on the road? I tried bunch of variete ways, but I am not getting anywhere. Thanks.
If you want to continue down the bash shell road, you can do something like this:
item_re="^(Item.*):$"
while read -r; do
if [[ $REPLY =~ $item_re ]]; then
item=${BASH_REMATCH[1]}
else
printf "%s=>%s\n" "$item" "$REPLY"
fi
done < file.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With