简体   繁体   English

如何在Linux bash中将相邻行放在.txt文件中的同一行上?

[英]How do I put adjacent lines in a .txt file on the same line in linux bash?

so I am new to bash linux and I am trying to fix a .txt file. 所以我是bash linux的新手,我正在尝试修复.txt文件。

I have a .txt file that looks something like this: 我有一个.txt文件,看起来像这样:

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: 我想创建一个新的.txt文件,如下所示:

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: 到目前为止,我已经尝试过使用echo这样的方式:

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 但它只是返回完全相同的.txt文件,我也尝试使用printf和echo -e获得相同的结果

any suggestions or tips would be greatly appreciated! 任何建议或技巧将不胜感激! thank you! 谢谢!

You have several syntax errors: if[[ needs a space, if needs a then. 您有几个语法错误: if[[需要一个空格, if需要一个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 如果行以“结尾”,则打印换行符

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Linux中使用bash从文本文件中提取行? - How do I extract lines out of a text file using bash in linux? 在Linux shell bash脚本中,如何在同一行打印到文件? - In Linux shell bash script, how to print to a file at the same line? 我有如下 3 行的文件。 使用 linux 如何获取一行的拆分变量并将其附加到同一行 - I have file with with 3 lines as follows. Using linux how can i get the split variables of a line and append it to the same line 如何在多行中分隔一行Bash脚本? - How Do I Separate a Line of Bash Script Over Multiple Lines? 如何在 bash 中为 printf 的每一行添加评论? - How do I put comment on each line in bash for printf? 如何使用 %BASH 将包含特定字符串的行从 a.csv 文件复制到 Python 中的 a.txt 文件? - How do you copy lines that contain specific strings from a .csv file to a .txt file in Python using %BASH? 如何使用termianl在linux中更改具有相同文件名(例如:data.txt)的所有子目录中的字符串? - how do I change string in all sub directories with same file name (For eg: data.txt) in linux using termianl? 是否可以使用 linux 比较同一行 in.txt 文件的 2 个内容? - Is it possible to compare 2 contents of same line in .txt file using linux? 如何使用bash在txt中随机行 - how to random lines in txt with bash Linux bash 脚本:如何将命令行放入变量中? - Linux bash script: How to put command line into variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM