【发布时间】:2021-07-23 09:45:26
【问题描述】:
我的代码检查了一个文本文件并对其进行了一些处理。我最终无法处理(缺少)换行符。我想测试该行是否在行尾有换行符。如果是这样,我想在文件的行中添加一个换行符。现在,我的代码根本没有添加任何新行,我不太清楚为什么。
用 Google 搜索过,但没有任何效果。
while read line || [ -n "$line" ]; do
...#Do things
SUB="\n"
if [[ "$line" =~ .*"$SUB".* ]]; then
echo "It's there."
printf "\n" >> $DEC
fi
done <ip.txt
我只能使用 bash(没有 sed、awk 等)。
我想要:
案例一:
ip:
Line1 (\n here)
Line2 (\n here)
Line3(no \n here)
输出:
line1 (\n here)
line2 (\n here)
line3 (no \n here)
案例 2:
ip:
Line1 (\n here)
Line2(\n here)
Line3(\n here)
输出:
line1 (\n here)
line2 (\n here)
line3 (\n here)
但我明白了:
line1(no space)line2(no space)line3
两种情况
【问题讨论】:
-
不确定我是否理解目标,也不知道您希望如何匹配一行中间的“换行符”字符 (
"$line" =~ .*"$SUB".*),所以 fwiw ...SUB="\n"会继续将 2 个字符的字符串分配给SUB... 字符 '\' 和 'n';分配换行符尝试SUB=$'\n' -
我不希望。在所有用于换行的谷歌解决方案都不起作用之后,我只是检查了子字符串检测。认为它应该工作
-
@markp-fuso 你给我的 sub 不起作用。它没有被检测到
标签: bash shell file io scripting