使用sed删除文件中的空行
Remove blank lines in a file using sed
France 211 55 Europe
Japan 144 120 Asia
Germany 96 61 Europe
England 94 56 Europe
Taiwan 55 144 Asia
North Korea 44 2134 Asia
以上是我的数据文件
里面有空行。
这些空行中没有空格或制表符。
我想删除数据中的所有空行。
我搜索了一下 Delete empty lines using SED 给出了完美的答案。
在此之前,我自己写了两段sed代码:
sed -r 's/\n\n+/\n/g' cou.data
sed 's/\n\n\n*/\n/g' cou.data
我试了awk gsub,也没成功。
awk '{ gsub(/\n\n*/, "\n"); print }' cou.data
但它们不起作用,没有任何变化。
我的sed代码哪里做错了?
使用下面的sed
删除所有空行。
sed '/./!d' cou.data
解释:
/./
匹配任何字符,包括换行符。
!
否定选择器,即它使命令应用于与选择器不匹配的行,在这种情况下是空行.
d
删除所选行。
cou.data
是输入文件的路径。
你哪里错了?
以下摘自 How sed
Works 的内容:
sed operates by performing the following cycle on each line of input: first, sed reads one line from the input stream, removes any trailing newline, and places it in the pattern space. Then commands are executed; each command can have an address associated to it: addresses are a kind of condition code, and a command is only executed if the condition is verified before the command is to be executed.
When the end of the script is reached, unless the -n option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed.8 Then the next cycle starts for the next input line.
我特意强调了与您的 sed
示例无法正常工作的原因相关的部分。鉴于您的示例:
- 他们似乎无视
sed
一次读取一行。
- 您试图匹配的尾随换行符(分别在您的第一个和第二个示例中的
\n\n
和 \n\n\n
)实际上并不存在。它们在您的正则表达式模式执行时已被删除,然后在脚本结束时恢复。
如果您的行仅由换行符(换行符或 \n)终止,RobC 的回答很好,因为 SED 以这种方式分隔行。如果您的行以 \r\n (或 CRLF)终止 - 即使在 unix 系统上您也可能有这样做的理由 - 您将不会得到匹配,因为从 sed 的角度来看,该行不为空 - \ r (CR) 算作一个字符。相反,您可以尝试:
sed '/^\r$/d' filename
解释:
^
匹配行的开头
\r
匹配回车 return
$
匹配行尾
d
删除所选行。
filename
是输入文件的路径。
France 211 55 Europe
Japan 144 120 Asia
Germany 96 61 Europe
England 94 56 Europe
Taiwan 55 144 Asia
North Korea 44 2134 Asia
以上是我的数据文件
里面有空行。
这些空行中没有空格或制表符。
我想删除数据中的所有空行。
我搜索了一下 Delete empty lines using SED 给出了完美的答案。
在此之前,我自己写了两段sed代码:
sed -r 's/\n\n+/\n/g' cou.data
sed 's/\n\n\n*/\n/g' cou.data
我试了awk gsub,也没成功。
awk '{ gsub(/\n\n*/, "\n"); print }' cou.data
但它们不起作用,没有任何变化。
我的sed代码哪里做错了?
使用下面的sed
删除所有空行。
sed '/./!d' cou.data
解释:
/./
匹配任何字符,包括换行符。!
否定选择器,即它使命令应用于与选择器不匹配的行,在这种情况下是空行.d
删除所选行。cou.data
是输入文件的路径。
你哪里错了?
以下摘自 How sed
Works 的内容:
sed operates by performing the following cycle on each line of input: first, sed reads one line from the input stream, removes any trailing newline, and places it in the pattern space. Then commands are executed; each command can have an address associated to it: addresses are a kind of condition code, and a command is only executed if the condition is verified before the command is to be executed.
When the end of the script is reached, unless the -n option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed.8 Then the next cycle starts for the next input line.
我特意强调了与您的 sed
示例无法正常工作的原因相关的部分。鉴于您的示例:
- 他们似乎无视
sed
一次读取一行。 - 您试图匹配的尾随换行符(分别在您的第一个和第二个示例中的
\n\n
和\n\n\n
)实际上并不存在。它们在您的正则表达式模式执行时已被删除,然后在脚本结束时恢复。
如果您的行仅由换行符(换行符或 \n)终止,RobC 的回答很好,因为 SED 以这种方式分隔行。如果您的行以 \r\n (或 CRLF)终止 - 即使在 unix 系统上您也可能有这样做的理由 - 您将不会得到匹配,因为从 sed 的角度来看,该行不为空 - \ r (CR) 算作一个字符。相反,您可以尝试:
sed '/^\r$/d' filename
解释:
^
匹配行的开头\r
匹配回车 return$
匹配行尾d
删除所选行。filename
是输入文件的路径。