How do I put adjacent lines in a .txt file on the same line in linux bash? -
so new bash linux , trying fix .txt file.
i have .txt file looks this:
blueberry, "yummy", 12345, "i love fruit , eating apples" blueberry, "tasty", 4455, "fruit you" blueberry, "yum", 109833, "i go crazy fruit" blueberry, "wooohh", 1347672, "i love fruit , eating apples" blueberry, "yummy yummy", 1023433, "i love fruit more dog" blueberry, "yummy", 12345, "i love fruit , eating apples" blueberry, "something eat", 42, "fruit greatest thing ever" blueberry, "tasty", 4455, "fruit you" blueberry, "yum", 109833, "i go crazy fruit"
i want create new .txt file looks this:
blueberry, "yummy", 12345, "i love fruit , eating apples" blueberry, "tasty", 4455, "fruit you" blueberry, "yum", 109833, "i go crazy fruit" blueberry, "wooohh", 1347672, "i love fruit , eating apples" blueberry, "yummy yummy", 1023433, "i love fruit more dog" blueberry, "yummy", 12345, "i love fruit , eating apples" blueberry, "something eat", 42, "fruit greatest thing ever" blueberry, "tasty", 4455, "fruit you" blueberry, "yum", 109833, "i go crazy fruit"
(so random sentences on 2 lines put together)
so far have tried using echo so:
while read p; #for every line in .txt file if[[ $p == "blueberry"* ]] #if line starts 'blueberry' echo -n "$p" >> newfruit.txt #add line newfruit.txt without new line else echo " $p" >> newfruit.txt #add current line fi done <fruit.txt
but returns exact same .txt file have tried using printf , echo -e same result
any suggestions or tips appreciated! thank you!
you have several syntax errors: if[[
needs space, if
needs then. aside that, logic little bit incorrect. should it:
while read p; if [[ $p == "blueberry"* ]]; if [[ -n "$notfirst" ]]; # in blueberry lines first, echo >> newfruit.txt # make sure previous line terminated fi echo -n "$p" >> newfruit.txt # wait possible continuation else echo -n " $p" >> newfruit.txt # there's continuation! fi notfirst=1 # don't add newline before first line done < fruit.txt if [[ -n "$notfirst" ]]; # add newline after last line echo >> newfruit.txt fi
Comments
Post a Comment