so I am new to bash linux and I am trying to fix a .txt file.
I have a .txt file that looks something like this:
blueberry, "yummy", 12345, "I love fruit and eating apples"
blueberry, "tasty", 4455, "fruit is good for you"
blueberry, "yum", 109833, "I go crazy for fruit"
blueberry, "wooohh", 1347672, "I love fruit and
eating apples"
blueberry, "yummy yummy", 1023433, "I love fruit more than my dog"
blueberry, "yummy", 12345, "I love fruit and eating apples"
blueberry, "something good to eat", 42, "fruit is the
greatest thing EVER"
blueberry, "tasty", 4455, "fruit is good for you"
blueberry, "yum", 109833, "I go crazy for fruit"
I want to create a new .txt file that looks like this:
blueberry, "yummy", 12345, "I love fruit and eating apples"
blueberry, "tasty", 4455, "fruit is good for you"
blueberry, "yum", 109833, "I go crazy for fruit"
blueberry, "wooohh", 1347672, "I love fruit and eating apples"
blueberry, "yummy yummy", 1023433, "I love fruit more than my dog"
blueberry, "yummy", 12345, "I love fruit and eating apples"
blueberry, "something good to eat", 42, "fruit is the greatest thing EVER"
blueberry, "tasty", 4455, "fruit is good for you"
blueberry, "yum", 109833, "I go crazy for fruit"
(so the random sentences that are on two lines are put back together)
So far I have tried using echo like so:
while read p; do #for every line in the .txt file
if[[ $p == "blueberry"* ]] #if the line starts with 'blueberry'
echo -n "$p" >> newfruit.txt #add the line to newfruit.txt without a new line
else
echo " $p" >> newfruit.txt #add to the current line
fi
done <fruit.txt
but it just returns the exact same .txt file I have also tried using printf and echo -e with the same result
any suggestions or tips would be greatly appreciated! thank you!
You have several syntax errors: if[[
needs a space, if
needs a then. Aside from that, your logic is a little bit incorrect. This should do it:
while read p; do
if [[ $p == "blueberry"* ]]; then
if [[ -n "$notfirst" ]]; then # in all blueberry lines but the first,
echo >> newfruit.txt # make sure the previous line is terminated
fi
echo -n "$p" >> newfruit.txt # then wait for possible continuation
else
echo -n " $p" >> newfruit.txt # there's the continuation!
fi
notfirst=1 # don't add newline before the first line
done < fruit.txt
if [[ -n "$notfirst" ]]; then # do add the newline after the last line
echo >> newfruit.txt
fi
awk '{printf $0}/"$/{printf "\n"}' fruit.txt
Print each line without new line character. If line ends with ", print newline character
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.