bash - 使用 Shell 脚本删除断线
问题描述
我正在尝试从中删除断线:
Test1,Test2,Test3,Test4
Test1,Test2,Test3,
Test4
Test1,Test2,Test3,Test4
Test1,Test2,Test3,
Test4
Test1,Test2,Test3,Test4
要得到:
Test1,Test2,Test3,Test4
Test1,Test2,Test3,Test4
Test1,Test2,Test3,Test4
Test1,Test2,Test3,Test4
Test1,Test2,Test3,Test4
但我只得到:
Test1,Test2,Test3,Test4, Test1,Test2,Test3,Test4, Test1,Test2,Test3,Test4, Test1,Test2,Test3,Test4, Test1,Test2,Test3,Test4, Test1,Test2,Test3,Test4
我正在使用以下代码:tr '\r\n' ' ' < test.txt
解决方案
如果输入文件被称为 text.txt 这个命令与 GNU sed 将解决它:
sed 'n;N;s/\n//' text.txt
说明:
n: # If auto-print is not disabled, print the pattern space, then,
# regardless,
# replace the pattern space with the next line of input. If there is no
# more
# input then sed exits without processing any more commands.
# This command is useful to skip lines (e.g. process every Nth line).
N; # Add a newline to the pattern space, then append the next line of
# input to the pattern space. If there is no more input then 'sed'
# exits without processing any more commands.
s/\n// # delete the first newline
您可以在官方文档中阅读更多内容:https ://www.gnu.org/software/sed/manual/sed.html#sed-regular-expressions
推荐阅读
- django - 基于 Django 类的视图,在 CreateView 之后保存在另一个模型中
- android - 升级 facebook 网络受众 6.5 时出现此错误
- spartacus-storefront - spartacus 4X 是否支持 Commerce Quotes?
- batch-file - wmic 的输出格式(列顺序)是确定性的吗?
- pandas - 了解熊猫系列的数据类型
- node.js - “QuerySnapshot”类型上不存在属性“id”
' - php - 数据库结果到 json
- mysql - MySQL:'%' 不匹配以 TAB 字符开头的文本,用于具有 utf8mb4_bin 排序规则的 TEXT 类型列
- android - 如何使用 BLE 与 iOS 中的 CheckSUM Logics 进行请求和响应的物联网设备进行交互
- google-sheets - 谷歌表 | 从一个字符串中提取多个值