子字符串检测无法检测换行符

Substring Detection not able to detect newline

我的代码遍历一个文本文件并对其进行一些处理。最后,我在处理(缺少)换行符时遇到了麻烦。我想测试该行的末尾是否有换行符。如果是这样,我想在文件的行中添加一个换行符。现在,我的代码根本没有添加任何新行,我不太清楚为什么。

用谷歌搜索但没有任何效果。

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)

案例二:

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

对于这两种情况

您当前的方法存在两个问题。第一个是 read 从行尾删除换行符,因此您无法检查换行符的结果——它不会在那里。 read 如果到达文件末尾而不是换行符,将 return 显示错误状态,这就是为什么您需要 || [ -n "$line" ] 来防止循环在读取未终止的行时退出。

第二个问题是SUB="\n"在变量中存储了一个反斜杠和一个“n”;要换行,请使用 SUB=$'\n'.

根据您要在循环中执行的其他操作,有多种选择。如果在文件末尾添加缺失的换行符是唯一的目标,那么 this question.

的答案中有很多选项

如果您需要通读这些行,在 shell 中处理它们,然后在末尾添加缺少的换行符输出它们,然后只需使用您当前的循环,并用一个换行符 -- 无论它最初是否存在,您都需要添加它,如果您总是添加它,它就会一直存在。

如果您需要明确找出最后一行是否有换行符,如果有则做一些不同的事情,一个选择是稍微修改您的原始代码:

while read line; do
    # process lines that had newlines at the end
done <ip.txt
if [ -n "$line" ]; then
    # final line was missing a newline; process it here
fi

另一种选择是将整个文件读入一个数组(每行作为一个数组条目),因为 mapfile 不会删除行终止符(除非您特别要求它使用 -t):

mapfile ipArray <ip.txt
for line in "${foo[@]}"; do
    if [[ "$line" = *$'\n' ]]; then
        # Process line with newline at end
        cleanLine="${line%$'\n'}"    # If you need the line *without* newline
    else
        # Process line without newline
    fi
done