你好!!
正如标题中所说,我需要删除仅由白色间距分隔成两行的白色间距,如图所示,具有绿线的白色间距是我需要的删除,但是带有红线的多个白色间距,我不想删除它们,带有绿线的,仅由一个白色间距分隔,我不知道使用 AWK 或 SED 或 CUT 是否有效,问题是我不知道该怎么做,谢谢你的帮助。
我尝试使用SED和AWK进行如下操作,但没有产生任何效果
awk -F, '{gsub("\n","",$1); print}' archivo.txt
sed 's/ //g' input.txt > no-spaces.txt
最佳答案
假设:
- 输入文件有“\n”(而不是“\r\n”)行结尾。
- 非空行至少包含两个字符。
- 我们不必关心开头或结尾的空行 文件的内容。
如果支持 -z
(slurp) 选项和 \n
表示法的 GNU sed
是
可用的。请你尝试一下:
sed -Ez "s/([^\n]\n)\n([^\n])/\1\2/g" input.txt > no-spaces.txt
input.txt
示例:
line1
line2 # following blank line should be removed
line3 # following blank lines should be kept
line4
输出:
line1
line2 # following blank line should be removed
line3 # following blank lines should be kept
line4
Sed
通常逐行处理输入。这就是为什么我们无法处理
跨多行输入。 -z
选项通过以下方式更改行为
将输入行分隔符设置为 NUL 字符。
([^\n]\n)
匹配非空行的最后一个字符。\1
设置为 bac 引用。\n
是中间的空行(要删除)。([^\n])
匹配以下非空行的第一个字符。\2
是 设置为反向引用。
顺便说一句,在 bash
的帮助下,以下内容将适用于任何符合 POSIX 标准的 sed:
#!/bin/bash
# define newline character for replacement
NL=$'\\\n'
sed -E '
:l
N
$!b l
# first slurp all lines in the pattern space
# and perform the replacements over the lines
s/([^'"$NL"']'"$NL"')'"$NL"'([^'"$NL"'])/\1\2/g
' input.txt > no-spaces.txt
关于linux - 我需要从 Ubuntu 中删除 .txt 文件中以白色间距分隔的行的白色间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76807759/