linux - How to split a variable with text content on an empty line
问题描述
how can i split a variable with text content by every empty line? Maybe into an array or something like this ...
Example:
line1
line2
line3
line4
line5
line6
line7
line8
line9
maybe into:
array[0]: line1
line2
line3
array[1]: line4
line5
...
Thanks!
解决方案
替换\n\n
为数据中未包含的内容,将 IFS 设置为它,填充数组。
#! /bin/bash
in='line1
line2
line3
line4
line5
line6
line7
line8
line9'
separator=$'\xff' # or whatever char not appearing in the data
${IFS+"false"} && unset oldifs || oldifs="$IFS"
IFS=$separator
arr=(${in//$'\n\n'/$separator})
${oldifs+"false"} && unset IFS || IFS="$oldifs"
printf '<%s>\n' "${arr[@]}"
推荐阅读
- maven - Maven跳过模块发布
- ios - Swift:打开/创建 SQLite 数据库适用于模拟器,但不适用于 iPhone
- python - 所以我有这个正在使用的文本文件,我想将其中的信息用于我的停车系统
- xml - 如何在 android studio 中放置滑块而不是状态栏
- pygame - pygame黑屏错误有什么解决办法吗?
- android - 如何在 Roboelectric 中模拟 Scannerscanner = new Scanner(file, "UTF-8")
- javascript - NextJS 错误:构建项目时未定义组件道具(在开发模式下一切正常)
- azure - ADF 中的集合和附加变量中的引号和特殊字符问题
- flutter - iOS 应用程序在向后滑动手势时冻结约 0.5 秒
- python - 组合饼图和条形图 - Python Matplotlib